1、Spring使用注解是需要在配置文件配置以下内容,指定哪些包下可以使用注解;
<context:component-scan base-package="service.Impl"></context:component-scan>
2、常用注解:
2.1、@Component():创建类对象,相当于标签
2.2、@Service(),相当于@Component(),次注解多用于业务层(Service层)类,主要用于区别,功能相同;
2.3、@Repository()与@Component()功能相同,多用于数据访问层(Dao层),与@Service一致,也是主要用于区别;
2.4、@Controller()与@Component()功能相同,主要用于控制器类;
2.5、@Resource,java中提供的注解,自动注入,默认使用byName方式自动注入,如果没有找到对应名称的对象,使用byType方式,示例代码如下:
package model;
import javax.annotation.Resource;
public class People {
/**
* 引用其他封装实体类对象
* 使用@Resource注解可以不用写get/set方法
*/
@Resource
private User user;
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
2.6、@Autowired注解,Spring注解,默认使用byType方式注入,示例代码如下:
package model;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
/**
* 引用其他封装实体类对象
* 使用@Resource注解可以不用写get/set方法
*/
@Autowired
private User user;
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
2.7、@Value(),获取属性文件.properties中的数据;
2.8、@PonintCut(),定义切点;
2.9、@Aspect(),定义切面类;
2.10、@Before(),前置通知;
2.11、@After(),后置通知,无论切点是否正确执行,该通知都执行;
2.12、@AfterReturning(),后置通知,只有当切点方法正确执行时,该通知方法才会执行;
2.13、@AfterThrowing(),异常通知;
2.14、@Around(),环绕通知。