In object-oriented programs, in order to call a member method of a class, you need to instantiate an object of that class. In Spring, there are three ways Bean instantiated, the constructor are instantiated, and the static factory instantiate instantiate an instance factory.
Examples of builder configured to instantiate refers Bean Spring container through the corresponding default constructor class instantiation Bean. By following case demonstrates how to use the constructor to instantiate Bean. 1 Create project and import JAR package to create a name for springDemo02 Web project in MyEclipse, and then copy the Spring JAR dependencies and support package to the lib directory of the project, and published to the classpath. 2 Create Entity class created in a project src directory named com.mengma.instance.constructor package to create an entity class Person1 in the packet, as shown below. Package com.mengma.instance.constructor; public class of Person1 { } . 3 Creating a Spring configuration file created applicationContext.xml Spring configuration file in the package com.mengma.instance.constructor, edited as shown below. <?? XML Version = "1.0" encoding = "UTF-. 8"> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns: the p-= "http://www.springframework.org/schema/p" xsi: schemaLocation = "HTTP: //www.springframework. ORG / Schema / Beans HTTP: // www.springframework.org/schema/beans/spring-beans-3.2.xsd "> <the bean ID =" PERSON1 " class =" com.mengma.instance.constructor.Person1 "/> </ beans> in the above configuration, defines an id of person1 Bean, wherein the class attribute specifies the corresponding class person1.
4 creating a test class to create a test class named InstanceTest1 at com.mengma.instance.constructor package, edited as shown below. Package com.mengma.instance.constructor; Import org.junit.Test; Import org.springframework.context.ApplicationContext; Import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest1 { @Test public void Test () { // Spring configuration file defines the path String XMLPath = "COM / mengma / instance / constructor / the ApplicationContext.xml" ; // initialize Spring container, load the configuration file, and the bean is instantiated applicationContext applicationContext =new new the ClassPathXmlApplicationContext ( XMLPath); // Get an instance of person1 id of the container by System.out.println (to applicationContext.getBean ( "person1" )); } } above documents, first defined in the configuration file Spring test () method path, and then the Spring container will load the configuration file. While loading, Spring of the container will instantiate class Person1 Bean by implementing the default constructor with no arguments.
5 . Run and view the results
using JUnit test after test run () method is successful, as shown in FIG console output
As can be seen, Spring Bean container has been successful is instantiated, and outputs the result.
Instantiate static factory in Spring, you can also use static factory instantiate Bean. In this way need to provide a static factory method to create an instance of Bean. 1 Create entity classes to create a package called com.mengma.instance.static_factory in the src directory of the project, and create an entity class Person2 In this package, the class with the same Person1, without adding members. 2 . Create a static class factory to create a class called MyBeanFactory at com.mengma.instance.static_factory package, and creates a static method called createBean () in the class for creating instances of Bean, as shown below . Package com.mengma.instance.static_factory; public class MyBeanFactory { // create a static factory method Bean instance public static the Person2 createBean () { return new new the Person2 (); } }
3 . Creating a Spring configuration file created applicationContext.xml Spring configuration file in the package com.mengma.instance.static_factory, edited as shown below. <? XML Version = "1.0" encoding = "UTF-. 8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance "xmlns: the p-=" http://www.springframework.org/schema/p " xsi: schemaLocation =" http://www.springframework.org/schema/beans HTTP: // the WWW. springframework.org/schema/beans/spring-beans-3.2.xsd "> <the bean ID =" PERSON2 " class =" com.mengma.instance.static_factory.MyBeanFactory " Factory -method =" createBean "class attribute specifies the corresponding class factory implementation MyBeanFactory, the factory-method attribute tells Spring container createBean calling the factory class () method to get the Bean instance.
4 creating a test class to create a test class named InstanceTest2 at com.mengma.instance.static_factory package, edited as shown below. Package com.mengma.instance.static_factory; Import org.junit.Test; Import org.springframework.context.ApplicationContext; Import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest2 { @Test public void Test () { // Spring configuration file defines the path String XMLPath = "COM / mengma / instance / static_factory / the applicationContext.xml"; // initialize Spring container, load the configuration files, examples of the bean and the applicationContext applicationContext = new newThe ClassPathXmlApplicationContext ( XMLPath); // Get person2 instance id of the container by System.out.println (to applicationContext.getBean ( "person2" )); } }
5 . Run the program and view the results
using the JUnit test run after the output method, run successfully, the console test ()
Examples of plant by way of example in the Spring, there is one example of the way is to use instances Bean plants. In this way, the plant will not create an instance of the class using a static method of Bean, but Bean directly create an instance of a member method. Meanwhile, in the configuration file, nor need Bean instantiated by class attribute points to directly instantiate classes, but by factory-bean property configuration example of a factory, then factory- Method property to determine which method to use in the plant . 1 Create entity classes to create a package called com.mengma.instance.factory in the src directory of the project, create a Person3 class in this package, the class with the same Person1 class, without adding members. 2 . Create an instance of the class factory to create a class called MyBeanFactory at com.mengma.instance.factory package, edited as shown below. Package com.mengma.instance.factory; public class MyBeanFactory { public MyBeanFactory () { System.out.println ( "person3 plant instantiation" ); } //The method creates the Bean public person3 createBean () { return new new person3 (); } } The above code using the default constructor with no arguments plant output person3 instantiation statements, create an instance of the use of the Bean createBean member methods.
3 . Creating a Spring configuration file created profile applicationContext.xml Spring at com.mengma.instance.factory package, as follows. <? XML Version = "1.0" encoding = "UTF-. 8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance "xmlns: the p-=" http://www.springframework.org/schema/p " xsi: schemaLocation =" http://www.springframework.org/schema/beans HTTP: // the WWW. springframework.org/schema/beans/spring-beans-3.2.xsd "> <-! configuration example of plant -> <the bean ID =" myBeanFactory " class =" com.mengma.instance.factory.MyBeanFactory "/> < ! < bean id = " </ Beans> in the above code, the first configuration example of a plant Bean, and configured to instantiate Bean. In the id of person3 Bean using Factory -bean instance attribute to specify a factory, the attribute value is the id attribute value factory instance. Use factory-method attribute determine createBean () method in the plant.
4 creating a test class to create a test class named InstanceTest3 at com.mengma.instance.factory package, edited as shown below. Package com.mengma.instance.factory; Import org.junit.Test; Import org.springframework.context.ApplicationContext; Import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest3 { @Test public void Test () { // Spring configuration file defines the path String XMLPath = "COM / mengma / instance / Factory / the applicationContext.xml"; // initialize Spring container, load the configuration file, and the bean is instantiated // initialization Spring container, load the configuration file, and examples of the bean = ApplicationContext the ApplicationContext new new the ClassPathXmlApplicationContext ( XMLPath); // Get person3 instance id of the container by System.out.println (to applicationContext.getBean ( "person3" )); } } . 5 . Run and view the results using JUnit test run test () method, after a successful run, the console output