Spring IoC之Bean的装配

  1. 默认装配方式 通过getbean()方式从容器中获取指定的Bean实例,容器首先会调用Bean类的无参构造器,创建空值的实例对象。
    @Test
    public void test01() {
    // 加载Spring配置文件,创建Spring容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba01/applicationContext.xml");
    // 从容器中获取指定的bean对象
    ISomeService service = (ISomeService) ac.getBean("someService");
    service.doFirst();
    service.doSecond();
    }
    public SomeServiceImpl() {
    System.out.println("===========");
    }
    若加入有参就会报错比如SomeServiceImpl(int a);
  1. 动态工厂Bean(创建工厂对象,调用方法)
    public class ServiceFactory {
    public ISomeService getSomeService() {
    return new SomeServiceImpl();
    }
    }

@Test
public void test01() {
// 加载Spring配置文件,创建Spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba02/applicationContext.xml");
// 从容器中获取指定的bean对象
// ServiceFactory sf = (ServiceFactory) ac.getBean("serviceFactory");
// ISomeService service = sf.getSomeService();
//降低类和类之间的耦合度,看不出实现类是谁,工厂是谁
ISomeService service = (ISomeService) ac.getBean("someService");
service.doFirst();
service.doSecond();
}
<bean id="serviceFactory" class="com.ba02.ServiceFactory"></bean>
<bean id="someService" factory-bean="serviceFactory" factory-method="getSomeService"></bean>
someService对象由serviceFactory工厂的getSomeService方法创建

  1. 静态工厂Bean(直接调用静态方法)
    public class ServiceFactory {
    public static ISomeService getSomeService() {
    return new SomeServiceImpl();
    }
    }

    <bean id="someService" class="com.ba03.ServiceFactory" factory-method="getSomeService"></bean>
    someService对象由com.ba03.ServiceFactory类的getSomeService方法创建

4.Bean的作用域
<bean id="someService" class="com.ba04.SomeServiceImpl" scope="prototype"/>
scope="prototype"的bean容器在接受到该类型的对象的请求的时候,会每次都重新生成一个新的对象给请求方;(即真正使用时才创建,每获取一次,都会创建不同的对象)
scope="singleton"的bean是由容器来保证这种类型的bean在同一个容器内只存在一个共享实例(容器初始化时就创建,每次获取的都是同一个对象。默认的)

?????5.Bean后处理器(这个还有一些问题)
public class MyBeanPostProcessor implements BeanPostProcessor {
// bean:当前调用Bean后处理器的Bean对象
//beanname:Bean对象的id
@Override
public Object postProcessAfterInitialization(Object bean, String beanname) throws BeansException {
System.out.println("执行****After****()");
return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanname) throws BeansException {
    System.out.println("执行****Before****()");
    return bean;
}

}

public void test01() {
// 加载Spring配置文件,创建Spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba05/applicationContext.xml");
// 从容器中获取指定的bean对象
ISomeService service1 = (ISomeService) ac.getBean("someService1");
service1.doFirst();
service1.doSecond();
System.out.println("----------------");
ISomeService service2 = (ISomeService) ac.getBean("someService2");
service2.doFirst();
service2.doSecond();
}
<bean id="someService1" class="com.ba05.SomeServiceImpl" />
<bean id="someService2" class="com.ba05.SomeServiceImpl" />
<bean class="com.ba05.MyBeanPostProcessor"></bean>
容器初始化之后对象初始化之前

6.定制Bean的生命始末
public void test01() {
// 加载Spring配置文件,创建Spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba06/applicationContext.xml");
// 从容器中获取指定的bean对象
ISomeService service = (ISomeService) ac.getBean("someService");
service.doFirst();
service.doSecond();
// 销毁方法的执行有两个要求:
// 1)被销毁的对象需要是singleton的,即单例
// 2)容器要显示关闭
((ClassPathXmlApplicationContext) ac).close();// 接口没有实现类有
}

public void initAfter() {
System.out.println("初始化之后");
}

public void preDestory() {
    System.out.println("销毁之前");
}

<bean id="someService" class="com.ba06.SomeServiceImpl"
    init-method="initAfter" destroy-method="preDestory" />

7.Bean的生命周期的可控点很多
step1.对象的创建
step2.执行setAdao()
step3.beanName=someService
step4.获取到beanFactory容器
step5.执行****Before()
step6.当前Bean初始化工作已经完毕
step7.初始化之后
step8.执行****After
()
step9.执行主业务方法
step10. 准备销毁工作,进行销毁流程
step11.销毁之前

8.id与name属性的区别
id的命名需要满足XML对ID属性命名规范;必须以字幕开头,可以包含字母,数字,下划线,连字符,句号,冒号。
name属性值则可以包含各种字符

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,198评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 一、Spring框架 1.1 Spring框架是什么 Spring是一种容器框架,用于配置各个组件(bean)并且...
    Jane_Static阅读 415评论 0 0
  • Spring容器高层视图 Spring 启动时读取应用程序提供的Bean配置信息,并在Spring容器中生成一份相...
    Theriseof阅读 2,862评论 1 24
  • 都市人有个通病,就是怕寂寞、无聊。 一到周末,我们便会呼朋引伴,各种饭局聚会下午茶走起,当然不忘发几条丰富多彩的朋...
    樱花蜗牛阅读 536评论 0 2