使用@Configuration_@Bean_@Import简化xml配置
首先建一个Maven项目,导入项目所需要的spring依赖如下:
<dependencies>
<!--spring context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<!--spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<!--spring bean -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<!--spring aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<!--spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<!-- spring tx -->
<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两个类,如下:
package com.lyx;
public class UserService {
public void addUser() {
System.out.println("add user");
}
}
package com.lyx;
public class RoleService {
public void addRole() {
System.out.println("add role");
}
}
使用@Configuration注解
如下:
package com.lyx;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServiceConfig {
@Bean(name = "roleService")
public RoleService getRoleService() {
return new RoleService();
}
@Bean(name = "userService")
public UserService getUserService() {
return new UserService();
}
}
使用@Import注解
如下:
package com.lyx;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(ServiceConfig.class)
public class AppConfig2 {
}
运行该项目
App.java如下,使用AnnotationConfigApplicationContext类加载项目
package com.lyx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String argss[]) {
@SuppressWarnings("resource")
ApplicationContext context = new AnnotationConfigApplicationContext(
AppConfig2.class);
RoleService roleService = (RoleService) context.getBean("roleService");
roleService.addRole();
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
}
}
运行结果:
add role
add user
使用XML的方式配置上面的例子
spring-service.xml配置service,如下:
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="roleService" class="com.lyx.RoleService"></bean>
<bean id="userService" class="com.lyx.UserService"></bean>
</beans>
applicationContext.xml配置app,如下:
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="spring-service.xml" />
</beans>
App2.java如下
package com.lyx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App2 {
public static void main(String args[]) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
RoleService roleService = (RoleService) context.getBean("roleService");
roleService.addRole();
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
}
}
====END====