由于单独的Aop需要在Xml中配置好多,在Spring中,Spring提供了接口来拦截方法,这使得更简便
下面是例子:
beans.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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-3.0.xsd"
default-lazy-init="false" default-autowire="no">
<bean id="around" class="org.hzy.advices.AroundAdvice"></bean>
<bean id="before" class="org.hzy.advices.BeforeAdvice"></bean>
<bean id="after" class="org.hzy.advices.AfterAdvice"></bean>
<bean id="student" class="org.hzy.dao.impl.StudentImpl"></bean>
<aop:config>
<aop:pointcut id="target" expression="execution(* org.hzy.dao.impl.StudentImpl.addStudent(..))"/>
<!-- <aop:advisor id="be" advice-ref="before" pointcut-ref="target"/> -->
<!-- <aop:advisor id="af" advice-ref="after" pointcut-ref="target"/> -->
<aop:advisor id="ar" advice-ref="around" pointcut-ref="target"/><!-- 不是不需要切面,而是它已经包含了切面 -->
</aop:config>
</beans>
AroundAdvice.java
package org.hzy.advices;
import java.lang.reflect.Method;
import org.aopalliance.intercept.Invocation;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor{//相当于环绕通知
public Object invoke(MethodInvocation arg0) throws Throwable {//arg0相当于struts2的拦截器的调用者actionInvocation
// TODO Auto-generated method stub
System.out.println("进入拦截方法!!!!!!!!");
Method me=arg0.getMethod();
System.out.println(me.getName());
System.out.println(arg0.getArguments()[0]);
Object result=null;
String stu_name=arg0.getArguments()[0].toString();
if("hzy".equals(stu_name)){
result= arg0.proceed();
}else{
System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
}
return result;
}
}
BeforeAdvice.java
package org.hzy.advices;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice{//相当于前置通知
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println( " 这是BeforeAdvice类的before方法. " );
// args[0]="hzy";
System.out.println(args[0]);
}
}
AfterAdvice.java
package org.hzy.advices;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice{//相当于后置通知
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("这是AfterAdvice类的afterReturning方法.");
System.out.println(target.getClass());
}
}
IStudent.java
package org.hzy.dao;
public interface IStudent {
public void addStudent(String name);
}
StudentImpl.java
package org.hzy.dao.impl;
import org.hzy.dao.IStudent;
public class StudentImpl implements IStudent {
public void addStudent(String name) {
// TODO Auto-generated method stub
System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );
}
}
package org.hzy.test;
import org.hzy.dao.IStudent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest2 {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans1.xml");
IStudent st=(IStudent) ctx.getBean("student");
st.addStudent("aa");
}
}
注意:这只是Spring中的模仿Aop,你也可以配置Aop来操作
分享到:
相关推荐
顾名思义,Before Advice会在目标对象的方法执行之前被调用,您可以通过实现org.springframework.aop.MethodBeforeAdvice接口来实现Before Advice的逻辑,接口定义如下: java 代码 1. package org.springframework....
顾名思义,Before Advice会在目标对象的方法执行之前被调用,您可以通过实现org.springframework.aop.MethodBeforeAdvice接口来实现Before Advice的逻辑,接口定义如下: java 代码 1. package org.spring...
网上找了很多例子多数都不能运行。包括after,before,round,throw切入,可对类直接拦截,不需要定义接口文件,自己研究了2天整理出来的。 Spring AOP 的完整例子,可直接运行。带jar包。
工程介绍:SpringBoot项目脚手架,利用spring aop+java反射实现自定义注解校验参数 源码里有使用都例子在DemoContorller example1:校验userName参数必填 @CheckParams(notNull = true) private String userName;...
传统的程序开发也是如此,在一个对象中,如果要使用另外的对象,就必须得到它(自己new一个,或者从JNDI中查询一个),使用完之后还要将对象销毁(比Connection等),对象始终会和其他的接口或类藕合起来。
第6章:我们从Spring AOP的底层实现技术入手,一步步深入到Spring AOP的内核中,分析它的底层结构和具体实现。 第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、...
目录: Spring教程 1 ...Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22 注:后面的内容我将不再完善,但网上的朋友可以帮助完善,只需注明住处即可。
作者:钱安川(Moxie) 注:后面的内容我将不再完善,但网上的朋友可以帮助完善,只需注明住处即可。...Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22
为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: ...5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量
springboot maven 项目,导入如 idea 开发工具,可快速...springboot 2.7.16, 使用了 spring-boot-starter-aop。 面向接口的编程方式,因为比较简单没有做细分包,引用可自行修改。 如运行遇到任何问题,欢迎留言。
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...
为了简单起见,例子没有没有使用任何第三方的AOP Framework, 而是利用Java语言本身自带的动态代理功能来实现AOP. 让我们先回到AOP本身,AOP主要应用于日志记录,性能统计,安全控制,事务处理等方面。它的主要...
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor ...
2 一个简单的基于@AspectJ的AOP例子 2.1 创建Performance接口 创建Performance(表演、演出)接口,接口包含一个方法perform(),包名为aop(本章的类均放在aop包中)。 2.2 创建Performance接口的实现类Ballet 创建...
4.3.6.IFactoryObject接口的其它实现 4.3.6.1.Log4Net 4.3.7.使用depends-on 4.3.8.自动装配协作对象 4.3.9.检查依赖项 4.4.类型转换 4.4.1.枚举类型的转换 4.4.2.内置的类型转换器 4.4.3.自定义类型转换器 4.4.3.1....
该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis(redis缓存)、quartz(集群任务实现)、shiro(权限管理)、oauth2(四种认证模式)、shign(接口参数防篡改重放)、encoder(用户...
SpringAOP概念拓展: 73 之前实现了目标方法的动态调用,现在来实现切面的动态调用。 74 4.2.2 AOP实现的两种模式 78 4.2.2.1 xml形式 78 XML形式拓展: 81 异常通知处理例子: 91 不用spring异常通知,另一种处理...
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...