Spring Series Assembly Bean

Source: Internet
Author: User

Spring Series Assembly Bean
I. Overview containers are the core of the Spring framework. Spring containers use IOC to manage all components that constitute the application system. Spring has two different containers: BeanFactory provides the simplest container and the most basic dependency Injection Support. ApplicationContext is built on BeanFactory, provides system architecture services such as reading text information from attribute files and passing events. Assembling beans in Spring containers is called assembly. When assembling beans, you are telling the containers which beans are needed and how the containers use dependency injection to combine them. Back to Top 2. Assembly Bean 2.1 uses XML to assemble BeanFactory in the factory design mode. Its implementation class is responsible for creating and distributing various types of beans. Spring has several BeanFactory implementations. For example, org. springframework. beans. factory. xml. XmlBeanFactory loads beans according to the definition in the XML file. (Not recommended now) ClassPathXmlApplicationContext: a context that loads the context definition file FileSystemXmlApplicationContext from the class path: An application context that loads the context file XmlWebApplicationContext from the file system: A Spring-based web Application System context that loads context definition files from the web application context

Public class TestMain {public static void main (String [] args) {// ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); ClassPathResource res = new ClassPathResource (" applicationContext. xml "); BeanFactory factory = new XmlBeanFactory (res); // currently Demo2 t = (Demo2) factory is not recommended. getBean ("demo2"); System. out. println (t. getPrice ());}}

 

2.2 Add a bean factory to read Bean definition information from the XML file, but Bean has not been instantiated yet, and Bean is delayed to be loaded into the Bean factory. Then, call the getBean method, and the factory will instantiate the Bean and use dependency injection to set Bean attributes. A basic BeanFactory configuration consists of one or more Bean definitions managed by it. In an XmlBeanFactory, the root node beans contains one or more elements.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>  <bean id="..." class="...">    ...  </bean>  <bean id="..." class="...">    ...  </bean>  ...</beans>

 

The Bean definition in an XmlBeanFactory includes: classname: it is usually the real implementation class of bean. However, if a bean is created using a static factory method instead of being created by a common constructor, this is the classnamebean behavior configuration element of the factory class: it declares the behavior of the bean in the container, for example, parameters of the automatic assembly mode, dependency check mode, initialization and destructor constructor, and attributes required for creating a bean: for example, the size limit of the pool and other beans related to the bean work: for example, there are two main forms of cooperator direction control/dependency injection: 1. setter-based dependency injection: it is implemented by calling the setter Method on your bean after the factory method of the static method without the parameter constructor or the parameter is invoked to instantiate your bean. Spring generally advocates the use of setter-based dependency injection. The following is the method distance: the Bean construction implementation class is Demo1.java:
Public class Demo1 {private String name; private int age; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Demo1 () {System. out. println ("call a constructor without Parameters ");}}

 

The configuration in applicationContext. xml is:
 <bean id="demo1" class="com.Demo1">        <property name="name">            <value>xujian</value>        </property>            <property name="age">            <value>23</value>        </property>    </bean>

 

Compile the test class:
public class TestMain{    public static void main(String[] args)    {        ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");        Demo1 t=(Demo1) factory.getBean("demo1");                System.out.println(t.getName());    }}

 

The running result is: visible. Through the setter method, the non-argument constructor of the class is executed first, and then the setXXX method is called to set properties. 2. constructor-based dependency injection: It is implemented by calling constructor methods with many parameters. Each parameter represents a partner or attribute. The following uses this method as an example, you must add a constructor with parameters to the bean implementation class.
public class TestMain{    public static void main(String[] args)    {        ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");        Demo2 t=(Demo2) factory.getBean("demo2");            System.out.println(t.getBookName());    }}  

 

The configuration of applicationContext. xml is as follows:
public class TestMain{    public static void main(String[] args)    {        ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");        Demo2 t=(Demo2) factory.getBean("demo2");            System.out.println(t.getBookName());    }}  

 

Compile the test class:
public class TestMain{    public static void main(String[] args)    {        ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");        Demo2 t=(Demo2) factory.getBean("demo2");            System.out.println(t.getBookName());    }}  

 

2.3 The prototype and single-instance Spring are in single-instance mode by default, and the same instance is always returned when containers allocate beans. The test is as follows:
public class TestMain{    public static void main(String[] args)    {        ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");        Demo2 t1=(Demo2) factory.getBean("demo2");            Demo2 t2=(Demo2) factory.getBean("demo2");         System.out.println(t1==t2);    }}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.