首页 > 代码库 > Spring中用java config简化xml配置
Spring中用java config简化xml配置
个人感觉还是这种方式配置方式最灵活了。
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"); } }
先定义上面3个类。然后我们来测试。
package com.baobaotao.conf; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; @Configurable public class AppConf { // 以下两个方法定义了两个Bean,并提供了Bean的实例化逻辑 @Bean public UserDao userDao() { return new UserDao(); } @Bean public LogDao logDao() { return new LogDao(); } // 定义了logonService的Bean @Bean public LogonService logonService() { LogonService logonService = new LogonService(); // 将上面定义的Bean注入到logonService Bean中 logonService.setLogDao(logDao()); logonService.setUserDao(userDao()); return logonService; } }
测试类。
package com.baobaotao.conf; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ConfigTest { @Test public void test(){ AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConf.class); LogonService logonService = ac.getBean(LogonService.class); logonService.print(); } }
如果bean在多个@Configuration中定义。
package com.baobaotao.conf; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public 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; @Configuration public class ServiceConfig { //想普通Bean一样注入DaoConfig @Autowired private DaoConfig daoConfig; @Bean public LogonService logonService(){ LogonService logonService = new LogonService(); //像普通Bean一样,调用Bean相关的方法 logonService.setLogDao(daoConfig.logDao()); logonService.setUserDao(daoConfig.userDao()); return logonService; } }
因为@Configuration是通过@Component进行元注解的,所以意味着通过@Configuration注解的类,可以被Spring的<context:component-scan>扫描到。故可以使用@Autowired进行自动装配。
xml配置文件
<?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>
测试类。
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(); } }
通过component 扫描之后,使用@Configuration注解的类已经被组装到了xml文件中,所以可以使用应用程序上下文去得到这个bean。
通过configuration配置类引入xml配置文件
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; //通过@ImportResourcce引入XML配置文件 @Configuration @ImportResource("classpath:com/baobaotao/conf/bean2.xml") public class LogonAppConfig { //自动注入XML文件中定义的Bean @Bean @Autowired public LogonService logonService(UserDao userDao, LogDao logDao){ LogonService logonService = new LogonService(); logonService.setUserDao(userDao); logonService.setLogDao(logDao); return logonService; } }
引用的xml配置文件
<?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>
测试类。
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(); } }
Spring中用java config简化xml配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。