SPRING in ACTION 4th Note-Chapter II -002-@ComponentScan, @Autowired usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.