Java – sort using spring AOP \ u0026 \ u0026 MVC

I am trying to use spring AOP and spring MVC controller

@Aspect
@Component
public class LoggingAspect implements Ordered{

public int getOrder() {
System.out.println("Abra");
return 1;
}

Recommended courses:

@Component
@Controller
public class HomeController {

Entry point:

@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(){}

}

Configuration:

<?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 has no way I will appreciate any practical suggestions, or if you don't have a definite answer, I will know any theoretical knowledge about spring proxy and weaving mechanism

I will release any code / configuration as required Thank you for reading

Update: I tried @ order (1) and the results were the same I tried to move aspects to the same package, and it changed their order, but I still couldn't control it

Solution

You do not need to implement ordered interfaces

In spring AOP, you can do something easily

@Aspect
@Order(1)
public class AspectA
{
  @Before("............")
   public void doit() {}
}

@Aspect
@Order(2)
public class AspectB
{
  @Before(".............")
  public void doit() {}
}

to update:

@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;
     }
}

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

You need to enable the AOP proxy by:

<aop:aspectj-autoproxy />

Otherwise, no suggestions will be activated

Update 2:

I'm just studying this problem@ The order annotation applies only to spring - based proxy AOP (I used it in my example) If you are using weaving, use the declare precedence option

Update 3

>There are no suggestions in my code, just aspects and entry points. > If your advice class is: x.y.z.systemarchitecture

Then you need to configure it to

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

I don't see it in my code

>"Execute (* com. JAJAH. Storagemanager. Homecontroller. * (..)" What is your goal? Can you write in words?

in any case. Please leave me a message on Facebook and I'll send you an example of what you're doing

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>