Spring 4.0 学习日记(1) --IOC 控制反转概念解释

来源:互联网 发布:尚硅谷大数据视频 编辑:程序博客网 时间:2024/06/18 03:14

写在前面 以前的那个坑了 被提溜去做手顺作业的测试了 坑 真坑 打死不干这种活了

说spring 主要的特点就是 IOC 和 AOP了 先说IOC
恩…
先看一段代码
很简单的Dao层→Service层实现 这本来没什么问题 不引入框架的原始JDBC就是这么个原理 可是这就出现类与类之间的耦合性很重 不符合的高内聚低耦合的目的

package com.wow.StudentDao;public interface StudentDao {    public void getStudent();}
package com.wow.StudentDaoImpl;import com.wow.StudentDao.StudentDao;public class StudentDaoImpl implements StudentDao{    @Override    public void getStudent() {        System.out.println("Student info !!!");    }}
package com.wow.StudentService;public interface StudentService {    public void getStudent();}
package com.wow.StudentServiceImpl;import com.wow.StudentDao.StudentDao;import com.wow.StudentDaoImpl.StudentDaoImpl;import com.wow.StudentService.StudentService;public class StudentServiceImpl implements StudentService{    StudentDao stuDao = new StudentDaoImpl();    @Override    public void getStudent() {        stuDao.getStudent();        }}

所以做一下小的修改 我们把StudentServiceImpl实现层的代码利用get,set方法修改一下 于是变成了

package com.wow.StudentServiceImpl;import com.wow.StudentDao.StudentDao;import com.wow.StudentService.StudentService;public class StudentServiceImpl implements StudentService{    private StudentDao stuDao = null;    public void setStudentDao (StudentDao stuDao){        this.stuDao = stuDao;    }    @Override    public void getStudent() {        stuDao.getStudent();        }}

修改好了之后 做一个测试类

package com.wow.TestInfo;import com.wow.StudentDaoImpl.StudentDaoImpl;import com.wow.StudentServiceImpl.StudentServiceImpl;public class StudentInfoTest {    public static void main(String[] args) {        StudentServiceImpl stuServiceImpl = new StudentServiceImpl();        stuServiceImpl.setStudentDao(new StudentDaoImpl());        stuServiceImpl.getStudent();    }}

于是run的结果是

Student info !!!

ps.如果实现多个方法例如Teacher的实例 也是一样的 不过这样就体现出了一点 实例化对象并主动的带入实参的值 试想如果一个两个实例还是可以接受的 在大型的企业架构体系中这样的信息是足以让人崩溃的 所以!!! 引入Spring的IOC概念

那么假如IOC之后呢

首先是Spring的文件

这里写图片描述

package com.wower.Paladin;public class PaladinInfo {    private String name;    public void setName(String name){        this.name = name;    }    public void printInfo(){        System.out.println("Paladin info get!!!"+name);    }}

测试类

package com.wower.TestInfo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wower.Paladin.PaladinInfo;public class TestInfo {        public static void main(String[] args) {            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");            PaladinInfo Paladin = (PaladinInfo) context.getBean("PaladinInfoo");            Paladin.printInfo();        }}

beans.xml文件 或者 ApplicationContext.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             http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean name = "PaladinInfoo" class = "com.wower.Paladin.PaladinInfo">    //这里的name对应的是测试类中 context.getBean("PaladinInfoo")中的字符串"PaladinInfoo"     <property name= "name" value = "瓦里安国王"></property>    //此处的name对应的是被测试类中的private String name属性 value则是传递进去的值    </bean></beans>  

run之后打印

Paladin info get!!!瓦里安国王

所以可以很明显的看出测试类中拿到了被测试类的实例 因为有着

<bean name = "PaladinInfoo" class = "com.wower.Paladin.PaladinInfo">

这句映射

所以可以得出结论

IOC 控制反转

控制——–实际上从原先的由运行的类主动创建的实例变成了由spring框架被动的实例化对象
反转——–原先由测试类主动的new一个对象传参进去变成了由spring框架被动的传参

由此也可以得出依赖注入是个什么意思了。


2017.7.21补充

针对最开始的studentInfo类可以用Spring包装一下 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             http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id = "StudentDao" class = "com.wow.StudentDaoImpl">    <bean id = "StudentService" class = "com.wow.StudentServiceImpl">         <property name= "stuDao " ref= "StudentDao"></property>         //ref 映射引用对象 在StudentService中引用的是StudentDao类型的stuDao          //ref= "StudentDao"对应的是bean id = "StudentDao"中的StudentDao         //id = "StudentDao" 指向了ref= "StudentDao"          //想起一个词 依赖注入    </bean></beans>  

那么测试类就相应的变为

package com.wow.TestInfo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wow.StudentService.StudentService;public class TestInfo {        public static void main(String[] args) {            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");            StudentService StuService = (StudentService) context.getBean("StudentService");            StuService.getStudent();                    }}

以上

原创粉丝点击