AOP概念
通知(Advice)类型
• 前置通知(Before advice)MethodBeforeAdvice:
• 在某连接点之前执行的通知
• 后置通知(After returning advice) AfterReturningAdvice:
• 在某连接点正常完成后执行的通知
• 异常通知(After throwing advice) ThrowsAdvice:
• 在方法抛出异常退出时执行的通知
• 最终通知(After finally advice):
• 当某连接点退出的时候执行的通知(不管是否有异常)
• 环绕通知(Around advice) MethodInterceptor:
• 包围一个连接点的通知,这是最强大的一种通知类型
Spring AOP介绍
• Spring AOP是由纯Java语言实现
• Spring AOP和其他AOP框架不同,目的并不是提供最完整的AOP,目前仅支持方法执行上的连接点;但是它和Spring的IoC集成紧密,可以帮助解决企业应用中的常见问题
• 当Spring AOP不能满足我们的需求时,可以采用其他AOP框架,比如AspectJ,Spring框架可以很好的集成诸如AspectJ等框架,他们是互补的
Spring AOP提供了3种实现方式:
• 实现SpringAPI的传统方式
• 纯POJO类(Advice不用实现任何接口)
• 一种基于Schema的XML配置,用标签
• 一种基于注解驱动的切面
• 使用AspectJ切面
一、pringAPI的传统方式(Schema-based)
• 经典的基于代理的AOP,适用于所有版本
• 实现步骤:
• 编写通知
• 编写切点
• Spring自动创建代理对象
二、纯粹POJO方式-基于Xml
• Spring2.x以后提供了新的aop的命名空间来定义切面等内容
• 通知不需要实现特定的接口
• 切面都集中在xml文件中,更加集中、清晰
三、纯粹POJO方式-基于Annotation
• @Aspect 是一种使用普通Java类注解
来声明AOP切面的方式
• 开启AspectJ支持
• 开启Java的配置方式:@EnableAspectJAutoProxy
开启Xml的配置方式:<aop:aspectj-autoproxy />
使用到的注解:
@Aspect:配置类为切面
@Pointcut:配置切入点表达式
@Before:配置前置通知
@AfterReturning:配置后置通知
@AfterThrowing:配置异常通知
@After:配置最终通知
@Around:配置环绕通知
Demo
util
public class TestSpring {
public static void main(String[] args) {
//SpringAOP动态代理
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
Student student=(Student) ac.getBean("student");
System.out.println(student.toString());
StudentService ss=(StudentService)ac.getBean("stdservice");
ss.saveStudent(1);
}
JAVABean(这里演示自动注入)
//配置使用注解实现自动注入
@Component("student")
public class Student {
@Value("14")
private int id;
@Value("lwz")
private String name;
@Value("21")
private String age;
@Value("南宁")
private String address;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, String age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "{\"id\":\"" + id + "\", \"name\":\"" + name + "\", \"age\":\"" + age + "\", \"address\":\"" + address
+ "\"}";
}
}
service
public interface StudentService {
void saveStudent(int id);
void deleteStudentById(int id);
}
serviceImpl
@Service("stdservice")
public class StudentServiceImpl implements StudentService{
@Override
public void saveStudent(int id) {
System.out.println("存储了一个ID为:"+id+"的学生信息");
//int a=1/0;
}
@Override
public void deleteStudentById(int id) {
System.out.println("删除一个ID为:"+id+"的学生信息");
}
}
切面类
@Component("Logger")
@Aspect
public class Logger {
@Pointcut("execution(public void com.lwz.spring.service.impl.StudentServiceImpl.saveStudent())")
private void cut() {
}
//@Before("cut()")
public void before() {
System.out.println("插入前置通知");
}
//@AfterReturning("cut()")
public void afterReturning() {
System.out.println("插入后置通知");
}
//@AfterThrowing("cut()")
public void afterThrowing() {
System.out.println("插入异常通知");
}
//@After("cut()")
public void after() {
System.out.println("插入最终通知");
}
@Around("cut()")//一般使用环绕通知
public Object around(ProceedingJoinPoint pjp) {
Object rtvalue=null;
try {
System.out.println("插入前置通知");//相当于前置通知
rtvalue=pjp.proceed();
System.out.println("插入后置通知");//相当于后置通知
} catch (Throwable e) {
System.out.println("插入异常通知");//相当于异常通知
e.printStackTrace();
}finally {
System.out.println("插入最终通知");//相当于最终通知
}
return rtvalue;
}
}
bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用set方法实现依赖注入 -->
<!-- <bean id="student" class="com.lwz.spring.entity.Student">
<property name="id" value="14"></property>
<property name="name" value="lwz"></property>
<property name="age" value="21"></property>
<property name="address" value="南宁"></property>
</bean> -->
<!-- <bean id="stdservice" class="com.lwz.spring.service.impl.StudentServiceImpl"></bean> -->
<!-- 配置切面类 -->
<!-- <bean id="Logger" class="com.lwz.spring.utils.Logger"></bean> -->
<!-- 使用aop:config开始配置AOP的配置 -->
<!-- <aop:config>
<aop:aspect id="logAdvice" ref="Logger">
<aop:pointcut expression="public void com.lwz.spring.service.impl.StudentServiceImpl.saveStudent()" id="before_aop"/>
<aop:before method="before" pointcut="execution(public void com.lwz.spring.service.impl.StudentServiceImpl.saveStudent())"/>
</aop:aspect>
</aop:config> -->
<!-- 自动注入,扫描带注解的包 -->
<context:component-scan base-package="com.*"></context:component-scan>
<!-- 配置此处使用注解实现 -->
<aop:aspectj-autoproxy/>
</beans>