Examples of the way Bean entry of Spring

Four kinds of ways to instantiate the Bean (it can be said are the three)

Examples of bean manner:

  ①. Constructor instantiation (no-argument constructor, regardless of the access to the constructor), the most standard, the most used.

  ②. Static factory method to instantiate (understand)

  ③. Examples factory method of Example (Learn)

  . ④ FactoryBean implementation of the interface instances: Example plant varieties : Other integrated framework uses: the SqlSessionFactoryBean

 1, the configuration instantiates

@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(){
        //Is used to test the scope of different prototype or singleton 
        Teacher teacher1 = context.getBean (Teacher. Class ); 
        Teacher teacher2 = context.getBean (. Teacher class ); 
        System.out.println (teacher1); 
        System.out.println (teacher2 ); 
    } 
}
Test category
<?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 ( "====== ======= performed constructor" ); 
    } 
}
bean class

Knowledge Point added:

  scope Scope

    

2, static factory method to instantiate

public  class Teacher2 {
     public Teacher2 () { 
        System.out.println ( "====== ======= performed constructor" ); 
    } 
}
bean class
public  class Teacher2Factory {
     public  static Teacher2 getTeacher2 () { 
        System.out.println ( "static factory ==== performed ================" );
         return  new 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);
    }
}
Test category
<?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, an instance factory method to instantiate

@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);
    }
}
Test category
public  class Teacher3 {
     public Teacher3 () { 
        System.out.println ( "====== ======= performed constructor" ); 
    } 
}
bean class
public  class Teacher3Factory { 

    public Teacher3 the getObject () { 
        System.out.println ( "==== example implementation of chemical ================" );
         return  new 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, the interface instantiation achieve 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);
    }
}
Test category
public  class Teacher4 {
     public Teacher4 () { 
        System.out.println ( "====== ======= performed constructor" ); 
    } 
}
bean class
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

Knowledge Point added:

  BeanFactory and FactoryBean difference:

  BeanFactory: Spring container object is essentially a factory, the factory Spring managed objects FactoryBean: the interface is a Spring framework, is the essence of the behavior of a bean, used to constrain create an object factory.

 The beginning of the bean and destruction

1, <bean id = "someBean" class = "......" init-method = "class initialization method name" destroy-method = "method name to destroy the class"> </ bean>

2, init-method: bean life cycle after initialization method, object creation call

3, destroy-method: when the container is destroyed if the bean is container management, will call the method.

4, initialization method default-init-method, default-destroy-method. All the bean configuration file elements and methods of destruction

5, analysis principle: if the bean scope = "prototype", then the container is only responsible for the creation and initialization methods of destruction and it would not be spring container management. To the user's own call. When the bean scope = "prototype", Spring containers at startup will not create them out of the objects in the container. But each time the object is acquired, have to create a new object. Therefore, when the container destroyed, I do not know to destroy the object. Therefore, the method will not be called to destroy the object.

 6, sample code

public  class Wow {
     public Wow () { 
        System.out.println ( "create object" ); 
    } 
    public  void the doWork () { 
        System.out.println ( "runs" ); 
    } 
    public  void the init () { 
        the System.out .println ( "resource initialization" ); 
    } 
    public  void Close () { 
        System.out.println ( "resource Close" ); 
    } 
}
bean class
@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();
    }
}
Test category
<?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

 

Guess you like

Origin www.cnblogs.com/xfdhh/p/11482578.html