接上次。。。
第一种实现方式:针对于拦截多个包中的某一规则的方法
配置文件:<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-lazy-init="false" default-dependency-check="none" default-autowire="no">
<bean name="advice" class="cn.icbc.service.bs.impl.AopAdvice"/>
<aop:config>
<aop:aspect id="myAspect" ref="advice">
<aop:before pointcut-ref="rmiService" method="before"/>
<aop:after pointcut-ref="rmiService" method="after"/>
<aop:around pointcut-ref="rmiService" method="around "/> </aop:aspect>
</aop:config>
<aop:config>
<aop:pointcut id="rmiService" expression="execution(* cn.icbc.service.bs.impl.*.*(..))" />
</aop:config>
关键拦截类AopAdvice.java:
package cn.icbc.service.bs.impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class AopAdvice
{
public void before() throws Throwable {
System.out.println("spring aop before 权限拦截测试....");
}
public void after() throws Throwable {
System.out.println("spring aop after 权限拦截测试....");
}
public Object around(ProceedingJoinPoint p) throws Throwable {
System.out.println("spring aop around before 权限拦截测试....");
Object o=p.proceed();
System.out.println("spring aop around after 权限拦截测试....");
return o;
}
}
第二种实现方式:拦截某个类下的所有方法
配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-lazy-init="false" default-dependency-check="none" default-autowire="no">
<bean name="advisor" class="cn.icbc.service.bs.impl.AopSingleAdvice" />
<bean id="addProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--属性proxyInterface定义了接口类 -->
<property name="proxyInterfaces" value="cn.icbc.service.bs.iface.IService"></property>
<!-- 属性target指向本地配置的一个bean,这个bean返回一个接口的实现 -->
<property name="target" ref="ServiceImpl"></property>
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
</bean>
拦截类AopSingleAdvice.java实现
package cn.icbc.service.bs.impl;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class AopSingleAdvice implements MethodBeforeAdvice, AfterReturningAdvice,MethodInterceptor
{
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("spring aop afterReturning 权限拦截测试....");
}
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("spring aop before 权限拦截测试....");
}
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("spring aop arount before 权限拦截测试....");
Object o=arg0.proceed();
System.out.println("spring aop arount after 权限拦截测试....");
return o;
}
}
补充说明:
1、两种实现方式的应用场景并不是绝对的,相互间可以通用,对于第二种方式中,可以引入org.springframework.aop.support.NameMatchMethodPointcutAdvisor和
org.springframework.aop.support.RegexpMethodPointcutAdvisor 来应用于跨包的拦截(待下次说明具体配置和实现)
2、例子只是列举了before,after,around的通知方式,其它的于此相似,可以自行举一反三.
3、此两种方式是在spring2.0的基础上实现的
4、第二种方式下,获得bean的名字应该是addProxy。
分享到:
相关推荐
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop demo 通过注解和配置文件的两种实现方式 包含jar包 下载直接运行
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
3、了解Spring中两种动态代理方式的区别; 4、掌握基于XML和注解的AspectJ开发。 二、 实验内容 1、按图所示的类图结构,设计接口及其实现类,并完成另外两附加要求:(1)日志功能:在程序执行期间追踪正在发生的...
sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释。
详细讲解了aop的xml配置和注解的方式配置,和本人的博客想应
主要为大家详细介绍了spring aop两种配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以参考下
spring-aop标签和配置文件两种方式实例,spring要求2.5及以上
Spring AOP 使用的动态代理主要有两种方式:JDK 动态代理和 CGLIB 代理。 JDK 动态代理:用于代理实现了接口的类。Spring 会使用 java.lang.reflect.Proxy 类来创建代理对象。 CGLIB 代理:用于代理没有实现接口的...
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)
有关于Spring,我们最常用的两个功能就是IOC和AOP,前几篇文章从源码级别介绍了Spring容器如何为我们生成bean及bean之间的依赖关系... 确实,Spring也就是通过这两种方式来实现AOP相关功能,下面就通过源码来简单求证下
SpringAop实例,包括xml和配置两种方式.jar齐全
这两种代理方式在Spring AOP中起到关键作用,用于实现横切关注点的切面编程。通过学习它们的原理和实际应用,您将能够更好地理解和利用Spring AOP来提高您的应用程序的可维护性和可扩展性。 内容亮点: JDK动态...
项目中含有一整个springboot实现aop的功能,在拦截的方法形式上有两种一种是通过切点设置为拦截某个包路径下面的类中的所有方法;还有一种是基于某个自定义注解的.
java手动实现、注解方式实现两种方法实现springaop编程,包含源码+jar包+解释
SpringAOP与SpringMVC拦截器两种方式实现权限管控,前台xml读取配置根据登录用户判断button是否enable
此文档介绍了Spring Aop编程的两种方法: 1、基于XML配置方式进行AOP开发。2、基于注解方式进行AOP开发。 文档中有详细的例子。
SpringAop配置事务管理,有两种配置方式。一种直接使用注解的方式,另外一种非注解
NULL 博文链接:https://8366.iteye.com/blog/500500