Spring relive (ii)--spring Javaconfig

Source: Internet
Author: User

1. Since spring 3, the Javaconfig feature has been included in the Spring Core module, which allows the developer to define the bean and in the spring configuration XML file into the Java class. However, it is still permissible to use classic XML to define beans and configurations, and Javaconfig is another alternative solution. Therefore, in the later version of SPRING3, XML mode and javaconfig two kinds of spring configuration methods are supported.

We define it by means of XML:

<beans xmlns= "Http://www.springframework.org/schema/beans"    xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "    xsi:schemalocation=" Http://www.springframework.org/schema/beans    http:   www.springframework.org/schema/beans/spring-beans-3.0.xsd ">     class=" Com.yiibai.hello.impl.HelloWorldImpl ">        </beans>

is equivalent to:

 PackageCom.yiibai.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportCom.yiibai.hello.HelloWorld;ImportCom.yiibai.hello.impl.HelloWorldImpl; @Configuration Public classAppConfig {@Bean (name= "Hellobean")     PublicHelloWorld HelloWorld () {return NewHelloworldimpl (); }    }

2. Small example:

A. A simple bean

 Package com.spring;  Public Interface Helloeorld {    void  Printhelloworld (String msg);}
 Package com.spring;  Public class Implements helloeorld{    @Override    publicvoid  Printhelloworld (String msg) {        System.out.println ("Hello:" + msg);}    }

B.javaconfig Annotations: Use @Configuration annotations to tell Spring that this is the core spring configuration file and define the Bean through @Bean

 Packagecom.spring;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/*Use the @Configuration comment to tell Spring that this is the core spring configuration file and define the Bean through @Bean*/@Configuration Public classtestconfig {@Bean (name= "Helloeorld")     PublicHelloeorld Gethello () {return NewHelloworldimpl (); }}

C. Test the Main method:

 Packagecom.spring;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext; Public classTestmain { Public Static voidMain (string[] args) {ApplicationContext context=NewAnnotationconfigapplicationcontext (Testconfig.class); Helloeorld obj= (Helloeorld) context.getbean ("Helloeorld"); Obj.printhelloworld ("Spring Java Config");}}

[Email protected]

When we use traditional XML to configure spring, multiple configuration files can be loaded as follows:

In a large project structure, Spring bean configuration files are located in different folders for maintenance and modularity. For example, Spring-common.xml in the Common folder, Spring-connection.xml in the Connection folder, spring-Modulea.xml in the Modulea folder and so on. You can load multiple spring bean configuration files in the following code: ApplicationContext context=NewClasspathxmlapplicationcontext (NewString[] {"Spring-common.xml",              "Spring-connection.xml", "Spring-modulea.xml"}); Put all the Spring XML files in the project Classpath. Project-classpath/spring-Common.xml Project-classpath/spring-Connection.xml Project-classpath/spring-Modulea.xml Solution The above approach is a lack of organization and is prone to error, and a better approach should be to organize all the spring bean configuration files into an XML file. For example, create a spring-all-module.xml file, and import the entire Spring bean file as follows: File:spring-all-Module.xml<beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "><ImportResource= "Common/spring-common.xml"/> <ImportResource= "Connection/spring-connection.xml"/> <ImportResource= "Modulea/spring-modulea.xml"/> </beans>now, you can load an XML file like this: ApplicationContext context=NewClasspathxmlapplicationcontext (spring-all-module.xml); Put this file into the project's classpath. Project-classpath/spring-all-module.xml

The above method is equivalent to the following method of Javaconfig:

1. Two simple spring beans. File One:CustomerBo.java PackageCom.yiibai.core; Public classCustomerbo { Public voidprintmsg (String msg) {System.out.println ("Customerbo:" +msg); }}

File Two:SchedulerBo.java PackageCom.yiibai.core; Public classSchedulerbo { Public voidprintmsg (String msg) {System.out.println ("Schedulerbo:" +msg); }}
2the. @Configuration example now declares the Bean class above using the Javaconfig @Configuration. File One:CustomerConfig.java PackageCom.yiibai.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importcom.yiibai.core.CustomerBo; @Configuration Public classcustomerconfig {@Bean (name= "Customer") PublicCustomerbo Customerbo () {return NewCustomerbo (); }}

File Two:SchedulerConfig.java PackageCom.yiibai.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importcom.yiibai.core.SchedulerBo; @Configuration Public classschedulerconfig {@Bean (name= "Scheduler") PublicSchedulerbo Suchedulerbo () {return NewSchedulerbo (); } }3The . @Import example loads multiple configuration files using @import. File:AppConfig.java PackageCom.yiibai.config;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.context.annotation.Import; @Configuration @import ({customerconfig.class, Schedulerconfig.class }) Public classAppConfig {}
4the execution program loads the master configuration file and tests it. PackageCom.yiibai.core;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;ImportCom.yiibai.config.AppConfig; Public classApp { Public Static voidMain (string[] args) {ApplicationContext context=NewAnnotationconfigapplicationcontext (AppConfig.class); Customerbo Customer= (Customerbo) Context.getbean ("Customer")); Customer.printmsg ("Hello 11"); Schedulerbo Scheduler= (Schedulerbo) context.getbean ("Scheduler"); Scheduler.printmsg ("Hello 22"); }} output result Customerbo:hello11Schedulerbo:hello22

Summary: Spring has XML and javaconfig two configuration methods, they are different, but the same effect, can be configured according to their own needs of selective configuration.

Spring relive (ii)--spring Javaconfig

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.