问题描述
我必须为旧版代码应用Spring AOP,而无需更改现有代码中的任何内容.没有bean的概念,并且使用new关键字创建对象,因此没有使用applicationContext.getBean("beanName")的范围.切入点也将作为最终用户的输入,并且将有一个公共的方面类.
I have to apply Spring AOP for legacy code without changing anything in the existing code. There is no bean concept and the objects are created using new keyword, so no scope of using applicationContext.getBean("beanName"). Also the pointcuts will be taken as input from end user and there will be a common aspect class.
例如.
package package1;
public class Demo1 {
public void method1()
{
System.out.println("From method1");
}
public void method2()
{
System.out.println("From method2");
}
}
package package2;
public class Demo2 {
public void method3()
{
System.out.println("From method3");
}
public void method4()
{
System.out.println("From method4");
}
}
package aspects;
public class SpringAspect {
public void beforeAdvice(JoinPoint jointPoint)
{
System.out.println("Before method : "+jointPoint.getSignature());
}
}
从最终用户那里获取的切入点如下,从方面来讲,它们的before Advisor方法是beforeAdvice():SpringAspect类:执行(* package1.Demo1.method1(..))执行(* package2.Demo2.method4(..))
Pointcuts taken as input from end user are as follows and their before advisor method is beforeAdvice() from aspects.SpringAspect class :execution(* package1.Demo1.method1(..))execution(* package2.Demo2.method4(..))
如果无法使用Spring AOP,我又如何在运行时使用AspectJ.
Also if it is not possible using Spring AOP, how can I use AspectJ during runtime for the same.
推荐答案
首先,您所谓的方面缺少与方面相关的注释,例如@Aspect
和@Before
,没有这些注释,它只是一个POJO类.它永远不会被执行,甚至不会被视为一个方面.
First of all, your so-called aspect is missing aspect-related annotations such as @Aspect
and @Before
, without which it is just a POJO class. It will never be executed or even recognised as an aspect.
第二,Spring AOP是一个基于代理的框架,仅与Spring组件结合使用,而与POJO类无关.您需要AspectJ才能解决您的问题. (顺便说一下,您是否曾经阅读过教程或Spring或AspectJ手册?我想您还没有阅读过.)
Second, Spring AOP is a proxy-based framework which only works in connection with Spring components, not with POJO classes. You need AspectJ in order to solve your problem. (Have you ever read a tutorial or the Spring or AspectJ manual, by the way? I guess you have not.)
此外,为什么要让用户在运行时输入切入点?您是否应该更早地知道在哪些方面应用您的方面?即使我向您展示了一个动态创建方面的解决方案,它们也不是很有价值,因为即使是加载时编织也需要在类加载期间对Java代码进行检测.如果目标类已经加载,则无法在事后将方面应用于它们.
Furthermore, why on earth would you want a user to enter pointcuts during runtime? Should you not know where to apply your aspects a bit earlier? Even if I would show you a solution creating aspects on the fly, they would not be very valuable because even load-time weaving needs to instrument your Java code during class-loading. If the target classes are already loaded, there is no way to apply aspects on them post factum.
也许您应该解释您想要实现的目标,而不是问如何从技术上讲没有意义且行不通的事情,因此无论如何都无法解决您的问题.
Maybe you should explain what you want to achieve and not ask how to do something technically which does not make sense and will not work, thus not solve your problem anyway.
如果您希望以更动态的方式定义切入点,而不是在方面中对其进行硬编码,则可以创建具有抽象切入点的抽象基础方面,并在 aop.xml 文件,该文件用于加载时编织,如 AspectJ文档.
If you want a more dynamic way to define your pointcuts other than hard-coding them within your aspects, you can create an abstract base aspect with an abstract pointcut and define the concrete pointcut in an aop.xml file which is used for load-time weaving, as described in the AspectJ documentation.
我希望这会有所帮助.
这篇关于如何通过将切入点作为用户输入来将spring aop应用于遗留代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!