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();
}


someService对象由serviceFactory工厂的getSomeService方法创建

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


    someService对象由com.ba03.ServiceFactory类的getSomeService方法创建

4.Bean的作用域

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();
}



容器初始化之后对象初始化之前

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("销毁之前");
}


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 IoC之Bean的装配)