纸上得来终觉浅
做笔记一个重要的问题是,在没有系统的掌握一项技能之前,无法系统的对其进行总结。 实际上,能够做的就是盲人摸象,一点点总结和记录,在回头的时候做更深入的总结和理解
1.初识Spring IOC
1) IOC即控制反转,inverse of control, 既然是反转,原来的控制权在哪里?,现在的控制权又在哪里?,如何反转。 首先看一张对IOC进行描述的图片:
可以这样来理解:类1中包含了类2,那么在类1实例化的时候,类2会进行实例化,而类2如何实例化是由类1控制的,这时候控制权在类1,如果采用Spring的控制反转的话,用一个文件来确定类1,类2的关系,采用文件综合的来指定类1,类2的关系的这种方式,叫做依赖注入(DI)。 IOC是思想,DI是实现。下面用代码来描述:
2)代码实现
A:传统方式
package com.roadArchitectWeb.dao; public interface AnimalDao { /*所有动物有一个说话的方法*/ public void say(); }
package com.roadArchitectWeb.dao; public class CatDaoImpl implements AnimalDao{ String says="喵喵"; public void say(){ System.out.println("CatDaoImpl.say():"+says); } }
package com.roadArchitectWeb.dao; public class DogDaoImpl implements AnimalDao{ String says="汪汪"; public void say() { System.out.println("DogDaoImpl.say():"+says); } }
package com.roadArchitectWeb.dao; class AnimalSay1{ AnimalDao animalDao = new CatDaoImpl(); public void Say(){ animalDao.say(); } } class AnimalSay2{ AnimalDao animalDao = new CatDaoImpl(); public void Say(){ animalDao.say(); } } class AnimalSay3{ AnimalDao animalDao = new CatDaoImpl(); public void Say(){ animalDao.say(); } } public class AnimalMain { public static void main(String[] args) { AnimalSay1 animalSay = new AnimalSay1(); animalSay.Say(); AnimalSay2 animalSay2 = new AnimalSay2(); animalSay2.Say(); AnimalSay3 animalSay3 = new AnimalSay3(); animalSay3.Say(); } }
结果是:
CatDaoImpl.say():喵喵
CatDaoImpl.say():喵喵
CatDaoImpl.say():喵喵
B:Spring 配置方式
修改CatDaoImpl:
package com.roadArchitectWeb.dao; public class CatDaoImpl implements AnimalDao{ /*这个时候并没有值,会有xml文件注入*/ private String says; /*set方法,为了bean注入*/ public void setSays(String says) { this.says = says; } public void say(){ System.out.println("CatDaoImpl.say():"+says); } /*一个无参的构造方法*/ public CatDaoImpl(){ } }
package com.roadArchitectWeb.dao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; class AnimalSay1{ /*创建Spring的IOX容器对象*/ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); /*从IOC容器中获取Bean实例*/ CatDaoImpl catDaoImpl = (CatDaoImpl)ctx.getBean("CatDaoImpl"); /*调用其中的say方法*/ AnimalDao animalDao = catDaoImpl; public void Say(){ animalDao.say(); } } class AnimalSay2{ AnimalDao animalDao = new CatDaoImpl(); public void Say(){ animalDao.say(); } } class AnimalSay3{ AnimalDao animalDao = new CatDaoImpl(); public void Say(){ animalDao.say(); } } public class AnimalMain { public static void main(String[] args) { AnimalSay1 animalSay1 = new AnimalSay1(); animalSay1.Say(); } }把AnimalSay1中实例化方式catDaoImpl实例化方式改为从xml文件中获取bean;
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 id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl"> <property name="says" value="喵喵喵"></property> </bean> </beans>
CatDaoImpl.say():喵喵喵
2.Spring
上面代码会有这样一个问题,AnimalSay1如果实现类不是CatDaoImpl,而是DogDaoImpl, 当然我们可以在xml中再增加一个bean,然后在AnimalSay1中获取这个bean;不过如果这个步骤也采用注入的方法完成,那么在实现类由CatDaoImpl改为DogDaoImpl的时候,AnimalSay1将不做任何改动。 AnimalSay2和AnimalSay3同AnimalSay1
DogDaoImpl:
package com.roadArchitectWeb.dao; public class DogDaoImpl implements AnimalDao{ private String says; public void setSays(String says) { this.says = says; } public void say() { System.out.println("DogDaoImpl.say():"+says); } public DogDaoImpl(){ } }
package com.roadArchitectWeb.dao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; class AnimalSay1{ private AnimalDao animalDao; public void setAnimalDao(AnimalDao animalDao) { this.animalDao = animalDao; } public void Say(){ animalDao.say(); } public AnimalSay1(){ } } class AnimalSay2{ private AnimalDao animalDao; public void setAnimalDao(AnimalDao animalDao) { this.animalDao = animalDao; } public void Say(){ animalDao.say(); } public AnimalSay2(){ } } class AnimalSay3{ private AnimalDao animalDao; public void setAnimalDao(AnimalDao animalDao) { this.animalDao = animalDao; } public void Say(){ animalDao.say(); } public AnimalSay3(){ } } public class AnimalMain { public static void main(String[] args) { /*创建Spring的IOX容器对象*/ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); /*从IOC容器中获取Bean实例*/ AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1"); /*调用其中的say方法*/ animalSay1.Say(); /*从IOC容器中获取Bean实例*/ AnimalSay2 animalSay2 = (AnimalSay2)ctx.getBean("AnimalSay2"); /*调用其中的say方法*/ animalSay2.Say(); /*从IOC容器中获取Bean实例*/ AnimalSay3 animalSay3 = (AnimalSay3)ctx.getBean("AnimalSay3"); /*调用其中的say方法*/ animalSay3.Say(); } }
<?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="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl"> <property name="says" value="喵喵喵"></property> </bean> <bean id="DogDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl"> <property name="says" value="汪汪汪"></property> </bean> <bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1"> <property name="animalDao" ref="CatDaoImpl"></property> </bean> <bean id="AnimalSay2" class="com.roadArchitectWeb.dao.AnimalSay2"> <property name="animalDao" ref="DogDaoImpl"></property> </bean> <bean id="AnimalSay3" class="com.roadArchitectWeb.dao.AnimalSay3"> <property name="animalDao" ref="CatDaoImpl"></property> </bean> </beans>
结果如下:
CatDaoImpl.say():喵喵喵
CatDaoImpl.say():汪汪汪
CatDaoImpl.say():喵喵喵