Personal feeling is the most flexible way to configure.
Package Com.baobaotao.conf;public class Logdao {public void print () {System.out.println ("HelloWorld"); }}
Package Com.baobaotao.conf;public class Userdao {public void print () {System.out.println ("Helloworld"); }}
Package Com.baobaotao.conf;public class Logonservice {public Userdao Userdao; Public Logdao Logdao; public void Setlogdao (Logdao logdao) {This.logdao = Logdao; } public void Setuserdao (Userdao userdao) {This.userdao = Userdao; } public void print () {System.out.println ("HelloWorld"); }}
Define the above 3 classes first. Then we'll test it.
Package com.baobaotao.conf;import org.springframework.beans.factory.annotation.configurable;import org.springframework.context.annotation.Bean; @Configurablepublic class AppConf { // The following two methods define two beans and provide the bean instantiation logic @Bean Public userdao userdao () { return new Userdao (); } @Bean public logdao logdao () { return new logdao (); } // defines Logonservice's bean @Bean public logonservice logonservice () { logonservice logonservice = new logonservice (); // The bean defined aboveInjected into the Logonservice bean logonservice.setlogdao (LogDao ()); logonservice.setuserdao (Userdao ()); return logonservice; } }
The Test class.
Package Com.baobaotao.conf;import Org.junit.test;import Org.springframework.context.annotation.annotationconfigapplicationcontext;public class ConfigTest {@Test public voi D Test () {Annotationconfigapplicationcontext ac = new Annotationconfigapplicationcontext (appconf.class); Logonservice Logonservice = Ac.getbean (Logonservice.class); Logonservice.print (); }}
If the bean is defined in more than one @configuration.
package com.baobaotao.conf;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;@ configurationpublic class daoconfig { @Bean Public userdao userdao () { return new Userdao (); } @Bean public logdao logdao () { return new Logdao (); }}
Package com.baobaotao.conf;import org.springframework.beans.factory.annotation.autowired;import org.springframework.context.annotation.Bean; @Configurationpublic class serviceconfig { //like a regular bean. daoconfig @Autowired private daoconfig daoconfig; @Bean public logonservice logonservice () { logonservice logonservice = new logonservice (); //, like a normal bean, invokes the bean-related method logonservice.setlogdao ( Daoconfig.logdao ()); logonservice.setuserdao (DaoConfig.userDao ( )); return logonservice; }}
Because @configuration is annotated by @component, it means that the class that is annotated by @configuration can be scanned by spring's <context:component-scan>. The @autowired can be used for automatic assembly.
XML configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "http// Www.springframework.org/schema/aop " xmlns:tx=" Http://www.springframework.org/schema/tx " xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:p = "http://www.springframework.org/schema/p" xmlns:util= "http://www.springframework.org/ Schema/util " xsi:schemalocation=" http://www.springframework.org/schema/jdbc http:/ /www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http:/ /www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/ beans/spring-beans-3.0.xsd http://www.springframework.org/ schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:component-scan base-package= "com.baobaotao.conf" /></beans>
The Test class.
Package Com.baobaotao.conf;import Org.junit.test;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Configtest {@Test public void Test ( ) {Classpathxmlapplicationcontext AC = new Classpathxmlapplicationcontext ("Com/baobaotao/conf/bean . xml "); Logonservice Logonservice = Ac.getbean (Logonservice.class); Logonservice.print (); }}
After the component scan, the class using the @configuration annotation has been assembled into an XML file, so you can use the application context to get the bean.
Introducing an XML configuration file through a configuration class
Package com.baobaotao.conf;import org.springframework.beans.factory.annotation.autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.configuration;import org.springframework.context.annotation.importresource;// Introduction of XML configuration file @configuration@importresource ("Classpath:com/baobaotao/conf/bean2.xml") via @importresourcce public class logonappconfig { //automatically injects the bean defined in the XML file @Bean @Autowired public logonservice logonservice (Userdao userdao, logdao logdao) { logonservice logonservice = new logonservice (); logonservice.setuserdao (Userdao); Logonservice.setlogdao (Logdao); return logonservice; }}
Referenced XML configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "http// Www.springframework.org/schema/aop " xmlns:tx=" Http://www.springframework.org/schema/tx " xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:p = "http://www.springframework.org/schema/p" xmlns:util= "http://www.springframework.org/ Schema/util " xsi:schemalocation=" http://www.springframework.org/schema/jdbc http:/ /www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http:/ /www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/ beans/spring-beans-3.0.xsd http://www.springframework.org/ schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> < Bean id= "Userdao" class= "Com.baobaotao.conf.UserDao"/> <bean id= " Logdao " class=" Com.baobaotao.conf.LogDao "/></beans>
The Test class.
Package Com.baobaotao.conf;import Org.junit.test;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Configtest {@Test public void Test ( ) {Classpathxmlapplicationcontext AC = new Classpathxmlapplicationcontext ("Com/baobaotao/conf/bean . xml "); Logonservice Logonservice = Ac.getbean (Logonservice.class); Logonservice.print (); }}
Simplifying XML configuration with Java Config in spring