开始前我们要导入spring需要的jar包;
还需要引入 aspectjweaver.jar 和 cglib.jar 这两个jar包
如果是maven项目的话,在pom.xml 的dependencies节点下添加如下引用即可:
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
现在开始:
首先,我们定义一个工程中要用到的简单实体Bean-- User.java
package org.xmy.ldq.entity;
import java.io.Serializable;
/**
* 测试用实体
* @author LiDuanqiang
*/
public class User implements Serializable{
private static final long serialVersionUID = 5752197085695030514L;
private String id;
private String name;
private String password;
private String address;
public User() {}
public User(String id, String name,String password,String address) {
super();
this.id = id;
this.name = name;
this.password = password;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
接下来定义一个通知对象:UserAdvice.java
package org.xmy.ldq.aop;
import org.aspectj.lang.JoinPoint;
/**
* 用户操作的通知对象
* @author LiDuanqiang
*/
public class UserAdvice {
/**
* 主体方法返回后将执行的通知方法
* @param JoinPoint
* @param retValue主体方法传递到通知方法的返回值
* @return
*/
public Object afterReturning(JoinPoint joinPoint,Object retValue)throws Throwable{
Object object = null;
object = joinPoint.getThis();//返回代理对象
/*切入点主体方法的名字*/
String methodName = joinPoint.getSignature().getName();
if (retValue instanceof Boolean && (Boolean)retValue && methodName.equals("login")) {
System.out.println("方法:"+methodName+"()"+"成功执行;"+"登录成功!");
}
return object;
}
}
再定义一个目标类:AOPBean.java
package org.xmy.ldq;
import org.xmy.ldq.entity.User;
/**
* 主体Bean
* @author LiDuanqiang
*/
public class AOPBean{
public Boolean login(User user){
Boolean ret = false;
/*用户名和密码都为ldq则成功登录*/
if (user.getName().equals("ldq")&&user.getPassword().equals("ldq")) {
ret = true;
}
return ret;
}
}
最后定义一个测试主程序的实现类:App.java
package org.xmy.ldq;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xmy.ldq.entity.User;
/**
* 测试
* @author LiDuanqiang
*/
public class App{
public static void main( String[] args ){
ClassPathXmlApplicationContext factory =
new ClassPathXmlApplicationContext("applicationContext.xml");
AOPBean aopBean = (AOPBean) factory.getBean("aopBean");
aopBean.login(new User("1","ldq","ldq","cs"));
System.exit(0);
}
}
配置文件很重要: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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<aop:config>
<!-- 定义切点 -->
<aop:pointcut id="login_PointcutTarget" expression="execution(* org.xmy.ldq.AOPBean.login(..))"/>
<!-- 定义切面 -->
<aop:aspect id="userAspect" ref="userAdvice">
<aop:after-returning
pointcut-ref="login_PointcutTarget"
arg-names="joinPoint,retValue"
returning="retValue"
method="afterReturning"
/>
</aop:aspect>
</aop:config>
<bean id="userAdvice" class="org.xmy.ldq.aop.UserAdvice"></bean>
<bean id="aopBean" class="org.xmy.ldq.AOPBean"></bean>
</beans>
说明:arg-names="joinPoint,retValue" 被切目标方法的参数名称;
returning="retValue" 目标方法成功执行后的返回值 一般类型定义为 Object;
method="afterReturning"目标方法名称
如果程序中报出
java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
是aspectjweaver.jar包丢失了,将其引入即可;
Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.错误 是因为采用的CGLIB代理,应把相应的jar包引入。
分享到:
相关推荐
用spring aop 编写的权限验证实例代码,下载后将解压后的文件直接导入到eclipse中,直接运行测试方法即可。
一个运用Spring AOP 1.0 的小示例
spring aop 自定义切面示例 aspectj 需要相应的架包支持
spring aop简单例子,入门学习的好资料
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
spring2.5.6 aop简单示例,导入可运行,包括必须jar包
NULL 博文链接:https://fsh430623.iteye.com/blog/1226945
spring Aop的一个示例,没有使用注解,由于jar比较大,没有上传jar包,可以自行下载spring相应的jar包,用于学习spring aop
演示了spring对aop的before、after、throw、around几种advice的api操作。
Spring AOP 常用于日志记录,性能统计,安全控制,事务处理,异常处理等等,本项目对其常用语法进行示例。
演示了spring对aop的支持,包括注解方式、基于xml方式。
关于SpringAOP和java动态代理的示例和讲解,带PPT。
SpringAOP简单入门示例..pdf
aop的原理基于java动态代理模式,本资源是spring的aop运用简单示例,用于帮助初学者理解和运用aop技术
SpringAOP Spring AOP(面向方面的编程)用于模块化“横截面”服务。 用一种简单的方式,我们可以说它是一个旨在拦截某些进程的组件,例如,在执行某个方法时,Spring AOP可以审核该执行方法,并在该方法执行...
SpringAOP的日志拦截示例, 包含配置和代码
AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之。翻译过来就是“面向方面编程”,可我更...最好的演示案例,解释spring aop,物超所值,
eclipse的SpringAOP和动态代理的小例子
关于Spring Aop的几个简单示例,从别的网站脱下来的网页
自己总结的学Spring AOP的步骤,以及代码示例。可供刚学AOP的同学们参考。