Spring的控制反转

控制反转的发展由来,是为了把控制权从class文件中脱离,从而更少的编译Java源文件。

 

1.main控制:运行简单,操作容易。缺点必须修改源代码来把握控制权

 

2.使用工厂模式:可以通过传入的参数来确定创建哪个对象,创建大部分对象不需要修改源代码

   如果新增了对象,就必须工厂的判断代码,也不符合,封闭和开放原则

 

3.控制反转:只有源代码的修改才需要重新编译,可以完全将控制权交给配置文件

 缺点:编码复杂代码多的问题(spring)

 

面相对象的设计原则:

1.单一职责原则:一个类只做一件事情

2.封闭和开放原则:对修改关闭,对拓展开放

 

通过配置spring.xml的bean来实例化Dog对象

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.       
  8.     <!-- 相当也  Grade grade=new Grade()  
  9.         并给这个对象赋值   grade.setGradeNo("1");  
  10.     -->  
  11.     <bean id="grade" class="cn.et.day20170522.spring.Grade">  
  12.         <property name="gradeNo" value="1"></property>  
  13.         <property name="gradeName" value="1610"></property>  
  14.     </bean>  
  15.     <!--  
  16.         ref引用对象 通过属性名给对象赋值 就相当于调用set方法 
  17.      -->  
  18.     <bean id="stu" class="cn.et.day20170522.spring.Student">  
  19.         <property name="studentNo" value="1"></property>  
  20.         <property name="studentName" value="萌萌哒"></property>  
  21.         <property name="grade" ref="grade"></property>  
  22.     </bean>  
  23.     <!-- 通过带参数的构造器给给对象赋值 通过位置赋值  -->  
  24.     <bean id="stu1" class="cn.et.day20170522.spring.Student">  
  25.         <constructor-arg index="0" value="1"></constructor-arg>  
  26.         <constructor-arg index="1" value="小黑"></constructor-arg>  
  27.         <constructor-arg index="2" ref="grade"></constructor-arg>  
  28.     </bean>  
  29.     <!-- 通过带参数的构造器给给对象赋值 通过类型赋值  -->  
  30.     <bean id="stu2" class="cn.et.day20170522.spring.Student">  
  31.         <constructor-arg type="java.lang.String" value="1"></constructor-arg>  
  32.         <constructor-arg type="java.lang.String" value="小黑"></constructor-arg>  
  33.         <constructor-arg type="cn.et.day20170522.spring.Grade" ref="grade"></constructor-arg>  
  34.     </bean>  
  35. </beans>  
然后可以在Java获取到这个bean,并用它的父接口Animal来接收
[java] view plain copy
  1. public class SpringTest {  
  2.     public static void main(String[] args) {  
  3.         //先创建spring的 IOC 容器  
  4.         ClassPathXmlApplicationContext cpxc=new ClassPathXmlApplicationContext("/cn/et/day20170522/spring/spring.xml");  
  5.           
  6.         /** 
  7.          * Grade grade=new Grade(); 
  8.          * grade.setGradeNo("1"); 
  9.          * Student stu=new Student(); 
  10.          * stu.setStudent("1") 
  11.          */  
  12.         //通过配置文件的id获取对象  
  13.         Student stu=(Student)cpxc.getBean("stu");  
  14.         System.out.println(stu.getGrade().getGradeName());  
  15.           
  16.         Student stu1=(Student)cpxc.getBean("stu1");  
  17.         System.out.println(stu1.getGrade().getGradeName());  
  18.           
  19.         Student stu2=(Student)cpxc.getBean("stu2");  
  20.         System.out.println(stu2.getGrade().getGradeName());  
  21.     }  
  22. }  

这样一来Spring的控制反转便把所有的控制权交给了配置文件,我们完全可以在spring.xml文件中修改bean的class属性来控制创建Animal的其它对象

控制反转的发展由来,是为了把控制权从class文件中脱离,从而更少的编译Java源文件。

 

1.main控制:运行简单,操作容易。缺点必须修改源代码来把握控制权

 

2.使用工厂模式:可以通过传入的参数来确定创建哪个对象,创建大部分对象不需要修改源代码

   如果新增了对象,就必须工厂的判断代码,也不符合,封闭和开放原则

 

3.控制反转:只有源代码的修改才需要重新编译,可以完全将控制权交给配置文件

 缺点:编码复杂代码多的问题(spring)

 

面相对象的设计原则:

1.单一职责原则:一个类只做一件事情

2.封闭和开放原则:对修改关闭,对拓展开放

 

通过配置spring.xml的bean来实例化Dog对象

然后可以在Java获取到这个bean,并用它的父接口Animal来接收

这样一来Spring的控制反转便把所有的控制权交给了配置文件,我们完全可以在spring.xml文件中修改bean的class属性来控制创建Animal的其它对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值