简体   繁体   English

使用Spring / AspectJ进行架构实施

[英]Architectural Enforcement Using Spring/AspectJ

I have a Spring project uses annotations to apply (among other things) caching. 我有一个Spring项目使用注释来应用(除其他外)缓存。

My understanding is that these annotations will only work when @Autowired and called through SpringAOP. 我的理解是这些注释只有在@Autowired并通过SpringAOP调用时才有效。

This means that if a method calls another in the same class, then any annotations on the second method are ignored eg 这意味着如果方法在同一个类中调用另一个,则忽略第二个方法上的任何注释,例如

@Cacheable(...)
public Animal getAnimal(int id) {
    return get(m_url, id);
}

public Cage getCagedAnimal(int id) {
    Animal animal = getAnimal(id);  // This call will not apply @Cacheable
    Cage cagedAnimal = new Cage(animal);
    return cagedAnimal;
}

What I am looking for is a way to enforce against this ie public methods should not be able to call other public methods of the same class. 我正在寻找的是一种强制执行此方法的方法,即公共方法不应该能够调用同一类的其他公共方法。

I tried applying something similar to the approach used here http://www.jayway.com/2010/03/28/architectural-enforcement-with-aid-of-aspectj but it falls short when applying the restriction on the same class. 我尝试应用与此处使用的方法类似的内容http://www.jayway.com/2010/03/28/architectural-enforcement-with-aid-of-aspectj但是在同一类上应用限制时不尽如人意。

It's perfectly doable, AspectJ is very powerful. 这是完全可行的,AspectJ非常强大。 But you seem to be using Spring AOP instead of AspectJ, and Spring AOP is quite limited compared to AspectJ. 但是你似乎使用Spring AOP而不是AspectJ,而且与AspectJ相比,Spring AOP非常有限。 Spring AOP works by creating proxies around your actual bean implementation to implement the AOP features it provides. Spring AOP的工作原理是围绕实际的bean实现创建代理,以实现它提供的AOP功能。 When the proxy gets the call, the advices will be applied and flow control will be passed to the normal bean. 当代理获得调用时,将应用建议并将流控制传递给普通bean。 Should the normal bean (the target of the proxy) invoke another methods on itself, it won't be invoked on the proxy so the AOP part will be bypassed. 如果普通bean(代理的目标)本身调用另一个方法,则不会在代理上调用它,因此将绕过AOP部分。 AspectJ doesn't have that limitation as it's modifying your classes, not just creating proxies around them. AspectJ没有这个限制,因为它正在修改你的类,而不仅仅是围绕它们创建代理。 I would strongly suggest to go with AspectJ instead of Spring AOP. 我强烈建议使用AspectJ而不是Spring AOP。 Spring will work great with AspectJ too. Spring也适用于AspectJ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM