Spring入门之Bean的实例化方式

Bean的四种实例化方式(也可以说是三种)

bean的实例化方式:

  ①.构造器实例化(无参数构造器,与构造器的访问权限无关),最标准,使用最多。

  ②.静态工厂方法实例化(了解)

  ③.实例工厂方法实例化(了解)

  ④.实现FactoryBean接口实例化:实例工厂变种:集成其他框架使用:SqlSessionFactoryBean

 1、构造器实例化

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("App.xml")
//不写名字默认为App-context.xml
public class App {
    @Autowired
    private ApplicationContext context;

    @Test
    public void testOld(){
        Teacher teacher1 = new Teacher();
        Teacher teacher2 = new Teacher();
        System.out.println(teacher1);
        System.out.println(teacher2);
    }
    @Test
    public void testSpring(){
        //用来检验scope是singleton或prototype的不同
        Teacher teacher1 = context.getBean(Teacher.class);
        Teacher teacher2 = context.getBean(Teacher.class);
        System.out.println(teacher1);
        System.out.println(teacher2);
    }
}
测试类
<?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="teacher" class="com.practice.test_04.constructor.Teacher" scope="singleton"/>
</beans>
App.xml
public class Teacher {

    public Teacher(){
        System.out.println("======构造函数执行了=======");
    }
}
bean类

知识点补充:

  scope作用域

    

2、静态工厂方法实例化

public class Teacher2 {
    public Teacher2(){
        System.out.println("======构造函数执行了=======");
    }
}
bean类
public class Teacher2Factory {
    public static Teacher2 getTeacher2(){
        System.out.println("====静态工厂执行了=====");
        return new Teacher2();
    }
}
beanFactory
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("App.xml")
//不写名字默认为App-context.xml
public class App {
    @Autowired
    private ApplicationContext context;

    @Test
    public void testOld(){
        Teacher2 teacher2 = new Teacher2();
        System.out.println(teacher2);
    }
    @Test
    public void testSpring(){
        Teacher2 teacher2 = context.getBean(Teacher2.class);
        System.out.println(teacher2);
    }
}
测试类
<?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="teacherFactory" class="com.practice.test_04.staticfactory.Teacher2Factory" factory-method="getTeacher2"/>

</beans>
App.xml

3、实例工厂方法实例化

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("App.xml")
//不写名字默认为App-context.xml
public class App {
    @Autowired
    private ApplicationContext context;
    @Test
    public void testOld(){
        Teacher3 teacher3 = new Teacher3();
        System.out.println(teacher3);
    }
    @Test
    public void testSpring(){
        Teacher3 teacher3 = context.getBean(Teacher3.class);
        System.out.println(teacher3);
    }
}
测试类
public class Teacher3 {
    public Teacher3(){
        System.out.println("======构造函数执行了=======");
    }
}
bean类
public class Teacher3Factory {

    public Teacher3 getObject(){
        System.out.println("====实例化工厂执行了=====");
        return new Teacher3();
    }
}
beanFactory
<?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="teacher3Factory" class="com.practice.test_04.instancefactory.Teacher3Factory"/>
    <bean id="teacher" factory-bean="teacher3Factory" factory-method="getObject"/>

</beans>
App.xml

4、实现FactoryBean接口实例化

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("App.xml")
//不写名字默认为App-context.xml
public class App {
    @Autowired
    private ApplicationContext context;
    @Test
    public void testSpring(){
        Teacher4 teacher4 = context.getBean(Teacher4.class);
        System.out.println(teacher4);
    }
}
测试类
public class Teacher4 {
    public Teacher4(){
        System.out.println("======构造函数执行了=======");
    }
}
bean类
public class Teacher4Factory implements FactoryBean<Teacher4> {

    public Teacher4 getObject(){
        System.out.println("====实例化工厂执行了=====");
        return new Teacher4();
    }

    public Class<?> getObjectType() {
        return Teacher4.class;
    }
}
beanFactory
<?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="teacher" class="com.practice.test_04.factorybean.Teacher4Factory"/>

</beans>
App.xml

知识点补充:

  BeanFactory和FactoryBean 的区别:

  BeanFactory:Spring的容器对象,本质是一个factory,是Spring管理对象的工厂 FactoryBean:是Spring框架的一个接口,本质是一个bean,用来约束创建对象的工厂的的行为。

 bean的出始化和销毁

1、<bean id="someBean" class="......" init-method="该类中初始化方法名" destroy-method="该类中销毁方法名"> </bean>

2、init-method:bean生命周期初始化方法,对象创建后就进行调用

3、destroy-method:容器被销毁的时候,如果bean被容器管理,会调用该方法。

4、default-init-method,default-destroy-method.配置文件中所有的bean元素的初始化方法和销毁方法

5、分析原理: 如果bean的scope="prototype",那么容器只负责创建和初始化,销毁方法它并不会被spring容器管理。交给用户自己调用。当bean的scope="prototype"时,Spring容器在启动时,并不会将创建出来的对象放在容器当中。而是在每次获取对象时,都来创建一个新的对象。因此,在容器销毁的时候,并不知道要销毁该对象。因此就不会调用对象的销毁方法。

 6、代码示例

public class Wow {
    public Wow(){
        System.out.println("创建对象");
    }
    public void doWork(){
        System.out.println("开始运行");
    }
    public void init(){
        System.out.println("初始化资源");
    }
    public void close(){
        System.out.println("关闭资源");
    }
}
bean类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("App.xml")
public class App {
    @Autowired
    private ApplicationContext context;
    @Test
    public void testWow(){
        Wow wow1 = context.getBean(Wow.class);
        Wow wow2 = context.getBean(Wow.class);
        wow1.doWork();
    }
}
测试类
<?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="wow" class="com.practice.test_05.Wow" init-method="init" destroy-method="close" scope="prototype"/>
</beans>
App.xml

猜你喜欢

转载自www.cnblogs.com/xfdhh/p/11482578.html