Assembly (Wiring): Creates the behavior of collaborative relationships between application objects. This is the essence of dependency injection.
Optional Scenarios for spring configuration
Spring offers three kinds of assembly wit:
1) display assembly in XML
2) display assembly in Java
3) Implicit bean discovery Mechanism and auto-assembly
Automated assembly
Spring implements automated assembly from two angles:
1) Component Scan (component scanning): Spring automatically discovers the bean that is created in the app context.
2) automatic assembly (autowiring): Spring automatically satisfies the dependencies between beans.
Create a bean that can be discovered
Case: Use CD and CD player to realize why there is injection and dependency.
CD: As an interface, defines a series of CD player operations on the CD, while minimizing the coupling of the CD player to the CD itself
Public Interface Compactdisc { void play ();}
The specific implementation of the CD:
Note: @Component. This annotation indicates that the class will act as a component class and tell spring to create a bean for this class.
@Component Public class Implements Compactdisc { private String title = "Sgt. Pepper ' s Lonely Hearts Club Band "; Private String artist = "The Beatles"; Public void Play () { System.out.println ("Playing" + title + "by " + artist);} }
To configure the component scan class:
Note: @ComponentScan, the note will default to scan all classes with @component under the package of the configuration class and its sub-packages
Use the:<context:component-scan> element if you use XML format
@ComponentScan @configuration Public class Cdplayerconfig {}
Test Class Code:
Note: @ContextConfiguration will tell it to load the configuration in Cdplayerconfig, and Cdplayerconfig contains the annotation @componentscan, All the @component class scans under its package and its sub-packages, and generates beans for it
@ContextConfiguration (classes = cdplayerconfig. Class)publicclass cdplayertest { @Autowired Private Compactdisc Compactdisc; @Test publicvoid Test () { compactdisc.play (); }}
Name The bean scanned by the component
All beans in the Spring app context will be given an ID. The default is the lowercase class name.
To change this ID: Just modify the annotations to
@Component ("Myselfbean")publicclassimplements Compactdisc {... }
set up the base package for component scanning
We do not currently have any properties set for @componentscan, and the component is scanned by default in the package that contains the configuration class.
You can scan the package name with the following settings:
@Configuration @componentscan ("com") Public class Cdplayerconfig {}
If you want to specify the base package:
= "com")publicclass cdplayerconfig {}
You can also scan multiple base packages:
= {"com", "cn"})publicclass cdplayerconfig {}
Also provides another way to designate a class or interface that is contained in a package:
Note: You can create a component class specifically for scanning in packages that you must scan.
= {Cdplayerconfig. class, Test.class})publicclass cdplayerconfig {}
automate Assembly by adding annotations to beans
Automatic assembly: Spring automatically satisfies bean dependencies in one way. Automatic assembly can be achieved with @autowired.
@Component Public class Implements mediaplayer{ @Autowired private compactdisc compactdisc; Public void Play () { compactdisc.play (); }}
assembling beans through Java codeCreate a configuration class
Note: @Configuration indicates that the class is a configuration class that should contain details of how the bean is created in the spring application context.
@Configuration Public class Cdplayerconfig {}
declaring a simple bean
Note: @Bean will tell Spring that the object returned is the bean for the spring application context. The Name property of the @Bean can be set by default and the name is the same as the method name.
@Configuration Public class cdplayerconfig { @Bean public compactdisc getcompactdisc () { return New sgtpeppers (); }}
The XML format is not a record
Spring basic knowledge of assembly bean