上一篇稀里糊涂的弄了一下基于xml的例子
这一篇就稀里糊涂的弄一下基于注解的吧…
首先 还是写一个接口 和之前的一样
package aop;
public interface count{
public int jiafa(int a,int b);
public int jianfa(int a,int b);
}
实现类
package aop;
public class c1 implements count {
private void sysoo() {
System.out.println("构造器");
}
public int jiafa(int a, int b) {
return a+b;
}
public int jianfa(int a, int b) {
return a-b;
}
}
再然后就是切面类
这里就和xml不太一样了 啥不太一样的呢 就是多了注解呗
package aop;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect//声明这是一个切面
@Component//声明这是一个组件,泛指...可以去掉
public class c3 {
//切面类
@Pointcut(value="execution(* aop.c1.*(..))")
public void qierudian(){}//定义一个切入点 后面的通知直接引入切入点方法pointCut即可
//前置通知(进入环绕后执行,下一步执行方法)
@Before("qierudian()")
public void before(JoinPoint joinpoint){
//得到方法的名字
String name=joinpoint.getSignature().getName();
//得到方法的参数
List<Object> list=Arrays.asList(joinpoint.getArgs());
System.out.println("before"+name+list);
}
//后置通知
@After(value = "qierudian()")
public void after(JoinPoint joinpoint){
//得到方法的名字
String name=joinpoint.getSignature().getName();
//得到方法的参数
List<Object> list=Arrays.asList(joinpoint.getArgs());
System.out.println("after"+name+list);
}
//返回通知
public void fanhui(JoinPoint joinpoint){
String name=joinpoint.getSignature().getName();
System.out.println("得到方法的返回值"+name);
}
//异常通知
public void yicchang(JoinPoint joinPoint,Exception ex){
String name = joinPoint.getSignature().getName();
System.out.println("The yichang "+name+" ends with "+ ex.toString());
}
//这里加入一个环绕通知
//环绕通知基本可以包含前面的所有通知
@Around(value = "qierudian()")
public Object huanrao(ProceedingJoinPoint joinPoint) throws Throwable{
Object o=null;
String name=joinPoint.getSignature().getName();
List<Object> list=Arrays.asList(joinPoint.getArgs());
//前置
System.out.println("环绕前置"+name+list);
o=joinPoint.proceed();
//后置
System.out.println("环绕后置"+name+list);
//返回通知
System.out.println("The aroundMethod "+ name +" ends with "+ o);
return o;
}
}
```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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="pojo"></context:component-scan>
<context:component-scan base-package="test"></context:component-scan>
<context:component-scan base-package="aop"></context:component-scan>
<!-- 使aspectj生效-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="c1" class="aop.c1"></bean>
<!--配置切面类的bean-->
<bean id="c3" class="aop.c3"></bean>
<!--配置aop-->
</beans>`
测试
package aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class tess{
@Test
public void testa()throws Exception{
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{“applicationContext.xml”});
count c1=(count) context.getBean(“c1”);
int result=c1.jiafa(11, 3);
System.out.println(result);
}}
结果:
环绕前置jiafa[11, 3]
beforejiafa[11, 3]
环绕后置jiafa[11, 3]
The aroundMethod jiafa ends with 14
afterjiafa[11, 3]
14
结束.