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 {}
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); }}
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);} }
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; } }
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); }}
Said, this is not XML convenient, not like.
In spring, dependency injection is done using Java configuration