Ioc(控制反转)是一种通过描述并通过第三方去产生或获取特定对象的方式。通俗来讲,Ioc能够实现解耦合。将我们开发的各种Bean放入Ioc容器中,然后我们通过描述就可以得到它们。
2、Spring Ioc容器的设计Ioc容器主要依靠BeanFactory和ApplicationContext两个接口。ApplicationContext是BeanFactory的子接口并且对BeanFactory功能做了许多扩展,开发时,大部分都采用ApplicationContext作为Spring Ioc容器。
3、Spring Ioc容器的初始化和依赖注入在Spring Ioc中进行Bean的定义和初始化共有两大步骤:
(1)Bean的定义
Bean的定义分为三步:Resource定位 BeanDefinition的载入 BeanDefinition的注册 这三步均不会创建Bean实例
(2)Bean的初始化和依赖注入
Spring Bean有个初始化配置选项lazy-init。默认情况下值为default,实际值为false 即Ioc容器默认会自动初始化Bean。如果设置为true,只有在调用getBean方法时才会进行Bean的初始化并完成依赖注入。
4、Spring Bean的生命周期Bean在容器中的生命周期,即Ioc容器初始化和销毁Bean的过程:
(1)初始化依赖注入:创建Bean
(2)如果Bean实现了BeanNameAware接口的setBeanName方法,则调用该方法
(3)如果Bean实现了BeanFactoryAware接口的setBeanFactory方法,则调用该方法
(4)如果Bean实现了ApplicationContextAware接口的setApplicationContext方法,并且Ioc容器时ApplicationContext接口的实现类,则调用该方法
(5)如果Bean实现了BeanPostProcessor接口的postProcessBeforeInitialization方法,则调用该方法 (针对全部Bean)
(6)如果Bean实现了BeanFactoryPostProcessor接口的afterPropertiesSwt方法,则调用该方法
(7)如果Bean自定义了初始化方法,则调用已经定义的初始化方法
(8)如果Bean实现了BeanPostProcessor接口的postProcesAfterInitialization方法,则调用该方法 (针对全部Bean)
(9)如果Bean实现了接口DisposableBean的destory方法,那么就会调用它
(10)如果定义了自定义销毁方法,则会调用自定义销毁方法
5、测试(1)使用IDEA构建项目时,在pom.xml中加入spring相应依赖以及测试所用依赖junit
org.springframework spring-context5.3.14 junit junit4.12 test
(2)定义pojo类
package com.ssm.spring.pojo; public class Source { private String fruit; private String sugar; private String 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 String getSize() { return size; } public void setSize(String size) { this.size = size; } }
package com.ssm.spring.pojo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class JuiceMaker implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean { private String beverageShop = null; //定义制作果汁的店家,类型为String private Source source= null; //定义果汁的资源,类型为source 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; } //自定义初始化 public void init() { System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义初始化方法"); } //自定义销毁 public void myDestroy(){ System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义销毁方法"); } public String makeJuice() { String juice = "这是一杯由"+beverageShop+"饮品店,提供的"+source.getSize()+source.getSugar()+source.getFruit(); return juice; } //实现BeanFactoryAware接口的setBeanFactory方法 @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("【"+this.getClass().getSimpleName()+"】调用BeanFactoryAware接口的setBeanFactory方法"); } //实现BeanNameAware接口的setBeanName方法 @Override public void setBeanName(String s) { System.out.println("【"+this.getClass().getSimpleName()+"】调用BeanNameAware接口的setBeanName方法"); } //实现DisposableBean接口的destroy方法 @Override public void destroy() throws Exception { System.out.println("调用DisposableBean接口的destroy方法"); } //实现nitializingBean接口的afterPropertiesSet方法 @Override public void afterPropertiesSet() throws Exception { System.out.println("【"+this.getClass().getSimpleName()+"】调用InitializingBean接口的afterPropertiesSet方法"); } //实现ApplicationContextAware接口的setApplicationContext方法 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("【"+this.getClass().getSimpleName()+"】调用ApplicationContextAware接口的setApplicationContext方法"); } }
(3)使用xml配置方式,将Pojo中定义的Bean装配到Ioc容器
<--! 该部分实现Source类装配到Ioc中> <--! 该部分实现JuiceMaker类装配到Ioc中> <--!此处引用了id=source的bean,即将Source类装入juiceMaker中>
(4)编写测试代码
@Test public void test1(){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml"); //spring-cfg.xml为上述装配bean的配置文件 JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean("juiceMaker");//使用getBean获取JuiceMaker实例 System.out.println(juiceMaker.makeJuice()); ctx.close(); }
(5)测试结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)