Spring Bean in the equivalent of java in the class, can be configured and managed by bean xml file.
A, Bean instantiation:
Examples of the constructor, static factory instantiated, instantiate an instance factory.
table of Contents:
Examples of builder:
xml configuration file:
id unique, calss designated Bean implementation class, must be fully qualified class name, you can right-click on "public class Bean1" in Bean1 in Bean1.java file, select Copy Qualifiel Name get. Note the use of "." Separated.
Test function:
First test function defined in xml configuration file path, you can select the right directory view Copy Qualifiel Name to get attention from the com start here, because it is the path separated by /.
Then loading a configuration file to instantiate Bean, id specified by the obtained function getBean instance object, note type conversion.
Complete code:
package com.liu.instance.contructor; public class Bean1 { }
<?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-4.3.xsd"> <bean id="bean1" class="com.liu.instance.contructor.Bean1"></bean> </beans>
Package com.liu.instance.contructor; Import org.springframework.context.ApplicationContext; Import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest { public static void main (String [] args) { // definition configuration file path XMLPath = String "COM / Liu / instance / contructor / beans1.xml" ; // to be instantiated ApplicationContext Bean load configuration file. ApplicationContext = ApplicationContext new new ClassPathXmlApplicationContext (XMLPath); // the above mentioned id configuration file to determine which bean, java objects found in files created by class. Bean1 bean1 = (Bean1) applicationContext.getBean("bean1"); System.out.println(bean1); } }
Run shot:
Examples of static factory:
xml configuration file:
unique id, class for class factory, factory-method is the method name, method to identify which plant.
Static factory class:
Static method returns a Bean2 object.
Package com.liu.instance.static_factory; / * * LSQ * 2019-9-10 * static factory instantiates the Spring instantiated Bean2 * / public class Bean2 { }
Package com.liu.instance.static_factory; / * * LSQ * 2019-9-10 * static factory to instantiate the Spring * / public class MyBean2Factory { // create objects Bean2 public static Bean2 createBean2 () { return new new Bean2 (); } }
Package com.liu.instance.static_factory; Import org.springframework.context.ApplicationContext; Import org.springframework.context.support.ClassPathXmlApplicationContext; / * * LSQ * 2019-9-10 * the Spring a static factory test class instance object * / public class InstanceTest { public static void main (String [] as AGRS) { // definition configuration file path String XMLPath = "COM / Liu / instance / static_factory / beans2.xml" ; // instantiate an object of the ApplicationContext the ApplicationContext = new new the ClassPathXmlApplicationContext (XMLPath ); //Calling function // the getBean id attribute value acquisition function passing objects System.out.println (to applicationContext.getBean ( "bean2" )); } }
<?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-4.3.xsd"> <bean id="bean2" class="com.liu.instance.static_factory.MyBean2Factory" factory-method="createBean2"> </bean> </beans>
Run shot:
Examples of factory instance:
xml configuration file:
The first two bean static factory class static factory, class. The second is bean3, factory-method property configuration example plant, factory-method to determine which method to use in the plant.
Factory class:
Returns a Bean3 object.
Complete code:
Package com.liu.instance.factory; / * * LSQ * 2019-9-10 * Examples of the Spring factory object * / public class Bean3 { }
<?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-4.3.xsd"> <bean id="bean2" class="com.liu.instance.static_factory.MyBean2Factory" factory-method="createBean2"> </bean> </beans>
Package com.liu.instance.factory; / * * LSQ * 2019-9-10 * Examples of the Spring factory object * / public class MyBean3Factory { public MyBean3Factory () { System.out.println ( "Bean3 plant instantiation .. . " ); } public Bean3 createBean3 () { return new new Bean3 (); } }
Package com.liu.instance.factory; Import org.springframework.context.ApplicationContext; Import org.springframework.context.support.ClassPathXmlApplicationContext; / * * LSQ * 2019-9-10 * factory to instantiate the Spring test class * / public class {InstanceTest3 public static void main (String [] args) { // the specified configuration file path String XMLPath = "COM / Liu / instance / Factory / bean3.xml" ; // when ApplicationContext loading configuration file to instantiate Bean ApplicationContext = applicationContext new new the ClassPathXmlApplicationContext (XMLPath); System.out.println(applicationContext.getBean("bean3")); } }
Run shot:
The difference between the three methods: