使用@Configuration_@Bean_@Import简化xml配置
首先建一个Maven项目,导入项目所需要的spring依赖如下:
<dependencies> <!--springcontext--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!--springcore--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!--springbean--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!--springaop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!--springjdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!--springtx--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>3.0.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> </dependencies>
新建UserService,RoleService两个类,如下:
packagecom.lyx; publicclassUserService{ publicvoidaddUser(){ System.out.println("adduser"); } }
packagecom.lyx; publicclassRoleService{ publicvoidaddRole(){ System.out.println("addrole"); } }
使用@Configuration注解
如下:
packagecom.lyx; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; @Configuration publicclassServiceConfig{ @Bean(name="roleService") publicRoleServicegetRoleService(){ returnnewRoleService(); } @Bean(name="userService") publicUserServicegetUserService(){ returnnewUserService(); } }
使用@Import注解
如下:
packagecom.lyx; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.Import; @Configuration @Import(ServiceConfig.class) publicclassAppConfig2{ }
运行该项目
App.java如下,使用AnnotationConfigApplicationContext类加载项目
packagecom.lyx; importorg.springframework.context.ApplicationContext; importorg.springframework.context.annotation.AnnotationConfigApplicationContext; publicclassApp{ publicstaticvoidmain(Stringargss[]){ @SuppressWarnings("resource") ApplicationContextcontext=newAnnotationConfigApplicationContext( AppConfig2.class); RoleServiceroleService=(RoleService)context.getBean("roleService"); roleService.addRole(); UserServiceuserService=(UserService)context.getBean("userService"); userService.addUser(); } }
运行结果:
add role
add user
使用XML的方式配置上面的例子
spring-service.xml配置service,如下:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"> <beanid="roleService"class="com.lyx.RoleService"></bean> <beanid="userService"class="com.lyx.UserService"></bean> </beans>
applicationContext.xml配置app,如下:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"> <importresource="spring-service.xml"/> </beans>
App2.java如下
packagecom.lyx; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassApp2{ publicstaticvoidmain(Stringargs[]){ @SuppressWarnings("resource") ApplicationContextcontext=newClassPathXmlApplicationContext( "applicationContext.xml"); RoleServiceroleService=(RoleService)context.getBean("roleService"); roleService.addRole(); UserServiceuserService=(UserService)context.getBean("userService"); userService.addUser(); } }
====END====
原文链接:https://www.f2er.com/xml/297792.html