说明:此方法属于转载
1、配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- action暂未用注解 -->
<bean id="myAction" class="com.demo.annotation.action.MyAction" scope="prototype" />
<!-- 注解测试 -->
<context:component-scan base-package="com.demo.annotation.action" />
</beans>
2、TestDao
public interface TestDao {
/**
* 得到dao层注解
* @return
*/
public String getTestDaoAnnotation();
}
3、testDao实现类
@Repository("testDao")
public class TestDaoImpl implements TestDao{
public String getTestDaoAnnotation() {
// TODO Auto-generated method stub
return "This is testDao Annotation...";
}
}
4、TestService 接口
public interface TestService {
public String getTestAnnotation();
}
5、TestService接口实现
@Service("testService")
public class TestServiceImp implements TestService{
/**
* 自动装配
*/
@Autowired
@Qualifier("testDao")
//@Resource(name="testDao"), 等价于<property ………… ref="testDao" />
private TestDao testDao;
public TestDao getTestDao() {
return testDao;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public String getTestAnnotation() {
return testDao.getTestDaoAnnotation();
}
}
6、测试类 MyAction
@Resource(name="testService")
private TestService testService;
public String testAnnotation(){
if(null == testService){
System.out.println("Service is null!!");
}
String result = testService.getTestAnnotation();
System.out.println(result);
return "success";
}
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
}