一、IOC(控制反转)
二、DI(依赖注入)
DI Dependency Injection 依赖注入的概念,就是在Spring创建这个对象的过程中,将这个对象所依赖的属性注入进去。
三、BeanFactory & ApplicationContext
(1)BeanFactory
1、无国际化功能
2、getBean 时才实例化对象
(2)ApplicationContext
1、有国际化功能
2、加载配置文件时,就把所有单例模式的Bean实例化好,如果是多例模式的话,就等getBean的时候再实例化好
四、工程读取文件 & 磁盘读取文件
(1)工程读取文件
1、ClassPathXmlApplicationContext
2、ClassPathResource
(2)磁盘读取文件
1、FileSystemXmlApplicationContext
2、FileSystemResource
代码:
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--UserService的创建权交给了Spring--><beanid="userService"class="com.imooc.ioc.demo1.UserServiceImpl"><propertyname="name"value="李四"/></bean></beans>
packagecom.imooc.ioc.demo1; /*** Created by jt on 2017/10/8.*/publicinterfaceUserService { publicvoidsayHello(); }
packagecom.imooc.ioc.demo1; /*** Created by jt on 2017/10/8.*/publicclassUserServiceImplimplementsUserService{ // 添加属性:privateStringname; publicvoidsayHello() { System.out.println("Hello Spring"+name); } publicStringgetName() { returnname; } publicvoidsetName(Stringname) { this.name=name; } }
packagecom.imooc.ioc.demo1; importorg.junit.Test; importorg.springframework.beans.factory.BeanFactory; importorg.springframework.beans.factory.xml.XmlBeanFactory; importorg.springframework.context.ApplicationContext; importorg.springframework.context.annotation.Bean; importorg.springframework.context.support.ClassPathXmlApplicationContext; importorg.springframework.context.support.FileSystemXmlApplicationContext; importorg.springframework.core.io.ClassPathResource; importorg.springframework.core.io.FileSystemResource; /*** Created by jt on 2017/10/8.*/publicclassSpringDemo1 { @Test/*** 传统方式开发*/publicvoiddemo1(){ // UserService userService = new UserServiceImpl();UserServiceImpluserService=newUserServiceImpl(); // 设置属性:userService.setName("张三"); userService.sayHello(); } @Test/*** Spring的方式实现*/publicvoiddemo2(){ // 创建Spring的工厂ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml"); // 通过工厂获得类:UserServiceuserService= (UserService) applicationContext.getBean("userService"); userService.sayHello(); } @Test/*** 读取磁盘系统中的配置文件*/publicvoiddemo3(){ // 创建Spring的工厂类:ApplicationContextapplicationContext=newFileSystemXmlApplicationContext("c:\\applicationContext.xml"); // 通过工厂获得类:UserServiceuserService= (UserService) applicationContext.getBean("userService"); userService.sayHello(); } @Test/*** 传统方式的工厂类:BeanFactory*/publicvoiddemo4(){ // 创建工厂类:BeanFactorybeanFactory=newXmlBeanFactory(newClassPathResource("applicationContext.xml")); // 通过工厂获得类:UserServiceuserService= (UserService) beanFactory.getBean("userService"); userService.sayHello(); } @Test/*** 传统方式的工厂类:BeanFactory*/publicvoiddemo5(){ // 创建工厂类:BeanFactorybeanFactory=newXmlBeanFactory(newFileSystemResource("c:\\applicationContext.xml")); // 通过工厂获得类:UserServiceuserService= (UserService) beanFactory.getBean("userService"); userService.sayHello(); } }