Java 与Spring AOP&&;MVC
我正在尝试将SpringAOP与SpringMVC控制器一起使用。 我有3个方面,并希望在具体的顺序。为此,我使用Ordered接口并实现getOrder方法:Java 与Spring AOP&&;MVC,java,spring,spring-mvc,spring-aop,Java,Spring,Spring Mvc,Spring Aop,我正在尝试将SpringAOP与SpringMVC控制器一起使用。 我有3个方面,并希望在具体的顺序。为此,我使用Ordered接口并实现getOrder方法: @Aspect @Component public class LoggingAspect implements Ordered{ public int getOrder() { System.out.println("Abra"); return 1; } 建议级别: @Component @Controller public c
@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代理和编织机制的任何理论知识
我将根据需要发布任何代码/配置。
谢谢你的阅读
更新:
1.我尝试了@Order(1),得到了同样的结果。
2.我试图将方面移动到同一个包中,它改变了它们的顺序,但我仍然无法控制它。您不需要实现有序接口 在春季AOP中,你可以做得更容易
@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;
}
}
现在,应用程序上下文配置XML:
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean id="springAspect" class="com.vanilla.aspect.SpringAspect" />
<bean id="springAspect2" class="com.vanilla.aspect.SpringAspect2" />
我在你的代码里看不到
无论如何。请在facebook上给我留言,我会给你们发送一个工作示例,它正好说明你们想做什么 实际上,我从@Order annotation开始,但当它不起作用时,我尝试实现Ordered接口并添加System.out以验证代理是否至少尝试对方面进行排序。它不起作用。那么你的AOP配置就有问题了。请参阅我的更新代码,它在我的测试环境中运行得非常完美。不要忘记,您需要配置切入点和建议类型(大约、之前或之后)。我定义了切入点,方面正在工作。问题在于订货。顺便问一下,我对管制员有什么建议,会是这样吗?也许你知道一些调试编织的方法?谢谢你的反馈。我用adviced类(控制器)和切入点更新了原始帖子。请参阅我的第二次更新。希望它能解决你的问题。我总是使用AOP代理,所以我忘记了第二个选项:)
<aop:aspectj-autoproxy />
<bean id="systemArchitecture" class="x.y.z.SystemArchitecture" />