H__D  

控制反转(IoC)  

  IoC:Inverse of Control(控制反转)

  • 读作“反转控制”,更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来管理。
  • 正控:若要使用某个对象,需要自己去负责对象的创建
  • 反控:若要使用某个对象,只需要从 Spring 容器中获取需要使用的对象,不关心对象的创建过程,也就是把创建对象的控制权反转给了Spring框架

依赖注入

  1、构造器注入

  构造器注入,顾名思义就是被注入的对象通过在其构造方法中声明依赖对象的参数列表,让外部知道它需要哪些依赖对象。

  2、setter 方法注入

  对于 JavaBean 对象而言,我们一般都是通过 getter 和 setter 方法来访问和设置对象的属性。所以,当前对象只需要为其所依赖的对象提供相对应的 setter 方法,就可以通过该方法将相应的依赖对象设置到被注入对象中

  案例

  1、新建Dao接口 InjectionDao.java

1 package com.test.spring.injection;
2 
3 public interface InjectionDao {
4     
5     public int save(String str);
6 
7 }

  2、实现接口InjectionDao,类InjectionDaoImpl.java

1 package com.test.spring.injection;
2 
3 public class InjectionDaoImpl implements InjectionDao {
4 
5     public int save(String str) {
6         System.out.println("InjectionDaoImpl: 模拟数据库保存--" + str);
7         return 1; 
8     }
9 }

  3、新建Service接口 InjectionService.java

1 package com.test.spring.injection;
2 
3 public interface InjectionService {
4     
5     public int save(String str);
6 
7 }

  4、实现接口InjectionService,类InjectionServiceImpl.java

 1 package com.test.spring.injection;
 2 
 3 public class InjectionServiceImpl implements InjectionService {
 4 
 5     private InjectionDao injectionDao;
 6     
 7     public InjectionServiceImpl() {
 8     }
 9      
10     public InjectionServiceImpl(InjectionDao injectionDao) {
11         this.injectionDao = injectionDao;
12     }
13      
14     public int save(String str) {
15         System.out.println("InjectionServiceImpl:业务逻辑保存--"+ str);
16         injectionDao.save(str);
17         return 1;
18     }
19 
20     public void setInjectionDao(InjectionDao injectionDao) {
21         this.injectionDao = injectionDao;
22     }
23 
24 }

  5、新建spring配置文件,spring-injection.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 7     xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11     
12     <!-- Spring 注入 -->
13     <!--  构造注入 -->
14     <bean id="injectionService" class="com.test.spring.injection.InjectionServiceImpl">
15         <constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg>
16     </bean>
17     
18     <bean id="injectionDao" class="com.test.spring.injection.InjectionDaoImpl"></bean>
19     
20     
21     
22     <!--  设置注入 
23     <bean id="injectionService" class="com.test.spring.injection.InjectionServiceImpl">
24         <property name="injectionDao" ref="injectionDao"></property>
25     </bean>
26     
27     <bean id="injectionDao" class="com.test.spring.injection.InjectionDaoImpl"></bean>
28     -->
29     
30     
31 </beans> 

   6、编辑测试类,TestSpringInjection.java

 1 package com.test.spring.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.test.spring.bean.BeanPerson;
 7 import com.test.spring.injection.InjectionService;
 8 
 9 public class TestSpringInjection {
10         
11     public static void main(String[] args) {
12         
13         ApplicationContext context = new ClassPathXmlApplicationContext("spring-injection.xml");
14         InjectionService injectionService = (InjectionService) context.getBean("injectionService");
15         injectionService.save("ont record");
16     }
17     
18 }

 注入Bean的生命周期

  1、新建一个bean,BeanLifeCycle.java

 1 package com.test.spring.bean;
 2 
 3 import org.springframework.beans.factory.DisposableBean;
 4 import org.springframework.beans.factory.InitializingBean;
 5 
 6 public class BeanLifeCycle implements InitializingBean, DisposableBean{
 7 
 8 
 9     public void start() {
10         System.out.println("Bean start ...");
11     }
12 
13     public void stop() {
14         System.out.println("Bean stop ...");
15     }
16     
17     public void destroy() throws Exception {
18         System.out.println("Bean destroy ...");
19         
20     } 
21   
22     public void afterPropertiesSet() throws Exception {
23         System.out.println("Bean afterPropertiesSet ...");
24     }
25 
26     public void defaultInit() {
27         System.out.println("Bean defaultInit ...");
28     }
29 
30     public void defaultDestroy() {
31         System.out.println("Bean defaultDestroy ...");
32     }
33

  2、新建spring配置文件,spring-life.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 7     xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
11         
12         default-init-method="defaultInit" default-destroy-method="defaultDestroy">
13     
14     <bean id="beanLifeCycle" class="com.test.spring.bean.BeanLifeCycle" init-method="start" destroy-method="stop"></bean>
15     
16 </beans>

  3、编辑测试类,TestSpringLife.java

 1 package com.test.spring.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.test.spring.bean.BeanLifeCycle;
 7 
 8 
 9 public class TestSpringLife {
10         
11     public static void main(String[] args) {
12         
13         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-life.xml");
14         BeanLifeCycle beanLifeCycle = (BeanLifeCycle) context.getBean("beanLifeCycle");
15         context.close();
16     }
17     
18 }

  4、测试结果

    

 

posted on 2019-08-31 01:25  H__D  阅读(407)  评论(0编辑  收藏  举报