一,声明bean的注解。
(1)@component
这个注解仅仅声明一个组件对象bean,可以作用于任何层次上。
(2)@Repository
这个注解用于DAO持久层,将持久层的类标识为bean,功能与@component相同。
(3)@Service
这个注解用于Service业务逻辑层,将业务逻辑层的类标识为bean,功能与@component相同。
(4)@Controller
这个注解用于Controller控制层,将控制层的类标识为bean,功能与@component相同。
二,注入bean的注解。
(1)@Autowired
这个注解默认按照bean的类型进行自动装配。
(2)@Resource
该注解与@Autowired功能一样,但区别在于这个注解默认按名称进行装配注入,只有当找不到与名称相匹配的bean时才会按照类型装配注入。此外,该注解并不是spring框架下的注解,如果项目找不到该注解可以下载javax.annotation.jar。如果系统不报错但装配的对象为null,有可能是jdk版本不兼容,jdk1.8以上与该注解冲突。
(3)@Qualifer
该注解与@Autowired配合使用,当注解@Autowired需要按照名称装配注入时,与@Qualifer配合使用,bean的实例名称与@Qualifer的参数相同。
三,环境配置。
(1)spring四个核心模块jar包以及spring框架需要依赖的jar包。spring-beans-xxx.jar,spring-core-xxx.jar,spring-context-xxx.jar,spring-expression-xxx.jar,commons-logging-xxx.jar。(xxx为版本号)
(2)在使用注解编程时需要引入spring-aop-xxx.jar。
四,案例。
1.在项目中引入依赖文件。
2.编写dao层。
package annotation.dao;
public interface TestDao {
public void save();
}
package annotation.dao;
import org.springframework.stereotype.Repository;
@Repository("testDaoImpl")//声明持久层bean
/**相当于@Repository,但如果在service层使用@Resource(name="testDaoImpl"),testDaoImpl不能省略。testDaoImpl为IoC容器中的对象名**/
public class TestDaoImpl implements TestDao{
@Override
public void save() {
System.out.println("testDao save");
}
}
3.编写Service层。
package annotation.service;
public interface TestService {
public void save();
}
package annotation.service;
import annotation.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("testServiceImpl")//相当于@Service,声明业务逻辑层bean
public class TestSeviceImpl implements TestService{
//@Resource(name="testDaoImpl")
@Autowired
@Qualifier("testDaoImpl")
/**相当于@Autowired,@Autowired默认按照Bean类型注入**/
private TestDao testDao;
@Override
public void save() {
testDao.save();
System.out.println("testService save");
}
}
3.编写Controller层。
package annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import annotation.service.TestService;
@Controller//声明控制层bean
public class TestController {
@Autowired
private TestService testService;
public void save() {
testService.save();
System.out.println("testController save");
}
}
4.applicationContext.xml配置文件。
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描annotation包及其子包中的注解 -->
<context:component-scan base-package="annotation"/>
</beans>
5.注意事项。
一定要在applicationContext.xml配置文件中配置扫描器,扫描指定包以及其子包中的注解!!!
6.编写测试。
package annotation.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import annotation.controller.TestController;
public class TestAnnotation {
public static void main(String[] args) {
@SuppressWarnings("resource")
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("config/applicationContext.xml");
TestController tt = (TestController)classPathXmlApplicationContext.getBean("testController");
tt.save();
}
}