基于注解的aop开发步骤:
1、创建目标接口和目标类(内部有切点)
2、创建切面类(内部有增强方法)
3、将目标类和切面类的对象创建权交给spring
4、在切面类中使用注解配置织入关系
5、在配置文件中开启组件扫描和AOP的自动代理
6、测试
具体操作:
第一步: 在src/main/java目录下,右键com.huanf目录,新建anno目录
第二步: 在anno目录新建MyAspect类,写入如下
xxxxxxxxxx
//切面类,需要使用@Component注解把这个类的创建权交给spring容器。注意下面的通知也叫增强,增强也叫通知
"myAspect") (
//标注。告诉spring这个类是切面类
public class MyAspect {
value="execution(* com.huanf.anno.*.*(..))")//配置前置通知,需要在括号指定切点表达式,'value='可以省略不写 (
public void before_met(){
System.out.println("前置增强 -- 注解方式实现aop");
}
(value="execution(* com.huanf.anno.*.*(..))")//配置后置通知,需要在括号指定切点表达式,'value='可以省略不写
public void afterReturning_met(){System.out.println("后置增强 -- 注解方式实现aop");}
/*@Around(value="execution(* com.huanf.anno.*.*(..))")//配置环绕通知,需要在括号指定切点表达式,'value='可以省略不写
public Object around_met(ProceedingJoinPoint pjp) throws Throwable {//ProceedingJoinPoint表示正在执行的连接点,也就是切点,目的是为了下面能执行切点方法
System.out.println("环绕前增强 -- 注解方式实现aop");
Object proceed = pjp.proceed();//目标方法(也叫切点方法)
System.out.println("环绕后增强 -- 注解方式实现aop");
return proceed;
}*/ //注意环绕增强的效果 = 前置增强+后置增强。由于上面使用了前后置增强,所以这里的环绕增强我注释了,否则冲突报错
(value="execution(* com.huanf.anno.*.*(..))")//配置异常抛出通知,需要在括号指定切点表达式,'value='可以省略不写
public void afterThrowing_met(){
System.out.println("异常抛出后增强 -- 注解方式实现aop");
}
(value="execution(* com.huanf.anno.*.*(..))")//配置最终通知,需要在括号指定切点表达式,'value='可以省略不写
public void after_met(){
System.out.println("最终增强 -- 注解方式实现aop");
}
}
第三步: 在anno目录新建Target类,写入如下
//目标类,需要使用@Component注解把这个类的创建权交给spring容器
"target") (
public class Target implements TargetInterface {
public void save() {
System.out.println("save方法正在启动 -- 注解方式实现aop");
}
}
第四步: 在anno目录新建TargetInterface类,写入如下
public interface TargetInterface {
public void save();
}
第五步: 在src/test/java/com.huanf.test目录下新建AnnoTest类,写入如下
x
//测试AOP动态代理
SpringJUnit4ClassRunner.class) //指定运行引擎是括号里的 (
"classpath:applicationContext-anno.xml") //指定使用的配置文件是src/main/resources/applicationContext-anno.xml (
public class AnnoTest {
//我们要测com.huanf.aop.Target类里面的save方法(调用save方法需要去com.huanf.aop.TargetInterface里面,才有具体的调用),
//所以要引入(注入)applicationContext.xml里面id为target的bean
private TargetInterface target_itf;
//写一个测试方法
public void test1(){
target_itf.save();
}
}
第六步: 在src/main/resources目录下新建applicationContext-anno.xml,写入如下
xxxxxxxxxx
<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/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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.huanf.anno"/>
<!--开启aop自动代理,不添加下面那行的话,com.huanf.anno目录下的MyAspect类里面的增强方法是不会生效的-->
<aop:aspectj-autoproxy/>
</beans>
第七步: 在com.huanf.test目录新建AnnoTest类,写入如下
xxxxxxxxxx
//测试AOP动态代理
SpringJUnit4ClassRunner.class) //指定运行引擎是括号里的 (
"classpath:applicationContext-anno.xml") //指定使用的配置文件是src/main/resources/applicationContext-anno.xml (
public class AnnoTest {
//我们要测com.huanf.anno.Target类里面的save方法(调用save方法需要去com.huanf.anno.TargetInterface里面,才有具体的调用),
//所以要引入(注入)applicationContext.xml里面id为target的bean
private TargetInterface target_itf;
//写一个测试方法
public void test1(){
target_itf.save();
}
}
第八步: 在com.huanf.test目录的AnnoTest类运行测试