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>com.geyao</groupId><artifactId>spring01geyao</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.2.0.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency></dependencies>
</project>
UserDao类
package com.geyao.demo.dao;public interface UserDao {void add();
}
UserService类
package com.geyao.demo.service;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;public interface UserService {void add();@Configuration@ComponentScanclass Appconfig {}
}
userController
package com.geyao.demo.web;import com.geyao.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component("UserController")
public class UserController {@Autowired@Qualifier("UserServiceNormal")private UserService userService;public void add(){userService.add();}
}
Appconfig
package com.geyao.demo;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan
public class Appconfig {
}
log4j.properties
### 设置###
log4j.rootLogger = INFO,stdout### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.category.org.springframework.beans.factory=DEBUG
userServiceTest
package com.geyao.demo.service;import com.geyao.demo.Appconfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)public class UserServiceTest {//@Autowired//@Qualifier("")@Resource(name="userServiceFestival")private UserService userService;@Testpublic void testAdd(){userService.add();}
}
usercontrollerTest
package com.geyao.demo.web;import com.geyao.demo.Appconfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)public class UserControllerTest {@Autowired@Qualifier("UserController")private UserController userController;@Testpublic void testAdd(){userController.add();}
}
userservicefestival
package com.geyao.demo.web;import com.geyao.demo.Appconfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)public class UserControllerTest {@Autowired@Qualifier("UserController")private UserController userController;@Testpublic void testAdd(){userController.add();}
}
userserviceNormal
package com.geyao.demo.service.com.geyao.demo.service.impl;import com.geyao.demo.dao.UserDao;
import com.geyao.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Componentpublic class UserServiceNormal implements UserService
{@Autowired
private UserDao userDao;public void add(){userDao.add();}
}