<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<aop:aspectj-autoproxy /> <context:component-scan base-package="wjf.maven">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
</beans>
package wjf.maven; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.PARAMETER })
public @interface DemoAnnotation {
//定义注解里的参数,通过default指定默认值
String desc() default "##########无描述信息##########";
}
package wjf.maven; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; //定义一个切面
@Aspect
public class DemoProcess { @Around(value = "execution(* wjf.maven.*.*(..)) && @annotation(log)")
public Object aroundMethod(ProceedingJoinPoint pjd, DemoAnnotation log ) {
Object result = null;
System.out.println(log.desc());
try {
System.out.println("前置通知");
result = pjd.proceed();
System.out.println("后置通知");
} catch (Throwable e) {
System.out.println("异常通知");
}
System.out.println("返回通知");
return result;
}
}
package wjf.maven; import org.springframework.stereotype.Service; @Service
public class AopTest { @DemoAnnotation(desc = "我是测试")
public void test() {
// System.out.println("hello world!!");
} @DemoAnnotation
public void test2() {
System.out.println("hello world!!");
}
}