【问题标题】:Ordering aspects with Spring AOP && MVCSpring AOP && MVC 的排序方面
【发布时间】:2012-01-29 07:48:22
【问题描述】:

我正在尝试将 Spring AOP 与 Spring MVC 控制器一起使用。 我有 3 个方面,并希望按特定顺序进行。为此,我使用了 Ordered 接口并实现了 getOrder 方法:

@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.我尝试将方面移动到同一个包中,它改变了它们的顺序,但我仍然无法控制它。

【问题讨论】:

    标签: java spring spring-mvc spring-aop


    【解决方案1】:

    您不需要实现 Ordered 接口。

    在 Spring 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" />
    

    您需要通过以下方式启用 AOP 代理:

    <aop:aspectj-autoproxy />
    

    否则将不会激活任何建议。

    更新 2:

    我只是对这个问题进行了研究。 @order 注释仅适用于基于 Spring 的代理 AOP(我在示例中使用它)。根据文档,如果您使用编织,则应使用声明优先级选项。

    更新 3

    1. 我在您的代码中没有看到任何建议,只有方面和切入点。
    2. 如果您的 Advice 类是:x.y.z.SystemArchitecture

    那么你需要将其配置为

    <bean id="systemArchitecture" class="x.y.z.SystemArchitecture" /> 
    

    我没有在您的代码中看到它。

    1. “执行 (* com.jajah.StorageManager.HomeController.*(..))”你的目标是什么?你能用文字写出来吗?

    无论如何。请在 facebook 上给我留言,我会向您发送工作示例,该示例完全符合您的要求。

    【讨论】:

    • 实际上我是从@Order 注解开始的,但是当它不起作用时,我尝试实现 Ordered 接口并添加 System.out 以验证代理至少尝试对方面进行排序。它没有用。
    • 那你的AOP配置有问题。请查看我的更新代码,该代码在我的测试环境中运行完美。不要忘记你需要配置 POintcut 和 Advice type(Around, before or after)。
    • 我定义了切入点,并且方面正在工作。问题只是订购。顺便说一句,我对控制器的建议是这样吗?也许你知道一些调试编织的方法?
    • 感谢您的反馈。我用建议的类(控制器)和切入点更新了原始帖子。
    • 请看我的第二次更新。希望它能解决你的问题。我一直在使用 AOP 代理,所以我忘记了第二个选项 :)
    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多