Spring AOP--Annotation配置
spring对AOP的支持(采用Annotation方式)
1、spring的依赖包配置
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/aspectj/*.jar
2、将横切性关注点模块化,建立SecurityHandler.java
3、采用注解指定SecurityHandler为Aspect
4、采用注解定义Advice和Pointcut
5、启用AspectJ对Annotation的支持,并且将目标类和Aspect类配置到IoC容器中
6、开发客户端
在xml中添加启用Annotation
<aop:aspectj-autoproxy/>
@Aspectpublic class SecurityHandler {/** * 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数 * 该方法就是一个标识,不进行调用 */@Pointcut("execution(* add*(..))")private void addAddMethod(){};/** * 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上 */@Before("addAddMethod()")//@After("addAddMethod()")private void checkSecurity() {System.out.println("-------checkSecurity-------");}}public static void main(String[] args) {BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");UserManager userManager = (UserManager)factory.getBean("userManager");userManager.addUser("张三", "123");}