一.Spring IOC阐述
1.概念
1.1通过第三方产生或获取特定对象的方式。
1.2优点:
降低对象之间的耦合度,在一个系统中的某些类的实现逻辑,你不需要去理解,只需要知道怎么用就可以了。
2.举例
在日常生活中,如果没有饮品店,我们想喝果汁,就得自己买水果等东西自己做,如今,有饮品店,就不需要自己做了。现实中系统开发者是一个团队,团队有很多开发者组成,如果你开发的是一个电商项目,你熟悉商品交易流程,但是不熟悉财务交易,所以,熟悉财务的人员去开发财务接口,而财务接口的逻辑你不需要自己去了解,也就是说,你不需要自己会如何制造果汁就能喝到果汁。正如也许你对这个领域不精通,但你可以把创建对象的主动权交给别人。
3.代码举例
3.1果汁制造类
package com.hly.spring.pojo; /** * @author :hly * @date :2018/5/9 */ public class JuiceMaker { private String beverageShop = null; private Source source; public String makeJuice(){ return "这是一杯由"+beverageShop+"饮品店提供的"+source.getSize()+" "+source.getSugar()+" "+source.getFruit()+" 果汁"; } public String getBeverageShop() { return beverageShop; } public void setBeverageShop(String beverageShop) { this.beverageShop = beverageShop; } public Source getSource() { return source; } public void setSource(Source source) { this.source = source; } }
3.2
果汁材料类
package com.hly.spring.pojo; /** * @author :hly * @date :2018/5/9 */ public class Source { private String fruit; private String sugar; private Integer size; public String getFruit() { return fruit; } public void setFruit(String fruit) { this.fruit = fruit; } public String getSugar() { return sugar; } public void setSugar(String sugar) { this.sugar = sugar; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } }
3.3
清单描述类
3.3.1这是果汁的xml清单描述,它描述了果汁是如何制造的,但是这并不是不熟悉果汁制造的开发人员所关心的。这里的对象产生依靠IOC容器,并不是开发者。
<?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="source" class="com.hly.spring.pojo.Source"> <property name="fruit" value="橙汁"/><!--property:构造方法的参数--> <property name="sugar" value="无糖"/> <property name="size" value="3"/> </bean> <bean id="juiceMaker" class="com.hly.spring.pojo.JuiceMaker"> <property name="beverageShop" value="避风塘"/> <property name="source" ref="source"/><!--ref:引用之前定义的bean--> </bean> </beans>
3.4
测试类
import com.hly.spring.pojo.JuiceMaker; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author :hly * @date :2018/5/9 */ public class Juice { public static void main(String []hly){ ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml"); JuiceMaker juiceMaker = (JuiceMaker)ctx.getBean("juiceMaker"); System.out.print(juiceMaker.makeJuice()); } }
二.Spring IOC设计
1.接口
1.基于BeanFactory(最底层接口)和ApplicationContext(BeanFactory子接口)
2.ApplicationContext是其高级接口之一,对BeanFactory功能进行扩展,大部分情况都会用它作为容器
ClassPathXmlApplicationContext是其子类,实现初始化IOC容器,然后就可以通过IOC容器获得资源了;
3.getBean获取配置给IOC容器的Bean,使用该方法时,对Bean进行初始化。
4.getAaes获取别名的方法;
2.Spring Bean的生命周期
2.1.BeanPostProcessor实现类
如果实现了BeanPostProcessor接口的postProcessAfterInitialization方法的完成了调用,Bean就存在于SpringIOC的容器中了,使用者就可以获取Bean的服务。服务器正常关闭,或者遇到其他关闭SpringIOC的事件,就会调用对应的方法销毁Bean。
package com.hly.spring.pojo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * @author :hly * @date :2018/5/9 */ public class BeanPostProcessorImpl implements BeanPostProcessor { //bean.getClass().getSimpleName() //Bean实现了这个方法就会调用这个方法 @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("【" + bean.getClass().getSimpleName() + "】" + beanName + "开始实例化"); return bean; } //实现了BeanPostProcessor接口的postProcessAfterInitialization方法的完成了调用,Bean就存在于SpringIOC的容器中了,使用者就可以获取Bean的服务 @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("【" + bean.getClass().getSimpleName() + "】" + beanName + "实例化完成"); return bean; } }
2.2生命周期实现类
package com.hly.spring.pojo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @author :hly * @date :2018/5/9 */ public class JuiceMaker implements BeanNameAware ,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{ private String beverageShop = null; private Source source; public void init(){ System.out.println("【"+this.getClass().getSimpleName()+"】"+"执行自定义初始化方法"); } public void myDestroy(){ System.out.println("【"+this.getClass().getSimpleName()+"】"+"执行自定义销毁方法"); } public String makeJuice(){ return "这是一杯由"+beverageShop+"饮品店提供的"+source.getSize()+" "+source.getSugar()+" "+source.getFruit()+" 果汁"; } public String getBeverageShop() { return beverageShop; } public void setBeverageShop(String beverageShop) { this.beverageShop = beverageShop; } public Source getSource() { return source; } public void setSource(Source source) { this.source = source; } //以下为生命周期方法,按顺序 @Override public void setBeanName(String s) { System.out.println("【"+this.getClass().getSimpleName()+"】"+"调用BeanNameAware接口的setBeanFactory方法"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("【"+this.getClass().getSimpleName()+"】"+"调用BeanFactoryAware接口的setBeanFactory方法"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("【"+this.getClass().getSimpleName()+"】"+"调用setApplicationContext接口的setApplicationContext方法"); } //这里是postProcessBeforeInitialization,在BeanPostProcessImpl方法里 @Override public void afterPropertiesSet() throws Exception { System.out.println("【"+this.getClass().getSimpleName()+"】"+"调用InitializingBean接口的afterPropertiesSet方法"); } //这里是postProcessAfterInitialization,在BeanPostProcessImpl方法里 @Override public void destroy() throws Exception { System.out.println("调用DisposableBean的destroy方法"); } //之后是自定义销毁方法 }
<?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--> <bean id="beanPostProcessor" class="com.hly.spring.pojo.BeanPostProcessorImpl"/> <bean id="source" class="com.hly.spring.pojo.Source"> <property name="fruit" value="橙汁"/><!--property:构造方法的参数--> <property name="sugar" value="无糖"/> <property name="size" value="3"/> </bean> <!--自定义初始化和销毁方法--> <bean id="juiceMaker" class="com.hly.spring.pojo.JuiceMaker" init-method="init" destroy-method="destroy"> <property name="beverageShop" value="避风塘"/> <property name="source" ref="source"/><!--ref:引用之前定义的bean--> </bean> </beans>
2.3测试类
import com.hly.spring.pojo.JuiceMaker; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author :hly * @date :2018/5/9 */ public class Juice { public static void main(String []hly){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml"); JuiceMaker juiceMaker = (JuiceMaker)ctx.getBean("juiceMaker"); System.out.println(juiceMaker.makeJuice()); ctx.close(); } }