KESO
KESO

Reputation: 335

Struts 1 and Spring AOP

I have an application that was made using Struts 1 and it requires some auditory, so what I want is to use aspects, so I don't need to change classes but "intercept" the call to the methods.

What I'm trying to do is using Spring AOP + Aspectj integration, but first of all I need to integrate Struts and Spring. I have been trying with several tutorials and similar questions here, but apparently they are meant mostly for Struts 2.

To be more specific, I'm doing this:

web.xml

...
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...

applicationContext.xml

<context:component-scan base-package="com.app"/>
<context:annotation-config/>
<aop:aspectj-autoproxy />

<bean id="loggerAspect" class="com.app.util.LoggerAspect"/>

LoggerAspect.java

@Aspect
public class LoggerAspect {

    @Before("execution(* com.app.action.FileAction.list(..))")
    public void test(JoinPoint joinPoint) {
        System.out.println("Calling: " + joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName());
    }
}

pom.xml

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--  -->
    <!-- Spring AOP -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.13</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>

I did nothing with the struts-config.xml file. The server load the application without any problem, but Spring is not loading and so the aspect.

Any idea of how to solve this?

Upvotes: 0

Views: 801

Answers (1)

kriegaex
kriegaex

Reputation: 67317

If you want to apply AOP to a legacy, non-Spring application you do not introduce Spring just because of Spring AOP. You directly use AspectJ and have several options here:

  • Compile the original application with the AspectJ compiler if you control the build of the original application, and if you don't:
  • Post-compile time binary weaving creates AOP-enhanced copies of your original class files. Re-Package them and use them instead of the unwoven originals.
  • Load-time weaving: A Java agent does the weaving during class-loading.

AspectJ is much more powerful than Spring AOP, faster (no proxies) and completely independent of Spring. That is what you want to use.

Upvotes: 1

Related Questions