maven dependence
<dependencies> <!--IOC相关依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
applicationContext.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"> </beans>
1, using the default constructor with no arguments instantiated bean
1.1 Create a Student entity class
public class Student { private String name; private Integer age; public void say() { System.out.println("I'm a Student"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
1.2 fitted in the Student object file applicationContext.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"> <! - fitted into the IOC container objects Studnt -> < the bean ID = "Student" class = "com.demo.domain.Student" /> </beans>
1.3 Test
private ApplicationContext ac; @Before public void the init () { // Load Profile AC = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" ); } @Test public void getStudentObjectFromSrpingIoc () { // 1. Get IOC Student object id of the bean container according to Student Student = (Student) ac.getBean ( "Student" ); // 2. say student.say (); }
2. static factory method to instantiate bean
2.1 Creating Teacher entity classes
public class Teacher { public void say() { System.out.println("I'm a teacher"); } }
2.2 Creating a static factory class
public class StaticFactory { /** * Teacher for creating objects * @return */ public static Teacher createTeacher() { return new Teacher(); } }
2.3 static factory method for assembling objects Teacher
<?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"> <! - fitted into the IOC container objects Studnt -> < the bean ID = "Student" class = "com.demo.domain.Student" /> <! - static factory method, the object is fitted into the Teacher vessel IOC -> < the bean ID = "Teacher" class = "com.demo.factory.StaticFactory" Factory-Method = "createTeacher" /> </beans>
2.4 Test
@Test public void getTeaccherObjectFromSrpingIoc () { // 1. Teacher Gets the id of the object according to IOC bean container Teacher = Teacher (Teacher) ac.getBean ( "Teacher" ); // 2. say teacher.say (); }
3, the method of Example of Example bean plant
Cat create 3.1 entity class
public class Cat { public void say() { System.out.println("I'm a cat"); } }
3.2 creates an instance of the class factory
public class InstanceFactory { /** * Cat used to create objects * @return */ public Cat createCat() { return new Cat(); } }
3.3 Example Cat factory to create objects
<?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"> <! - fitted into the IOC container objects Studnt -> < the bean ID = "Student" class = "com.demo.domain.Student" /> <! - static factory method, the object is fitted into the Teacher vessel IOC -> < the bean ID = "Teacher" class = "com.demo.factory.StaticFactory" Factory-Method = "createTeacher" /> <! - Example factory method to instantiate the bean -> <! - 1. Example assembly plant -> < the bean ID = "instanceFactory" class = "com.demo.factory.InstanceFactory" /> <! - example 2. The cat factory to create an object -> < the bean ID = "cat" factory-the bean = "instanceFactory" factory-Method = "createCat" /> </beans>
3.4 Test
@Test public void getCatObjectFromSrpingIoc () { // 1. Get the object to IOC Cat bean container according to the ID Cat CAT = (Cat) ac.getBean ( "CAT" ); // 2. say cat.say (); }
4. Summary
4.1 default no-argument constructor to instantiate bean
Spring's IOC create objects using the fully qualified class name of the class attribute bean tag class configured by reflection, reflection is called the default constructor with no arguments class to instantiate the object.
4.2 static factory method to instantiate bean
Scenario: static factory method to instantiate bean purpose is to an object obtained by the method of assembling the static vessel into the IOC.
Spring disposed bean label configuration file to obtain the object reference returned by the static method and assembled into Spring IOC container through factory-method.
Examples of the method of Example 4.3 bean plants
Scenario: an object is returned to the non-static methods fitted into the vessel IOC.
1, by example as a factory assembled to the IOC bean container
2, through a new configuration of the bean, the use of factory-bean plants of this reference the bean instance, using factory-method wherein the non-static method returns a reference to the object acquired and fitted into the vessel IOC.