Spring is automatically helping us to create objects, how many ways to create beans?
Constructor method Instantiation: (default no parameter) is actually the reflection of the new Instance ().
Static Factory instantiation:
Instance Factory instantiation:
Generally does not change the way it is instantiated. Two other kinds of understanding can be.
<?XML version= "1.0" encoding= "UTF-8"?><!--don't go to Schema,schema is file, local file, you have to lead that head -<Beansxmlns= "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 "><!--demo1 Quick Start ================================================= -<!--The interface and implementation classes are configured in this configuration file, and the full path to the implementation class can be used for factory reflection. - <!--use a <bean> tag to set the class's information, using the id attribute as an identifier for the class. - <!--interfaces, implementation classes, and configuration files are also available. - <!--now there is a factory spring for us to provide, in fact, is to parse this XML file - <!--will you write this factory yourself? You use dom4j to find the bean tag inside, find the attribute value of class, and then you can class.forname () to generate an instance of the class. In fact spring did, but the factory was provided by spring. - <BeanID= "HelloService"class= "Cn.itcast.spring3.demo1.HelloServiceImpl"> <!--using the <property> Tag Injection property value refers to the normal value ref refers to the object - < Propertyname= "Info"value= "Preach Intelligence podcast"></ Property> </Bean> <!--demo1 Quick Start - <!--instantiation of the Demo2bean - <!--by default, a parameterless construction method is used. - <BeanID= "Bean1"class= "Cn.itcast.spring3.demo2.Bean1"></Bean> <!--The second use of static factory instantiation cannot write class, because now spring does not create objects for you directly. - <BeanID= "Bean2"class= "Cn.itcast.spring3.demo2.Bean2Factory"Factory-method= "GetBean2"></Bean> <!--The third instance of using instance factory instantiation - <BeanID= "Bean3"Factory-bean= "Bean3factory"Factory-method= "GetBean3"></Bean> <!--to instantiate the Bean3factory first - <BeanID= "Bean3factory"class= "Cn.itcast.spring3.demo2.Bean3Factory"></Bean> </Beans>
package Cn.itcast.spring3.demo2; /** * Instantiate using parameterless construction method * @author Zhongzh * */ public class Bean1 { /* Public Bean1 (String name) {//There is a parameter structure written here, then the parameterless construction does not execute//There is a parameter without a parameter} */ Span style= "COLOR: #0000ff" >public Bean1 () {System.out.println ( "Use parameterless construction Bean1 method of making a method ... "
package Cn.itcast.spring3.demo2; /** * Instantiate with a static factory * @author Zhongzh * */ public class Bean2 {}
package Cn.itcast.spring3.demo2; /** * Instantiate with instance factory * @author Zhongzh * */ public class bean3{ // Loading the configuration file will load this class and will help you create the }
package Cn.itcast.spring3.demo2; /** * static Factory of Bean2 * @author Zhongzh * */ public class bean2factory { public static Bean2 getBean2 () {System.out.println (" method of obtaining Bean2 for static factory ..... "); return new Bean2 (); // What is a static factory? This is where a static method is provided to return an instance of Bean2 }}
Package Cn.itcast.spring3.demo2; /** @author *//The only difference between the instance factory and the static factory is that the method inside it is not static public class bean3factory { public Bean3 getBean3 () { System.out.println (" GetBean3 method of Bean3 instance factory ... "); return New Bean3 (); }}
PackageCn.itcast.spring3.demo2;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** The way the bean is instantiated * *@authorZhongzh **/ Public classSpringTest2 {@Test Public voiddemo1 () {//instantiation of a parameterless construction methodApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Applicationcontext.xml");//Example Chemical FactoryBean1 bean1 = (Bean1) applicationcontext.getbean ("Bean1");//get Bean1 via key configured in config fileSystem.out.println (BEAN1); } @Test Public voidDemo2 () {//instantiation of a static factoryApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Applicationcontext.xml");//Example Chemical FactoryBean2 bean2 = (Bean2) applicationcontext.getbean ("bean2");//get bean2 via key configured in config fileSystem.out.println (bean2); } @Test//Instance Factory instantiation Public voidDemo3 () {//instantiation of a static factoryApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml");//Example Chemical FactoryBean3 bean3 = (Bean3) applicationcontext.getbean ("Bean3");//get bean2 via key configured in config fileSystem.out.println (BEAN3); }}
day38 07-spring Framework Bean when the way