First, @ComponentScan
1.
@Configuration // Description This class is a profile // Open Scan that scans the current class of packages and their child packages publicclass cdplayerconfig {}
2.
@ComponentScan (basepackages={"Soundsystem", "video"})// scan multiple packages publicclass cdplayerconfig {}
3.
@ComponentScan (Basepackageclasses={cdplayer. Class, AAA. class}) // Specify the classes to scan Public class Cdplayerconfig {}
Second, @Autowired
1. Can be used in the construction method
@Component Public class Implements MediaPlayer { private compactdisc cd; @Autowired //@Injectpublic cdplayer (compactdisc cd) { this. cd = cd; } Public void Play () { cd.play (); }}
2. In the Set method
@Autowired Public void Setcompactdisc (Compactdisc cd) {this. cd = cd;}
3. In the General method
@Autowired Public void Insertdisc (Compactdisc cd) {this. cd = cd;}
4. If the dependency is not required, you can set the property
@Autowired (required=false) public cdplayer (compactdisc CD) {this . cd = cd;}
5. @inject Replacement Available
1 PackageSoundsystem;2 ImportJavax.inject.Inject;3 Importjavax.inject.Named;4 @Named5 Public classCDPlayer {6 ...7 @Inject8 PublicCDPlayer (Compactdisc cd) {9 This. cd =cd;Ten } One ... A}
SPRING in ACTION 4th Note-Chapter II [ Email protected] , the use of @Autowired