【Java】Spring之控制反转(IoC)(二)

时间:2024-01-12 16:59:14

控制反转(IoC)  

  IoC:Inverse of Control(控制反转)

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

依赖注入

  1、构造器注入

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

  2、setter 方法注入

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

  案例

  1、新建Dao接口 InjectionDao.java

 package com.test.spring.injection;

 public interface InjectionDao {

     public int save(String str);

 }

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

 package com.test.spring.injection;

 public class InjectionDaoImpl implements InjectionDao {

     public int save(String str) {
System.out.println("InjectionDaoImpl: 模拟数据库保存--" + str);
return 1;
}
}

  3、新建Service接口 InjectionService.java

 package com.test.spring.injection;

 public interface InjectionService {

     public int save(String str);

 }

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

 package com.test.spring.injection;

 public class InjectionServiceImpl implements InjectionService {

     private InjectionDao injectionDao;

     public InjectionServiceImpl() {
} public InjectionServiceImpl(InjectionDao injectionDao) {
this.injectionDao = injectionDao;
} public int save(String str) {
System.out.println("InjectionServiceImpl:业务逻辑保存--"+ str);
injectionDao.save(str);
return 1;
} public void setInjectionDao(InjectionDao injectionDao) {
this.injectionDao = injectionDao;
} }

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- Spring 注入 -->
<!-- 构造注入 -->
<bean id="injectionService" class="com.test.spring.injection.InjectionServiceImpl">
<constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg>
</bean> <bean id="injectionDao" class="com.test.spring.injection.InjectionDaoImpl"></bean> <!-- 设置注入
<bean id="injectionService" class="com.test.spring.injection.InjectionServiceImpl">
<property name="injectionDao" ref="injectionDao"></property>
</bean> <bean id="injectionDao" class="com.test.spring.injection.InjectionDaoImpl"></bean>
--> </beans> 

  6、编辑测试类,TestSpringInjection.java

 package com.test.spring.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.spring.bean.BeanPerson;
import com.test.spring.injection.InjectionService; public class TestSpringInjection { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-injection.xml");
InjectionService injectionService = (InjectionService) context.getBean("injectionService");
injectionService.save("ont record");
} }

注入Bean的生命周期

  1、新建一个bean,BeanLifeCycle.java

 package com.test.spring.bean;

 import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class BeanLifeCycle implements InitializingBean, DisposableBean{ public void start() {
System.out.println("Bean start ...");
} public void stop() {
System.out.println("Bean stop ...");
} public void destroy() throws Exception {
System.out.println("Bean destroy ..."); } public void afterPropertiesSet() throws Exception {
System.out.println("Bean afterPropertiesSet ...");
} public void defaultInit() {
System.out.println("Bean defaultInit ...");
} public void defaultDestroy() {
System.out.println("Bean defaultDestroy ...");
}
}

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-init-method="defaultInit" default-destroy-method="defaultDestroy"> <bean id="beanLifeCycle" class="com.test.spring.bean.BeanLifeCycle" init-method="start" destroy-method="stop"></bean> </beans>

  3、编辑测试类,TestSpringLife.java

 package com.test.spring.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.spring.bean.BeanLifeCycle; public class TestSpringLife { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-life.xml");
BeanLifeCycle beanLifeCycle = (BeanLifeCycle) context.getBean("beanLifeCycle");
context.close();
} }

  4、测试结果

    【Java】Spring之控制反转(IoC)(二)