IOC理论推导
在之前的业务中,用户的需求可能会影响我们原来的代码,我们需要修改原来的代码!如果工程量浩大,修改起来是十分的麻烦。
可以通过使用set接口实现,此时不用修改原来的代码,只需调用者,通过自己的需求来调用不同的实现。
public class UserServiceImpl implements UserService {
private UserDao userDao ;
//利用set实现动态注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUser() {
userDao.getUser();
}
}
- 使用动态注入后,使用权掌握在调用者手上而不是程序猿手上
- 使用set注入后,程序不再具有主动性,而变成了被动的接受对象
这种思想,从本质上解决了问题,程序员不用再去管理对象的创建。系统的耦合性大大降低,可以更加专注的在业务上实现!这就是IOC的原型!
IOC本质
控制反转IOC,是一种设计思想,DI(依赖注入)是实现IOC的一种方法,也有人任务DI只是IOC的另一种说法。没有IOC的程序中,我们使用面向对象编程,对象的创建与对象之间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓的控制反转就是:获得依赖对象的方式反转了
采用XML方式配置bean的定义信息和实现分离的,而采用注解的方式可以把两者合为一体,bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置
控制反转是一种通过描述(XML或注解)并通过第三方生产或获取特定对象的方式。在Spring中实现控制反转的的是IOC容器,其实现方法是依赖注入(DI)
HelloSpring
pojo
public class Hello {
private String str;
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
beans.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">
<!--
之前:Hello hello = new Hello();
现在: bean:创建一个对象 id:变量名(对象名) class:类
property:属性 name:属性名(set()中的参数) value:属性值
-->
<bean id="hello" class="com.wjq.pojo.Hello">
<!-- 本质还是通过set依赖注入,当删除set()时,会报错-->
<property name="str" value="Spring"/>
</bean>
</beans>
官网模板
下示例显示了基于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 id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
test
public class MyTest {
public static void main(String[] args) {
//获取Spring的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//获取对象
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello);
}
}
第一个例子的beans.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 id="daoImpl" class="com.wjq.dao.UserDaoImpl"/>
<bean id="daoMysql" class="com.wjq.dao.UserDaoMysql"/>
<bean id="daoYY" class="com.wjq.dao.UserDaoYY"/>
<bean id="serviceImpl" class="com.wjq.service.UserServiceImpl">
<!-- value:对基本数据类型赋值-->
<!-- ref:对Spring容器中的对象赋值-->
<!-- set()中的参数赋值的对象是daoImpl-->
<property name="userDao" ref="daoImpl"/>
</bean>
</beans>
IOC创建对象的方式
-
无参创建
<!-- 默认调用无参构造器--> <bean id="user" class="com.wjq.pojo.User"> <property name="name" value="www"/> </bean>
-
有参创建
-
index
<bean id="user" class="com.wjq.pojo.User"> <constructor-arg index="0" value="aaa"/> </bean>
-
type
<bean id="user" class="com.wjq.pojo.User"> <constructor-arg type="java.lang.String" value="bbb"/> </bean>
-
name
<bean id="user" class="com.wjq.pojo.User"> <constructor-arg name="name" value="ccc"/> </bean>
-
<!-- 只要在此注册,即使在测试类中没有获得对象,也会调用构造器,因为在注册的时候Spring会自动创建对象,即此时已经调用构造器-->
<bean id="useTwo" class="com.wjq.pojo.UserTwo">
<property name="name" value="two"/>
</bean>
小结:对象的创建会由Spring帮忙完成,我们在测试类中只是获取此对象
上一节——> 简介 、优点、组成、扩展
如有不对的地方欢迎指出,共同进步!