In spring, dependency injection is done using Java configuration

Source: Internet
Author: User

Before using spring, we knew only how to use XML, and how to use annotations, but never knew that in spring, you could also use Java classes to configure them. You can replace XML and annotations in a way that uses Java classes. Using the Java configuration is the recommended configuration for the Spring4 version, which has been widely used in spring boot and is the recommended configuration for spring boot.

Today I probably learned how to use the Java configuration to configure spring to use since the injection function.

First we demonstrate the use of annotations to configure spring, and some of the features of annotations are all presented as annotations. Spring Container class chooses Annotationconfigapplicationcontext.

    • Functional Class 1,functionservice
 Packagesite.wangxin520.springstudy.dependency.injection;ImportOrg.springframework.stereotype.Service;//declaring the current class with @service annotations is a bean managed by spring//You can select the @component @Servie @Repository as needed @Controller@Service Public classFunctionservice {/*** Returns a string with a hello in front of the argument string to spell a new string *@paramWord *@return     */     Publicstring Sayhallo (string word) {return"Hello" +word+ "!"; }    }
    • Functional Class 2,usefunctionservice
 package   site.wangxin520.springstudy.dependency.injection;  import   org.springframework.beans.factory.annotation.Autowired;  import   Org.springframework.stereotype.Service; @Service  public  class   Userfuctionservice { //  @Autowired is an automatically injected annotation that injects an instantiated Functionservice class into a property in this class.          @Autowired Functionservice Functionservice;  public   string SayHello (string word) {return   Functionservice.sayhallo (word); }    }
    • Configuration Class Diconfig
 Packagesite.wangxin520.springstudy.dependency.injection;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;/*** Configuration class *@authorWangxgnaw **/@Configuration//declares that the current class is a configuration class@ComponentScan ("Site.wangxin520.springstudy")//equivalent to the configuration in XML to the scan package Public classDiconfig {}
    • Test class
 Packagesite.wangxin520.springstudy.dependency.injection;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext; Public classTest { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Diconfig.class); Userfuctionservice Userfuctionservice= Context.getbean (Userfuctionservice.class); String SayHello= Userfuctionservice.sayhello ("Little boy, Come and play!"));            System.out.println (SayHello); }}
    • Console output is

Fully operational, and in this example, no XML file is configured.

Another way to do this is not to use annotations on a class, but to use Java configuration directly.

Again, it's the example above, but in the new case, @service and @autowired annotations are removed.

    • Functionservice Functional Class 1
 Package site.wangxin520.springstudy.javaconf;  Public class Functionservice {    public  string Sayhallo (string word) {        return ' Hello ' +word+ "!" ;    }}
    • Userfunctionservice Functional Class 2
 Package site.wangxin520.springstudy.javaconf;  Public class Userfuctionservice {    functionservice functionservice;      Public void Setfunctionservice (Functionservice functionservice) {        this. Functionservice=  Functionservice;    }      Public string SayHello (string word) {        return  Functionservice.sayhallo (Word);}    }
    • Javaconfig
 Packagesite.wangxin520.springstudy.javaconf;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration; @Configuration//Note here that no annotations are used for packet scanning Public classJavaconfig {@Bean//using bean Annotations to declare the return value of the current method is a bean, the name is the method name     PublicFunctionservice Functionservice () {return NewFunctionservice (); } @Bean PublicUserfuctionservice Userfuctionservice () {//when using Userfunctionservice, this method is called to get the beanUserfuctionservice userfuctionservice=NewUserfuctionservice ();        Userfuctionservice.setfunctionservice (Functionservice ()); returnUserfuctionservice; } @Bean//This is another way of injecting, directly using the method of parameter injection     Publicuserfuctionservice Userfuctionservice (Functionservice functionservice) {Userfuctionservice userFuction Service=NewUserfuctionservice ();        Userfuctionservice.setfunctionservice (Functionservice ()); returnUserfuctionservice; }    }
    • Test
 Packagesite.wangxin520.springstudy.javaconf;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext; Public classTest { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Javaconfig.class); Userfuctionservice Userfuctionservice= Context.getbean (Userfuctionservice.class); String SayHello= Userfuctionservice.sayhello ("Little boy, Come and play!"));            System.out.println (SayHello); }}
    • Console output

Said, this is not XML convenient, not like.

In spring, dependency injection is done using Java configuration

Related Article

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.