Spring AOP方面在单独的模块中使用 [英] Spring AOP aspect used in a separate module
问题描述
我在一个maven项目模块中有一个方面com.x.NiceAspect,在一个单独的maven模块中有一个类com.x.NiceClass.这些模块具有相同的POM父级,一起创建了一个项目.
I have an aspect com.x.NiceAspect in one maven project module, and a class com.x.NiceClass in a separate maven module. Those modules have the same POM parent, together creating one project.
我要实现的目的是拥有一个通用的方面,可以将其包含在该项目的所有maven模块中.
The thing I am trying to achieve is to have a universal aspect that I can include in all my maven modules in this project.
现在NiceAspect中的切入点是 execution(* com.x .. .set (..)),这意味着我的软件包中的所有setter方法.
Now the pointcut in the NiceAspect is i.e. execution(* com.x...set(..)) meaning all setter methods in mine package.
我希望此方面与第二个模块一起运行,尤其是加入NiceClass
I want this aspect to run with the second module, especially to join NiceClass
我该如何实现?
此外,假设Aspect想要一个在第二个模块中定义的类的参数,那么我最终得到一个循环依赖项.
Moreover suppose that the Aspect wants an argument of a class that's defined in that second module, then I end up with a circular dependency..
任何帮助表示赞赏
亲切的问候,
x.
推荐答案
-
我希望此方面与第二个模块一起运行,尤其是加入NiceClass
如果切入点与另一个"模块中的方法匹配,则该方面将适用.除了正确设置依赖项之外,您无需执行其他任何特殊操作.例如,将AOP配置全局导入.
If the pointcut matches a method from "another" module, the aspect will apply. You need to do nothing special, besides setting your dependencies correctly. e.g have your AOP configuration globally imported.
此外,假设Aspect希望在第二个模块中定义的类的参数,那么我最终将得到一个循环依赖项.
对class参数的方面依赖关系不应导致循环依赖关系,因为您有一个将两者连接的父POM,并且在运行时,两个模块都在类路径中.
The aspect dependency on the class argument should not result in circular dependency, since you have a parent POM that connects the two, and at runtime both of the modules are in a classpath.
In general, it is recommended to follow Aspect structure as described by Spring documentation => create an overall SystemArchitecture aspect, and then add more narrowed scope aspects as needed:
@Aspect public class SystemArchitecture { /** * A join point is in the web layer if the method is defined * in a type in the com.xyz.someapp.web package or any sub-package * under that. */ @Pointcut("within(com.xyz.someapp.web..*)") public void inWebLayer() {} /** * A join point is in the service layer if the method is defined * in a type in the com.xyz.someapp.service package or any sub-package * under that. */ @Pointcut("within(com.xyz.someapp.service..*)") public void inServiceLayer() {} /** * A join point is in the data access layer if the method is defined * in a type in the com.xyz.someapp.dao package or any sub-package * under that. */ @Pointcut("within(com.xyz.someapp.dao..*)") public void inDataAccessLayer() {} /** * A business service is the execution of any method defined on a service * interface. This definition assumes that interfaces are placed in the * "service" package, and that implementation types are in sub-packages. * * If you group service interfaces by functional area (for example, * in packages com.xyz.someapp.abc.service and com.xyz.def.service) then * the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))" * could be used instead. * * Alternatively, you can write the expression using the 'bean' * PCD, like so "bean(*Service)". (This assumes that you have * named your Spring service beans in a consistent fashion.) */ @Pointcut("execution(* com.xyz.someapp.service.*.*(..))") public void businessService() {} /** * A data access operation is the execution of any method defined on a * dao interface. This definition assumes that interfaces are placed in the * "dao" package, and that implementation types are in sub-packages. */ @Pointcut("execution(* com.xyz.someapp.dao.*.*(..))") public void dataAccessOperation() {} }
这样,您将来可能需要的任何方面都可以重用整个系统体系结构方法,并与它们所应用的实际模块一起生活.
This way any future aspects you may need, can reuse the over all System Architecture approach as well as live together with the actual module they are applied to.
这篇关于Spring AOP方面在单独的模块中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!