0
点赞
收藏
分享

微信扫一扫

Spring - IOC(控制反转) & DI(依赖注入)


Spring - IOC(控制反转) & DI(依赖注入)_IOC

Spring - IOC(控制反转) & DI(依赖注入)_Spring_02

Spring - IOC(控制反转) & DI(依赖注入)_控制反转_03

Spring - IOC(控制反转) & DI(依赖注入)_IOC_04


一、IOC(控制反转)

Spring - IOC(控制反转) & DI(依赖注入)_Spring_05

Spring - IOC(控制反转) & DI(依赖注入)_依赖注入_06
Spring - IOC(控制反转) & DI(依赖注入)_控制反转_07



二、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


代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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 -->
<bean id="userService" class="com.imooc.ioc.demo1.UserServiceImpl">
<property name="name" value="李四"/>
</bean>

</beans>
package com.imooc.ioc.demo1;

/**
* Created by jt on 2017/10/8.
*/
public interface UserService {

public void sayHello();
}
package com.imooc.ioc.demo1;

/**
* Created by jt on 2017/10/8.
*/
public class UserServiceImpl implements UserService{

// 添加属性:
private String name;

public void sayHello() {
System.out.println("Hello Spring" + name);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
package com.imooc.ioc.demo1;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

/**
* Created by jt on 2017/10/8.
*/
public class SpringDemo1 {

@Test
/**
* 传统方式开发
*/
public void demo1(){
// UserService userService = new UserServiceImpl();
UserServiceImpl userService = new UserServiceImpl();
// 设置属性:
userService.setName("张三");
userService.sayHello();
}

@Test
/**
* Spring的方式实现
*/
public void demo2(){
// 创建Spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过工厂获得类:
UserService userService = (UserService) applicationContext.getBean("userService");

userService.sayHello();
}

@Test
/**
* 读取磁盘系统中的配置文件
*/
public void demo3(){
// 创建Spring的工厂类:
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("c:\\applicationContext.xml");
// 通过工厂获得类:
UserService userService = (UserService) applicationContext.getBean("userService");

userService.sayHello();
}

@Test
/**
* 传统方式的工厂类:BeanFactory
*/
public void demo4(){
// 创建工厂类:
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
// 通过工厂获得类:
UserService userService = (UserService) beanFactory.getBean("userService");

userService.sayHello();
}

@Test
/**
* 传统方式的工厂类:BeanFactory
*/
public void demo5(){
// 创建工厂类:
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("c:\\applicationContext.xml"));
// 通过工厂获得类:
UserService userService = (UserService) beanFactory.getBean("userService");

userService.sayHello();
}
}


举报

相关推荐

0 条评论