IOC Assembly Bean
(1) The Spring Framework Bean instantiation approach provides three ways to instantiate beans
Constructor instantiation (default parameter-free, most used)
Static factory instantiation
Instance factory
Let's start with the application Context. XML configuration files for these three methods:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 <!-- Bean Three ways of instantiation================================================================================ --> 10 <!-- 2.1 Using a parametric-free constructor --> 11 <bean id="bean1" class="com.study.spring.b_instance.Bean1"></bean> 12 <!-- 2.2 Using static factory method factory-method It is the static method provided by the factory. --> 13 <bean id="bean2" class="com.study.spring.b_instance.Bean2" factory-method="createInstance"></bean> 14 <!-- 2.3 Method of Configuring Instance Chemical Plant --> 15 <bean id="bean3Factory" class="com.study.spring.b_instance.Bean3Factory"></bean> 16 <bean id="bean3" factory-bean="bean3Factory" factory-method="getInstance"></bean> 17 <!-- end.Bean Three ways of instantiation================================================================================ -->
Class Bean1
public class Bean1 { //A parametric constructor system must be provided with default parametric constructors }
Class Bean2
public class Bean2 { private static Bean2 Bean2 = new Bean2(); private Bean2() { } public static Bean2 createInstance() { return Bean2; } }
Class Bean3
public class Bean3 { }
Bean3Factory class
1 public class Bean3Factory { 2 3 private Bean3Factory(){ 4 5 } 6 7 public Bean3 getInstance(){ 8 return new Bean3(); 9 } 10 }
Test class InstanceDemo
1 import org.junit.Test; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class InstanceDemo { 6 7 //Case Chemical Plant Method 8 @Test 9 public void demo3(){ 10 //Load configuration files to create factories 11 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 12 13 Bean3 bean3 =(Bean3) applicationContext.getBean("bean3"); 14 System.out.println(bean3); 15 16 } 17 18 //Static Factory Method 19 @Test 20 public void demo2(){ 21 //Load configuration files to create factories 22 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 23 24 Bean2 bean2 =(Bean2) applicationContext.getBean("bean2"); 25 System.out.println(bean2); 26 27 } 28 //Construction method bean object 29 @Test 30 public void demo1(){ 31 //Load configuration files to create factories 32 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 33 34 Bean1 bean1 =(Bean1) applicationContext.getBean("bean1"); 35 System.out.println(bean1); 36 37 } 38 } 39 /* 40 * All three have memory addresses similar to com.study.spring.b_instance.Bean1@7229c204 41 */
(2) Other configurations of Beans:
In general, when assembling a Bean, the name of the Bean is specified by specifying an id attribute
The id attribute must be unique in the IoC container
To satisfy the XML naming specification for ID attributes, the name of ID must start with letters, using letters, numbers, hyphens, underscores, sentences, colons
If the Bean name contains special characters, you need to use the name attribute such as <Bean name=" person" class="cn.itcast.Bean.Person"/>.
Since the name attribute can be the same, subsequent occurrences of beans override previous occurrences of beans with the same name
The difference between id and name:
id complies with the id constraints of XML constraints. id constraints ensure that the value of this attribute is unique and must start with letters, using letters, numbers, hyphens, underscores, sentences, colons
name does not have these requirements
If no id is configured on the bean tag, name can be used as id.
Bean's scope attribute
<!-- 3.Bean Of scope attribute===================================================================== --> <bean id="product" class="com.study.spring.c_scope.Product" scope="singleton"></bean> <!-- end.Bean Of scope attribute===================================================================== -->
* singleton: singleton. (default value.)
* prototype: Multiple examples.
* request: In web development, an object is created and stored in the request scope, request.setAttribute();
* session: In web development, an object is created and stored in session scope, session.setAttribute();
* globalSession: Generally used in Porlet application environment. It refers to distributed development. It's not a porlet environment. globalSession is equivalent to session.
3. Dependency Injection of Bean Attributes
Now that we know how to get an object, we need to know how to assign attributes to the object.
The following is illustrated by examples:
Class Car
public class Car { private String name; private double price; public Car(String name, double price) { super(); this.name = name; this.price = price; } @Override public String toString() { return "Car [name=" + name + ", price=" + price + "]"; } }