控制反转IOC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IOC的一种方法 。在没有IOC的程序中,我们使用面向对象编程,对象的创建于对象间的依赖完全硬编码在程序中,对象的创建有程序自己控制;控制反转后将对象的创建转移给第三方;
控制反转是一种 通过描述XML(或注解) 并 通过第三方去生产或获取特定对象 的方式。 在Spring中实现控制反转的是IOC容器 ,其 实现方法是依赖注入 (Dependency Injection,DI)
IOC演示:
public interface UserDao { void getUser(); }
public class UserDaoImpl implements UserDao{ public void getUser() { System.out.println("默认获取用户"); } }
public interface UserService { void getUser(); void setUserDao(UserDao userDao); }
public class UserServiceImpl implements UserService { private UserDao userDao ; //利用set进行动态实现值的注入 public void setUserDao(UserDao userDao){ this.userDao = userDao; } public void getUser() { userDao.getUser(); } }
这里使用一个Set方法实现
private UserDao userDao ; //利用set进行动态实现值的注入 public void setUserDao(UserDao userDao){ this.userDao = userDao; } //代替了原先主动在该类中穿创建对象: //userDao = new UserDao();
这种思想,从本质上解决了问题,程序员不用再去管理对象的创建了,系统的耦合性大大降低,可以更加专注在业务的实现上。这就是IOC的原型。
Spring的核心就是基于IOC容器,将对象的创建以及依赖的注入全部交由Spring容器来完成。
首先基于如上接口和业务创建两个接口实现类:
public class UserDaoMysqlImpl implements UserDao{ public void getUser() { System.out.println("mysql获取用户"); } } public class UserOracleImpl implements UserDao{ public void getUser() { System.out.println("oracle获取用户"); } }
创建applicationContext.xml文件
基于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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--一个bean就对应一个实例化一个对象,在Spring中管理--> <bean id="mysqlImpl" class="com.spong.dao.UserDaoMysqlmpl"/> <bean id="oracleImpl" class="com.spong.dao.UserOracleImpl"/> <!--userServiceImpl实例注入ioc容器--> <bean id="userServiceImpl" class="com.spong.service.UserServiceImpl"> <!-- ref:引用Spring容器中创建好的对象 value:具体的值,基本数据类型 --> <!--给实例化对象中的属性赋值,自动调用类中的setUserDao()方法,没有或者方法名不规范则会出错--> <!--ref传入哪个实现类的id,就调用userServiceImpl的set方法注入这个实现类--> <property name="userDao" ref="mysqlImpl"/> </bean> </beans>
测试:
public class MyTest { public static void main(String[] args) { //获取Spring的上下文对象ApplicationContext ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //对象已经在Spring中管理了,使用时直接根据bean id取即可 UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); serviceImpl.getUser(); } }
现在,我们彻底不用在程序中去改动了,要实现不同的操作,只需要在XML中进行修改;
这个过程就叫控制反转:
IOC是一种编程思想,由主动的编程变为被动的接收。
总结为一句话:对象由Spring来创建,管理,装配。
默认使用无参构造创建对象
自定义使用有参构造创建对象
下标赋值
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/> </bean>
参数名赋值
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg name="years" value="7500000"/> <constructor-arg name="ultimateAnswer" value="42"/> </bean>
3. 根据类型赋值(有同类型参数时则会有问题)
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/> </bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。
IOC的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。
DI的实现原理是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,Spring就是通过反射来实现注入的。
理解DI的关键是:“谁依赖谁,为什么需要依赖,谁注入谁,注入了什么”,那我们来深入分析一下:
谁依赖于谁 :是 应用程序依赖于IoC容器 ;
为什么需要依赖 : 应用程序需要IoC容器来提供对象需要的外部资源 ;
谁注入谁 :是 IOC容器注入应用程序依赖的某个对象 ;
注入了什么 :就是 注入某个对象所需要的外部资源 (包括对象、资源、常量数据)。
自定义使用有参构造创建对象:
2. 参数名赋值
【环境搭建】
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
@Data public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; //null private Properties info; }
<bean id="address" class="com.spong.pojo.Address"> <property name="address" value="zhejiang"/> </bean> <bean id="student" class="com.spong.pojo.Student"> <!--基本类型注入 value--> <property name="name" value="ps"/> <!--bean对象注入 ref--> <property name="address" ref="address"/> <!--数组--> <property name="books"> <array> <value>西游记</value> <value>三国演义</value> <value>红楼梦</value> <value>水浒传</value> </array> </property> <!--list--> <property name="hobbies"> <list> <value>唱歌</value> <value>打游戏</value> </list> </property> <!--map--> <property name="card"> <map> <entry key="学生证" value="147596654"/> <entry key="身份证" value="33216541236542512"/> </map> </property> <!--set--> <property name="games"> <set> <value>LOL</value> <value>CS</value> </set> </property> <!--properties--> <property name="info"> <props> <prop key="性别">男</prop> <prop key="年龄">18</prop> </props> </property> <!--null--> <property name="wife"> <null/> </property> </bean>
public class MyTest2 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(); } }
Student{ name='ps', address=Address{address='address'}, books=[西游记, 三国演义, 红楼梦, 水浒传], hobbies=[唱歌, 打游戏], card={学生证=147596654, 身份证=33216541236542512}, games=[LOL, CS], wife='null', info={性别=男, 年龄=18} }
可以使用p命名空间和c命名空间进行注入:
p命名(properties)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--带可选参数名称的传统声明--> <bean name="classic" class="com.example.ExampleBean"> <property name="email" value="[email protected]"/> </bean> <!--带参数名称的p-名称空间声明--> <bean name="p-namespace" class="com.example.ExampleBean" p:email="[email protected]"/> </beans>
c命名(constructor)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="beanTwo" class="x.y.ThingTwo"/> <bean id="beanThree" class="x.y.ThingThree"/> <!-- 带可选参数名称的传统声明 --> <bean id="beanOne" class="x.y.ThingOne"> <constructor-arg name="thingTwo" ref="beanTwo"/> <constructor-arg name="thingThree" ref="beanThree"/> <constructor-arg name="email" value="[email protected]"/> </bean> <!-- 带有参数名称的c-名称空间声明 --> <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo" c:thingThree-ref="beanThree" c:email="[email protected]"/> </beans>
注意:p命名和c命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
<table> <tr> <th> Scope </th> <th> Description </th> </tr> <tr> <td> [singleton](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring- framework-reference/core.html#beans-factory-scopes-singleton) </td> <td> (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. </td> </tr> <tr> <td> [prototype](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring- framework-reference/core.html#beans-factory-scopes-prototype) </td> <td> Scopes a single bean definition to any number of object instances. </td> </tr> <tr> <td> [request](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework- reference/core.html#beans-factory-scopes-request) </td> <td> Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring `ApplicationContext`. </td> </tr> <tr> <td> [session](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework- reference/core.html#beans-factory-scopes-session) </td> <td> Scopes a single bean definition to the lifecycle of an HTTP `Session`. Only valid in the context of a web-aware Spring `ApplicationContext`. </td> </tr> <tr> <td> [application](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring- framework-reference/core.html#beans-factory-scopes-application) </td> <td> Scopes a single bean definition to the lifecycle of a `ServletContext`. Only valid in the context of a web-aware Spring `ApplicationContext`. </td> </tr> <tr> <td> [websocket](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring- framework-reference/web.html#websocket-stomp-websocket-scope) </td> <td> Scopes a single bean definition to the lifecycle of a `WebSocket`. Only valid in the context of a web-aware Spring `ApplicationContext`. </td> </tr> </table>
bean的一个共享实例,并且所有对具有ID或与该bean定义相匹配的ID的bean的请求都会导致该特定的bean实例由Spring容器返回
<!--scope标签修改作用域--> <bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
每次对特定bean提出请求时,bean部署的非单一原型范围都会创建一个新的bean实例
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
原文链接:https://www.cnblogs.com/spang/p/13472236.html