目录
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.suda</groupId>
<artifactId>springStudy02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
SpringConfig.java配置类
@Configuration
public class SpringConfig {
@Bean(name = "userServiceImpl")
public UserService setUserService(){
return new UserServiceImpl();
}
@Bean(name = "userRoleServiceImpl")
public UserRoleService setUserRoleService(){
return new UserRoleServiceImpl();
}
// @Bean(name = "userInfo")
// public UserInfo setUserInfo(){
// UserInfo userInfo = new UserInfo(setUserService(),setUserRoleService());
// return userInfo;
// }
// @Bean(name = "userInfo")
// public UserInfo setUserInfo(){
// UserInfo userInfo = new UserInfo();
// userInfo.setUserService(setUserService());
// userInfo.setUserRoleService(setUserRoleService());
// return userInfo;
// }
@Bean(name = "userInfo")
public UserInfo setUserInfo(UserService userService,UserRoleService userRoleService){
UserInfo userInfo = new UserInfo();
userInfo.setUserService(userService);
userInfo.setUserRoleService(userRoleService);
return userInfo;
}
}
UserInfo.java类
public class UserInfo {
private UserService userService;
private UserRoleService userRoleService;
public UserInfo(){
}
public UserInfo(UserService userService, UserRoleService userRoleService){
this.userService = userService;
this.userRoleService = userRoleService;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserRoleService getUserRoleService() {
return userRoleService;
}
public void setUserRoleService(UserRoleService userRoleService) {
this.userRoleService = userRoleService;
}
public void getUserInfo(){
System.out.println("UserInfo getUserInfo begin");
System.out.println(userService.getUserInfo());
System.out.println(userRoleService.getUserRoleInfo());
System.out.println("UserInfo getUserInfo end");
}
}
UserService.java接口
public interface UserService {
UserVO getUserInfo();
}
UserServiceImpl.java实现类
@Component
@Qualifier("userServiceImpl")
public class UserServiceImpl implements UserService {
public UserVO getUserInfo() {
UserVO userVO = new UserVO();
userVO.setUserNum("zhangsan");
userVO.setAge("23");
userVO.setUserNum("123456");
userVO.setSex("1");
return userVO;
}
}
UserRoleService.java接口
public interface UserRoleService {
UserRoleVO getUserRoleInfo();
}
userRoleServiceImpl.java实现类
@Component
@Qualifier("userRoleServiceImpl")
public class UserRoleServiceImpl implements UserRoleService {
public UserRoleVO getUserRoleInfo() {
UserRoleVO userRoleVO = new UserRoleVO();
userRoleVO.setUserNum("2345");
userRoleVO.setUserRoleName("客户经理");
userRoleVO.setUserRoleNum("123456");
return userRoleVO;
}
}
UserRoleVO.java实体类
public class UserRoleVO {
private String userRoleName;
private String userRoleNum;
private String userNum;
.....
}
UserVO.java实体类
public class UserVO {
private String userName;
private String age;
private String sex;
private String userNum;
.....
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= SpringConfig.class)
public class userTest {
@Autowired
@Qualifier("userInfo")
private UserInfo userInfo;
@Test
public void getInfo(){
userInfo.getUserInfo();
}
}
使用Spring的SpringJUnit4ClassRunner,以便在测试开始的时候自动创建Spring的应用上下文。注解@ContextConfiguration会告诉它需要在SpringConfig类中加载配置。应为SpringConfig类中包含了@ComponentScan。因此最终的应用上下文中应该包含userServiceImpl,userRoleServiceImpl两个Bean。