订购Spring AOP&& amp; amp; MVC [英] Ordering aspects with Spring AOP && MVC
问题描述
我正在尝试使用Spring AOP和Spring MVC Controller。
我有3个方面,并希望按特定顺序。为了做到这一点,我使用Ordered接口并实现getOrder方法:
I am trying to use Spring AOP with Spring MVC Controller. I have 3 aspects, and want the to be in specific order. In order to do this, I use Ordered interface and implement getOrder method:
@Aspect
@Component
public class LoggingAspect implements Ordered{
public int getOrder() {
System.out.println("Abra");
return 1;
}
推荐课程:
@Component
@Controller
public class HomeController {
切入点:
@Aspect
public class SystemArchitecture {
@Pointcut("execution (* com.jajah.StorageManager.HomeController.*(..))")
public void inHomeController(){}
@Pointcut("execution (* com.jajah.StorageManager.HomeController.*(..))")
public void loggable(){}
@Pointcut("execution (* com.jajah.StorageManager.HomeController.*(..))")
public void authenticated(){}
}
配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<annotation-driven />
<context:annotation-config />
<aop:aspectj-autoproxy proxy-target-class="false"/>
<beans:bean id="AuthenticationAspect" class="com.jajah.CommonAspects.SecurityAspects.OAuthAspect"/>
<beans:bean id="ErrorHandlingAspect" class="com.jajah.StorageManager.Aspects.ErrorHandlingAspect"/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<!-- <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean> -->
<beans:bean name="homeController" class="com.jajah.StorageManager.HomeController">
<beans:constructor-arg>
<beans:ref bean="CloudStorage"/>
</beans:constructor-arg>
<beans:constructor-arg>
<beans:ref bean="ConfigurationContainer"/>
</beans:constructor-arg>
</beans:bean>
<beans:bean id="CloudStorage" name="CloudStorage" class="com.jajah.StorageManager.CloudStorageProxy" scope="singleton">
<beans:constructor-arg>
<beans:ref bean="ConfigurationContainer"/>
</beans:constructor-arg>
</beans:bean>
<beans:bean id ="ConfigurationContainer" class="com.jajah.StorageManager.ConfigurationContainer" scope="singleton"/>
</beans:beans>
getOrder无法解决问题。
我将感谢任何实用的建议,或者如果你没有确切的答案,我将欣赏有关Spring Proxy和编织机制的任何理论知识。
The getOrder doesn't do the trick. I will appreciate any practical advice, or if you don't have the exact answer I will appreciate any theoretical knowledge about the Spring Proxy and the weaving mechanism.
我会根据需要发布任何所需的代码/配置。
感谢您的阅读。
I will post any Code/Configuration required upon demand. Thanks for reading.
更新:
1.我尝试了@Order(1),结果相同。
2.我试图将方面移动到相同的包,它改变了它们的顺序,但我仍然无法控制它。
Update: 1. I tried @Order(1) with same result. 2. I tried to move aspects to same package, it changed their order, but I still couldn't control it.
推荐答案
您不需要实现Ordered接口。
You don't need to implement Ordered interface.
在Spring AOP中,你可以更容易地做事。
In Spring AOP you can do things much easier.
@Aspect
@Order(1)
public class AspectA
{
@Before("............")
public void doit() {}
}
@Aspect
@Order(2)
public class AspectB
{
@Before(".............")
public void doit() {}
}
更新:
@Aspect
@Order(1)
public class SpringAspect {
@Pointcut("within(com.vanilla.service.MyService+)")
public void businessLogicMethods(){}
@Around("businessLogicMethods()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("running Advice #1");
Object output = pjp.proceed();
return output;
}
}
@Aspect
@Order(2)
public class SpringAspect2 {
@Pointcut("within(com.vanilla.service.MyService+)")
public void businessLogicMethods(){}
@Around("businessLogicMethods()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("running Advice #2");
Object output = pjp.proceed();
return output;
}
}
现在应用程序Context Configuration XML:
Now the application Context Configuration XML:
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean id="springAspect" class="com.vanilla.aspect.SpringAspect" />
<bean id="springAspect2" class="com.vanilla.aspect.SpringAspect2" />
您需要启用AOP代理:
You need to enable AOP proxy by:
<aop:aspectj-autoproxy />
否则不会激活任何建议。
otherwise no advice will be activated.
更新2:
我只是对这个问题进行研究。 @order
注释仅适用于基于Spring的代理AOP(我在我的示例中使用)。如果您使用编织,请参阅文档,您应使用声明优先选项。
I just make a research on this issue. @order
annotation works only on Spring's based proxy AOP (Which I'm using in my example). Accoridng to documentation if you are using weaving you should use declare precedence option.
更新3
- 我的代码中没有任何建议,只是方面和切入点。
- 如果您的建议类是:xyzSystemArchitecture
然后您需要将其配置为
<bean id="systemArchitecture" class="x.y.z.SystemArchitecture" />
我在你的代码中没有看到它。
and I don't see it in your code.
- 执行(* com.jajah.StorageManager.HomeController。*(..))你的目标是什么上?你能用文字写吗?
无论如何。请在Facebook上给我留言,我会给你发送一个工作示例,它确切地说明了你要做什么。
Anyway. Please drop me a message on facebook and I'll send you working example which does exactly what are you trying to do.
这篇关于订购Spring AOP&& amp; amp; MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!