Wu Yuxiong - a natural framework for the development of natural JAVA SPRING study notes: Spring XML-based assembly Bean

Bean assembly may be understood as dependency injection, i.e. Bean Bean assembly embodiment of dependency injection. Bean Spring container assembly supports multiple ways, such as XML-based Bean assembly, automatic assembly on assembly and Bean Annotation like. 
Spring XML-based assembly usually implemented in two ways, i.e. the value set injection (Setter Injection) and configured injection (Constructor Injection). 
During instantiation Spring Bean, first invokes the default constructor Bean object instantiation, and then injected property setXxx call () method of the Java reflection mechanism. Thus, a set value of the injection requirement corresponding Bean class must satisfy two requirements. 
You must provide a default constructor with no arguments. 
We must provide the corresponding setter for properties to be injected. 
When using the set value of the injected, Spring configuration file is necessary to use <bean> child element of the element <property> injection element for each attribute value. When using the injection configuration, in the configuration file, the main use <constructor-arg> argument constructor defined tags, attributes whose value can be used (or sub-element) value of the parameter set. The following scenario illustrates the assembly by way of Bean XML-based.
1 . Create Person class 
at the src springDemo02 project directory, create a name for the Person class com.mengma.assembly create a package in the package, as shown below. 
Package com.mengma.assembly;
 public  class the Person {
     Private String name;
     Private  int Age;
     public String getName () {
         return name; 
    } 
    public  void the setName (String name) {
         the this .name = name; 
    } 
    public  int getAge () {
         return Age; 
    } 
    public  void the setAge ( intAge) {
         the this .age = Age; 
    } 
    // override toString () method 
    public String toString () {
         return "the Person [name =" + name + ", Age =" + Age + "]" ; 
    } 
    // Default no-argument constructor 
    public the Person () {
         Super (); 
    } 
    // have reference constructor 
    public the Person (String name, int Age) {
         Super ();
         the this .name = name;
         the this .age = Age; 
    } 
}
The above code, name and age are defined two attributes, and provide a getter and setter methods, due to the use configuration injection, it is necessary to provide with a constructor argument. In order to more clearly see the output result, there is also rewritten toString () method.
2 . Creating a Spring configuration file 
to create a configuration file called applicationContext.xml at com.mengma.assembly 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 "> 
    <-! Person instance injection method using the assembly set value -> 
    <the bean ID =" PERSON1 " class =" com.mengma.assembly.Person " > 
        <Property name = "name"
    <the bean ID = "PERSON2" class = "com.mengma.assembly.Person"> 
        <constructor Arg-index = "0" value = "Lisi" /> 
        <constructor Arg-index = ". 1" value = "21 is" / > 
    </ the bean> 
</ Beans> 
above code first uses examples set value of the injected Person class fitting manner, wherein the <property> child element for invoking Bean instance setXxx () method completes attribute assignment. Then fitting manner a configuration example of the Person class, where <constructor-arg> element is used to define the parameters of the construction method, which is represented by its index attribute index (starting from 0), the attribute value of the set value for the injection.
3 . Create a test class 
to create a test class name XmlBeanAssemblyTest, edited as shown in the following com.mengma.assembly package. 
Package com.mengma.assembly;
 Import org.junit.Test;
 Import org.springframework.context.ApplicationContext;
 Import org.springframework.context.support.ClassPathXmlApplicationContext;
 public  class XmlBeanAssemblyTest { 
    @Test 
    public  void Test () {
         // definition of Spring profile path 
        String XMLPath = "COM / mengma / Assembly / the applicationContext.xml" ;
         // initialize Spring container, load the configuration files, examples of the bean and 
        the applicationContext applicationContext = new newThe ClassPathXmlApplicationContext ( 
                XMLPath); 
        // set the value of the output mode 
        System.out.println (to applicationContext.getBean ( "PERSON1" ));
         // configuration mode output 
        System.out.println (to applicationContext.getBean ( "PERSON2" )); 
    } 
} 
the above code, respectively, and outputs the acquired instance id of the person1 and person2. 
4 . Run the project and view the results 
using the JUnit test after test run () method, run successfully as your console output

 

Guess you like

Origin www.cnblogs.com/tszr/p/12129512.html