It is often possible to create and inject beans using the Beans.xml file, but spring provides a simpler approach to convenience-Automatic AssemblyNext we assume a scenario: I have several players (MEDIAPLAYER{CD player/mp3}), and I also have a lot of media files such as (COMPACTDISC{CD disc/mp3 file}). Now we need to create two interface Mediaplayer/compactdisc and then create their implementation cdplayer/compactdisc_zhoujielun. Note: Compactdisc_zhoujielun is Jay's CD-ROM. The code is as follows:
1 Packagecom.spring.cd;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 Importorg.springframework.stereotype.Component;5 6@Component//spring automatically creates beans7 Public classCDPlayerImplementsmediaplayer{8 PrivateCompactdisc cd;9 Ten@Autowired//indicates that spring initializes as much as possible to satisfy the bean's dependency, where it injects a Compactdisc object One PublicCDPlayer (Compactdisc cd) { A This. cd=cd; - } - @Override the Public voidplayer () { -System.out.println ("Wo Yong cd!"); - } -}
Of course, we can also name the bean when it is created, which can be realized in the CDPlayer class. The code is as follows:
Package Com.spring.cd;import org.springframework.stereotype.Component, @Component ("Zhoujielun") public class Compactdisc_zhoujielun implements Compactdisc{private string title= "hair as Snow";p rivate string artist= "Jay Chou"; @Overridepublic void Play () {System.out.println ("Play:" +title+ "from Artist:" +artist);}}
Next, we need to enable component scanning, which is the Java code that defines the assembly rules for spring. The code is as follows:
Package Com.spring.cd;import Org.springframework.context.annotation.componentscan;import org.springframework.context.annotation.Configuration; @ComponentScan ("com.spring.cd")//parameter represents the path that is currently required to be scanned, For null defaults the current package path @configuration ("CD")///The package name that needs to be scanned//the assembly mechanism of spring is defined by Java code public class Cdplayerconfig {}
It is important to note that the real implementation process and the code body is very complex, @Component, @ComponScan, @Autowired, @Comfiguration and other annotations using methods and parameters are diverse.
In order to visualize, we create a new JUNIT4 test. The code is as follows:
Package Com.spring.cd;import static Org.junit.assert.*;import Org.junit.test;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, @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (classes=cdplayerconfig.class) Public class cdplayertest{@Autowiredprivate compactdisc cd; @Autowiredprivate MediaPlayer player, @Testpublic void Test () {Player.player (); Cd.play (); Assertnotnull (CD); Assertnotnull (player);}}
Context creation succeeded.
Love to share refuse take doctrine, blog spirit Forever--cvrigo
2016-11-09 00:19:44
Implicit bean discovery and auto-assembly mechanism