Spring可以采用Annotation方式实现AOP,也可以采用配置文件方式实现AOP。本文讲解采用Annotation方式。
当采用Annotation的方式实现AOP时,需要引入除spring以外的以下jar包(请在附件下载):
aspectjlib-1.5.0.jar
aspectjweaver.jar
aopalliance-1.0.jar
面向接口编程:UserManager.java
package com.cos;
public interface UserManager {
public void createUser();
public void readUser();
public void updateUser();
public void deleteUser(int id);
}
实现类:UserManagerImpl.java
package com.cos;
public class UserManagerImpl implements UserManager {
public void createUser() {
System.out.println("-----------------createUser-------------------");
}
public void readUser() {
System.out.println("-----------------readUser-------------------");
}
public void updateUser() {
System.out.println("-----------------updateUser-------------------");
}
public void deleteUser(int id) {
System.out.println("-----------------deleteUser-------------------");
}
}
切面类Aspect:
package com.cos;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/*
* 定义此类是切面类Aspect
*/
@Aspect
public class SecurityHandler{
/*
* 定义Pointcut 切入点 是个表达式 方法名随意,不允许有返回值和参数
* 最后需要把这个Aspect和目标对象都配置在IoC容器里
*/
@Pointcut("execution(* create*(..)) || execution(* update*(..))")
public void addMethod(){
}
/*
* 定义Advice 横切性关注点 此Advice是Before类型的
*/
@Before("addMethod()") //标识在哪个切入点支入、Before Advice
public void checkSecurity(){
System.out.println("------安全性检查------");
}
}
spring 配置文件 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 启用aspectj对Annotation的支持,否则spring不认识Aspect等Annotation -->
<aop:aspectj-autoproxy/>
<!-- 对Aspect的初始化 -->
<bean id="securityHandler" class="com.cos.SecurityHandler"/>
<bean id="userManagerImpl" class="com.cos.UserManagerImpl"/>
</beans>
测试类Client.java:
package com.cos;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)beanFactory.getBean("userManagerImpl");
userManager.createUser();
userManager.updateUser();
userManager.readUser();
}
}
输出结果为:
------安全性检查------
-----------------createUser-------------------
------安全性检查------
-----------------updateUser-------------------
-----------------readUser-------------------
注:
目标对象UserManagerImpl一定要实现一个接口,原因是spring 的AOP用的就是jdk的动态代理机制,而jdk的动态代理机制要求目标对象一定要实现一个接口,这样代理对象也实现同样的这个接口,便完成了代理功能。
分享到:
相关推荐
Spring_Annotation_AOP
NULL 博文链接:https://whp0731.iteye.com/blog/356167
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
NULL 博文链接:https://dyygusi.iteye.com/blog/1995858
spring 的aop的详解如:切面,连接点,通知,切入点,目标对象,代理对象及annotation方式的aop实现和xml方式的事务管理等
spring-aop实践项目,基于annotation方式的面向切面实践小项目
aop入门。
spring-aop-4.0.4.RELEASE.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.1.2.RELEASE.jar ...
NULL 博文链接:https://ysj5125094.iteye.com/blog/2055563
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
自己动手做的一个spring aop 的小demo,主要是annotation标签的配置联系,比较简单.
NULL 博文链接:https://tuoxinquyu.iteye.com/blog/1465187