Spring aop拦截问题

发布于 2021-11-29 04:18:36 字数 116 浏览 869 评论 9

我现在有一个A类并且在该类里面有一个方法say(),然后现在有一个B类继承了A但是我没有重写A类中的方法 现在我在测试类里面通过B类实例调用A类的say方法 没有拦截?请问这个问题如何解决  因为项目中有很多这样类似的调用

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

醉酒的小男人 2021-11-30 13:35:36

我在我本地试了一下,没问题的,可以使用

傾城如夢未必闌珊 2021-11-30 13:34:12

@尚浩宇 我在我这测试也是没问题的。。但是在我这个项目里面使用就是不行。其他方法都拦截。就是有个方法不拦截

成熟稳重的好男人 2021-11-30 13:14:02

回复
一般我遇到这种不可能的事,都是去检查配置,是不是粗心哪里弄错了

泛滥成性 2021-11-30 13:13:24

引用来自“尚浩宇”的评论

简单的说,使用cglib,在spring的形式可能是以下这样的,就是在xml里配置,如

  1. <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>  
  2.        
  3.     <bean id="mySecurityManager" class="com.bjsxt.spring.MySecurityManagerImpl"/>  
  4.        
  5.     <aop:config>  
  6.         <aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>  
  7.         <aop:aspect id="securityAspect" ref="mySecurityManager">  
  8.             <aop:before pointcut-ref="allAddMethod" method="checkSecurity"/>  
  9.         </aop:aspect>        
  10.     </aop:config>  

而aspectj则是在代码里的,如
@Aspect

@Component

public class BoyLog {

/**

* <p>Description: </p>

*
@author scc

*
@since 创建时间:2015年11月2日 下午5:08:18

********************************** 

*/

@Pointcut("execution(* *.say(..))")

public void say() {

// TODO Auto-generated constructor stub

}

@Before("say()")

public void before(){

System.out.println("BoyLog.before()");

}

@After("say()")

public void after(){

System.out.println("BoyLog.after()");

}

@AfterReturning("say()")

public void afterreturn(){

System.out.println("BoyLog.afterreturn()");

}

@Around("say()")

public void around(ProceedingJoinPoint joinPoint){

System.out.println("BoyLog.around()");

try {

joinPoint.proceed();

} catch (Throwable e) {

e.printStackTrace();

}

System.out.println("BoyLog.around()");

}

}

私藏温柔 2021-11-30 12:40:13

我是基于注解的

霞映澄塘 2021-11-30 10:00:54

简单的说,使用cglib,在spring的形式可能是以下这样的,就是在xml里配置,如

  1. <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>  
  2.        
  3.     <bean id="mySecurityManager" class="com.bjsxt.spring.MySecurityManagerImpl"/>  
  4.        
  5.     <aop:config>  
  6.         <aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>  
  7.         <aop:aspect id="securityAspect" ref="mySecurityManager">  
  8.             <aop:before pointcut-ref="allAddMethod" method="checkSecurity"/>  
  9.         </aop:aspect>        
  10.     </aop:config>  

而aspectj则是在代码里的,如
@Aspect

@Component

public class BoyLog {

/**

* <p>Description: </p>

*
@author scc

*
@since 创建时间:2015年11月2日 下午5:08:18

********************************** 

*/

@Pointcut("execution(* *.say(..))")

public void say() {

// TODO Auto-generated constructor stub

}

@Before("say()")

public void before(){

System.out.println("BoyLog.before()");

}

@After("say()")

public void after(){

System.out.println("BoyLog.after()");

}

@AfterReturning("say()")

public void afterreturn(){

System.out.println("BoyLog.afterreturn()");

}

@Around("say()")

public void around(ProceedingJoinPoint joinPoint){

System.out.println("BoyLog.around()");

try {

joinPoint.proceed();

} catch (Throwable e) {

e.printStackTrace();

}

System.out.println("BoyLog.around()");

}

}

无声静候 2021-11-30 09:58:28

jdk是基于接口吧,cglib是基于继承吧

泪冰清 2021-11-30 04:36:33

回复
好吧,记错了

私藏温柔 2021-11-29 16:55:14

额,看看你们项目里是用什么方式切面吧,是cglib还是aspectj,cglib是基于接口的,你继承可定会报错,应该使用aspect,这种基于继承的

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文