First, related concepts
assembly (Wiring): The act of creating a collaborative relationship between application objects;
Second, the spring configuration of the optional scenario
The spring container is responsible for creating the bean in the application and reconciling the relationships between these objects through DI. How does spring assemble the beans? There are three main ways, as follows:
1 display configuration in XML; 2 use Javaconfig for display configuration in Java; 3 Implicit bean discovery Mechanism and auto-assembly;
Among them, the most recommended way is automatic assembly, but sometimes forced to use the display of the Assembly, the next state to demonstrate the three kinds of assembly.
2.1 Automatic assembly of implicit beans
In automatic assembly of an implicit bean, Spring automatically assembles from two angles:
1. Component Scan (component scanning): Spring automatically discovers the beans created in the application context, 2, automatic Assembly (autowiring): Spring automatically satisfies the dependencies between the beans.
component Scanning and automated assembly combine to give you the power to minimize display configurations.
In automatic assembly, the following components are often used:
1. @Component: This comment indicates that the class will act as a component class and tell spring to create a bean for this class. 2, @Configuration: 3, @ComponentScan (): This annotation indicates that the components in the package are automatically scanned, where basepackageclasses={} or basepackages={} two are filled in parentheses, The Baskpackageclass array contains classes in which the package is used as the base package for component scanning, and the Basepackages array contains the base package to be scanned as a component; 4, @contextConfiguration () : This annotation indicates which file needs to be loaded as a configuration file, 5, @AutoWired: This annotation indicates automatic loading;
Demo code, the main feature is CD player playing CD:
CD Interface class: Package Soundsystem;public interface Compactdisc {void play ();}
Define one of the official CD classes:
@Component annotations indicate that this is a component that can be used as a container for bean loading; */@Componentpublic class Sgtpeppers implements Compactdisc{private string title = "Sgt.pepper ' s Lonely Hearts Club Band";p rivate String artist = "The Beatles";p ublic Void Play () {System. Out.println ("Playing" + title + "by" + Artist);}}
Define a CD Player:
@Autowired Use the constructor to automatically load, where Compactdisc is a CD, specifically in the load to determine * This feels a bit like the Java polymorphic */@Autowiredpublic CDPlayer (Compactdisc CD) {this.cd = cd;} public void Play () {Cd.play ();}}
Javaconfig file:
Package Soundsystem;import Org.springframework.context.annotation.componentscan;import org.springframework.stereotype.component;/* * @Component: Represents a component, no longer says * @ComponentScan: Indicates that all components in the class can be scanned, and add to the spring container to go, Basepackages here is optional, if you do not write to indicate this class is located in the package */@Component @componentscan (basepackages= "Soundsystem") public class Cdplayconfig {}
To test the above assembly:
Package Soundsystem;import Org.junit.rule;import Org.junit.test;import Org.junit.contrib.java.lang.system.systemoutrule;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.springjunit4classrunner;import Junit.framework.Assert; @RunWith ( Springjunit4classrunner.class) @ContextConfiguration (classes=cdplayconfig.class) public class Cdplaytest {@ Rulepublic final Systemoutrule log = new Systemoutrule (); /* * Automatic assembly of COMPACTDISC components */@Autowiredprivate compactdisc cd;/* * Automatic assembly of cdplayer components */@Autowiredprivate CDPlayer player;@ testpublic void Cdshouldnotbenull () {assert.assertnotnull (CD);} @Testpublic void Play () {player.play (); String str = Log.getlog (); Assert.assertequals (str, "Playing sgt.pepper ' s Lonely Hearts Club Band by the Beatles");}
2.2 Assembly with Javaconfig
Pending insertion
2.3 Using XML to assemble a bean
There are two configuration scenarios for assembly using XML:
1, the original <constructor-arg> elements;
2, using the C-command space introduced in Spring3.0 and the P-command space for assembly;
The two in the Assembly have their own advantages and disadvantages, need to know for themselves, here is the main point of use the first way to assemble;
Or use the example above to explain:
CD class:
Package Soundsystem;public class Sgtpeppers implements compactdisc{private String title = "Sgt.pepper ' s Lonely Hearts Club Band ";p rivate String artist = " The Beatles ";p ublic Void Play () {System.out.println (" Playing "+ title +" by "+ Artist );}}
Player class:
Package Soundsystem;public class CDPlayer implements Mediaplayer{private Compactdisc cd;public CDPlayer (compactdisc CD) {this.cd = cd;} public void Play () {Cd.play ();}}
XML configuration information:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPRING-BEANS-2.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP-2.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-2.5.xsd "><!--to load the CD class as a bean into the spring container--><bean id=" Compactdisc "class=" Soundsystem.sgtpeppers "></bean><!--to load the player as a bean into the spring container--><bean id=" CDPlayer "class=" Soundsystem. CDPlayer "><!--1, add <constructor-arg ref=" Compactdisc "using the constructor element ></constructor-arg>--< Constructor-arg ref= "Compactdisc" ></constructor-arg><!--2, how to use the C command space: Add c:cd-ref= "C in the beanOmpactdisc "--></bean></beans>
Test method:
Package Soundsystem;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public static void main (String [] args) {//TODO auto-generated method stub/* * */applicationcontext APC = new Classpathxmlapplicationcontext ("Appli Cationcontext.xml ");//Read the corresponding Bean object from the container cdplayer CDP = (cdplayer) apc.getbean (" CDPlayer "); Cdp.play ();}
"section I" Scenarios for assembling beans