您的位置:首页 > 编程语言 > Java开发

spring原理

2015-09-10 18:02 555 查看
1、spring框架什么时候被加载?

(1)ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml")执行的时候,

spring容器对象被创建。

(2)同时,applicationContext.xml里面配置的对象被创建到内存中。(内存模型类似于HashMap)。容器利用反射机制创建bean的实例。如下图

package com.run;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.service.SpringService;

public class Run {

public static void testRun(){
//1、创建spring的IOC容器对象
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-bean.xml");
//2、从IOC容器获取spring bean实例
//SpringService sService = (SpringService)ac.getBean("springService");
SpringService sService = (SpringService)ac.getBean(SpringService.class);
//3、调用sayHello方法
sService.sayHello();
}

public static void main(String[] args) {
testRun();
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: