3.工厂bean

测试类:

    //测试工厂bean 单多实例
    @Test
    public void test01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("503bean5.xml");
        MyBean myBean1 = context.getBean("myBean", MyBean.class);
        MyBean myBean2 = context.getBean("myBean", MyBean.class);
        System.out.println(myBean1==myBean2);//false工厂类是多实例
        System.out.println(myBean1);
        System.out.println("-------------------单实例测试-");
        //设置为单实例的时候在加载spring配置文件的时候就会创建单实例对象
        ApplicationContext context1=new ClassPathXmlApplicationContext("502bean4.xml");
        Employee emp1 = context1.getBean("emp",Employee.class);
        Employee emp2=context1.getBean("emp",Employee.class);
        System.out.println(emp1==emp2);//单实例 true 可以通过scope属性值设置为单实例(singleton)
        System.out.println("-------------------多实例测试-");
        //设置为多实例的时候在getBean的时候才会创造多实例对象
        Employee emp3 = context1.getBean("emp2",Employee.class);
        Employee emp4=context1.getBean("emp2",Employee.class);
        System.out.println(emp3==emp4);//多实例 false 可以通过scope属性值设置为多实例(prototype)
    }
    //bean的生命周期
    @Test
    public void test02(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("503bean5.xml");
        MyBean bean1 = context.getBean("bean1", MyBean.class);
        System.out.println("对象创建完成");
        System.out.println(bean1);
        //销毁
        context.close();
    }
    //自动装配
    @Test
    public void test03(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("503bean5.xml");
        Employee emp = context.getBean("emp", Employee.class);
        System.out.println(emp);
    }

生命周期测试的结果

工厂被构造
无参构造调用
set方法被调用
初始化方法调用
对象创建完成
MyBean{name='bean的属性被注入'}
对象被销毁

实体类

@Component(value="thisBean")//就是之前写的id值,默认值是类名称首字母小写
public class MyBean {
    private String myName;

    public MyBean() {
        System.out.println("无参构造调用");
    }

    public String getName() {
        return myName;
    }
    public void setName(String name) {
        this.myName = name;
        System.out.println("set方法被调用");
    }
    @Override
    public String toString() {
        return "MyBean{" +
                "name='" + myName + '\'' +
                '}';
    }
    //创建执行的初始化的方法
    public void initMethod(){
        System.out.println("初始化方法调用");
    }
    //销毁的方法
    public void destroyMethod(){
        System.out.println("对象被销毁");
    }
    //测试方法
    public void test(){
        System.out.println("test here");
    }
}

bean的后置处理器

public class MyBeanPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("后置处理器before方法");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("后置处理器after方法");
        return bean;
    }
}

工厂bean

public class MyFactoryBean implements FactoryBean {
    public MyFactoryBean() {
        System.out.println("工厂被构造");
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
    //定义返回bean
    @Override
    public Object getObject() throws Exception {
        MyBean myBean=new MyBean();
        System.out.println("工厂的set方法被调用");
        myBean.setName("abc");
        return myBean;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }
}

503bean5.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="myBean" class="day503FactoryBean.MyFactoryBean"></bean>
    <bean id="bean1" class="day503FactoryBean.MyBean" init-method="initMethod" destroy-method="destroyMethod">
        <property name="name" value="bean的属性被注入" >
        </property>
    </bean>
    <!--配置后置处理器 所有的bean都会执行该方法-->
    <!--<bean id="myBeanPost" class="day503FactoryBean.MyBeanPost"></bean>-->
    <!--自动装配autowire
    byName:bean id名字必须和属性的名字必须一致
    byType:每种类型只能有一个
    -->
    <bean id="emp" class="day502IOC.Employee" autowire="byName"></bean>
    <bean id="dep" class="day502IOC.Department"></bean>
</beans>

502bean4.xml

<!--默认是单实例-->
    <bean id="emp" class="day502IOC.Employee">
        <property name="ename" value="charles"></property>
        <!--内部bean嵌套创建对象-->
        <property name="dep">
            <bean id="dept" class="day502IOC.Department">
                <property name="did" value="1"></property>
            </bean>
        </property>
    </bean>
    <!--级联赋值1(和外部bean相似)多实列-->
    <bean id="emp2" class="day502IOC.Employee" scope="prototype">
        <property name="ename" value="charles"></property>
        <!--内部bean嵌套创建对象-->
        <property name="dep" ref="dept2"></property>
    </bean>
    <bean id="dept2" class="day502IOC.Department">
        <property name="did" value="2"></property>
    </bean>

 

上一篇:idea项目在maven projects中显示灰色的解决办法


下一篇:@Configuration属性proxyBeanMethods详解