Spring assembly in three different ways. injection set, and automatic assembly constructor injection

 

A, Spring injection of a first embodiment, this tag is used with the property set methods for implanting Bean class attributes, as follows:

To test I created a maven project, and introduces the following dependencies in pom.xml :( do not need to log system)

 

 1     <dependencies>
 2         <!--springframewok模板-->
 3         <dependency>
 4             <groupId>org.springframework</groupId>
 5             <artifactId>spring-context</artifactId>
 6             <version>4.3.13.RELEASE</version>
 7         </dependency>
 8         <!--日志系统-->
 9         <dependency>
10             <groupId>log4j</groupId>
11             <artifactId>log4j</artifactId>
12             <version>1.2.17</version>
13         </dependency>
14         <!--测试-->
15         <dependency>
16             <groupId>org.springframework</groupId>
17             <artifactId>spring-test</artifactId>
18             <version>4.3.13.RELEASE</version>
19         </dependency>
20         <!--junit单元测试-->
21         <dependency>
22             <groupId>junit</groupId>
23             <artifactId>junit</artifactId>
24             <version>4.12</version>
25         </dependency>
26     </dependencies>

 

In java package to create a package, I created here is com.zm.a, create two classes and a test class a1 a2 class (constructor here do not write it in the constructor injection there will be error) code is as follows

. 1  Package com.zm.a;
 2  
. 3  
. 4  public  class A1 {
 . 5      public A1 () {
 . 6          Super ();
 . 7          System.out.println ( "the constructor with no arguments A1" );
 . 8      }
 . 9  
10      Private A2 A2 ;
 . 11  
12 is    // set method with use of the property tags xml injection assembly
 13 is      public  void setA2 (com.zm.a.a2 A2) {
 14          the this .a2 = A2;
 15      }
 16    
. 17      public  void Play () {
 18 is         System.out.println ( "a1 of the play method" );
 . 19          a2.play ();
 20 is      }
 21 is }
. 1  Package com.zm.a;
 2  
. 3  public  class a2 {
 . 4      public a2 () {
 . 5          Super ();
 . 6          System.out.println ( "constructor with no arguments and a2" );
 . 7      }
 . 8  
. 9      public  void Play ( ) {
 10          System.out.println ( "A2 the play () method" );
 11      }
 12 }

ApplicationContext.xml create a configuration file in the resources package

The following is a set of xml configuration implantation method, in a1 to a2 is injected into, and set constructor injection method using the configuration xml manner:

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        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5         <bean id="a1" class="com.zm.a.a1">
6                 <property name="a2" ref="a2"></property>
7         </bean>
8         <bean id="a2" class="com.zm.a.a2"></bean>
9 </beans>

Finally, we created under test at a com.zm.a java package bag, and creates a test class for unit testing, as follows:

 1 package com.zm.a;
 2 import org.junit.Test;
 3 import org.junit.runner.RunWith;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.test.context.ContextConfiguration;
 6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 7 
 8 @RunWith(SpringJUnit4ClassRunner.class)
 9 @ContextConfiguration("classpath:applicationContext.xml")
10 public class TestA {
11 
12   
13     @Autowired
14     private a1 a1;
15 
16     @Test
17     public void test(){
18         a1.play();
19     }
20 }

 

The second method: using a constructor with constructor-arg implanting, only need to modify xml a1 and a2 as well as the class code as follows

 

. 1  Package com.zm.a;
 2  
. 3  
. 4  public  class A1 {
 . 5      public A1 () {
 . 6          Super ();
 . 7          System.out.println ( "the constructor with no arguments A1" );
 . 8      }
 . 9  
10      Private A2 A2 ;
 . 11  
12 is      // used xml configuration mating the constructor-arg implanting assembly 
13 is      public A1 (A2 com.zm.a.a2) {
 14          the this .a2 = A2;
 15      }
 16  
. 17      public  void Play () {
 18 is         System.out.println ( "a1 of the play method" );
 . 19          a2.play ();
 20 is      }
 21 is }

 

. 1  Package com.zm.a;
 2  
. 3  public  class a2 {
 . 4      public a2 () {
 . 5          Super ();
 . 6          System.out.println ( "constructor with no arguments and a2" );
 . 7      }
 . 8  
. 9      public  void Play ( ) {
 10          System.out.println ( "A2 the play () method" );
 11      }
 12 }
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        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5         <bean id="a1" class="com.zm.a.a1">
6                 <constructor-arg ref="a2"></constructor-arg>
7         </bean>
8         <bean id="a2" class="com.zm.a.a2"></bean>
9 </beans>

The third method, using the annotated automatic injection assembly, as follows:

 1 package com.zm.a;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class a1 {
 8     public a1() {
 9         super();
10         System.out.println("a1的无参构造方法");
11     }
12     @Autowired
13     private a2 a2;
14 
15     public void play(){
16         System.out.println ( "a1 of the play method" );
 . 17          a2.play ();
 18 is      }
 . 19 }
. 1  Package com.zm.a;
 2  
. 3  Import org.springframework.stereotype.Component;
 . 4  
. 5  @Component
 . 6  public  class A2 {
 . 7      public A2 () {
 . 8          Super ();
 . 9          None System.out.println ( "a2 of reference constructor " );
 10      }
 . 11  
12 is      public  void Play () {
 13 is          System.out.println (" Play A2 () method " );
 14      }
 15 }
 1 package com.zm.a;
 2 import config.AppConfig;
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.test.context.ContextConfiguration;
 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 8 
 9 @RunWith(SpringJUnit4ClassRunner.class)
10 @ContextConfiguration(classes = AppConfig.class)
11 public class TestA {
12 
13 
14     @Autowired
15     private a1 a1;
16 
17     @Test
18     public void test(){
19         a1.play();
20     }
21 }

 

 

Since no xml configuration the bean, thus the need for a container class, we create a package in com.zm.config AppConfig class, and then implanted in the assembly assembled by scanning the specified package 
used here is com.zm.a, so com.zm.a specified package
1 package config;
2 
3 import org.springframework.context.annotation.ComponentScan;
4 import org.springframework.context.annotation.Configuration;
5 
6 @Configuration
7 @ComponentScan("com.zm.a")
8 public class AppConfig {
9 }

 

Constructor injection and injection can also be set in xml import p, c namespaces to simplify the code, how to achieve specific, I do not write

Of course, I did not order a simple test using a hierarchical structure, in the actual development, such as the interface implementation class under the dao will be more, this time need to involve

@ Primary @ Qualifier @ Resource These annotations, and these annotations are primarily designated assembly required classes. Even if not read.

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zemengcheng/p/11329278.html