0

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?

KESO
  • 335
  • 2
  • 5
  • 17
  • Looking for answers I have found this post https://stackoverflow.com/a/1606780/1950103 where they say: **"Spring-AOP cannot add an aspect to anything that is not created by the Spring factory"**. So what I understand is that I can't use Spring AOP and Struts, since I don't have the classes mapped as beans. Is this true? – KESO Feb 22 '18 at 19:18
  • I have added my class `com.app.action.FileAction` as a bean in the _applicationContext_ but still can't load even Spring – KESO Feb 22 '18 at 19:49

1 Answers1

1

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thanks @kriegaex, the concept of aspects is very handy, but despite the capabilities I consider the implementation of AspectJ by itself a bit impractical. I'm still doing some test and looking for options like filters, so I don't have dependency on third parts – KESO Feb 28 '18 at 20:23
  • Then why on earth did you ask an AspectJ question, making people spend time reading and answering it, if you dismiss the concept as such as "impractical"? As I said, your problem is Spring AOP, introducing a big framework into a legacy application that otherwise does not need it. Use AspectJ and be happy. Nothing about it is impractical if you learn to do it right. I don't know any other way of adding e.g. logging or tracing within 10 mins to any application without changing the source code or introducing a big framework with interceptors. Did you even try? If so, describe your difficulties. – kriegaex Mar 01 '18 at 02:36
  • And as for dependencies: AspectJ is only one JAR, compare that with Spring or any other framework. Do you really want to have vendor lock-in for whichever framework you are thinking about introducing? Is that not a dependency? Your argument is not convincing. – kriegaex Mar 01 '18 at 02:39
  • don't get me wrong, read again because I meant about the configuration, the less complex the better, that's why I went looking for Spring AOP, otherwise this wouldn't be the most common way for working with aspects. I'm still testing, but if I finally go for filters, this is basic on servlets, so look, no more dependencies – KESO Mar 01 '18 at 14:24
  • It is fine if you find on-board means to solve your problem. Still I am failing to see what is complicated with AspectJ. You said you were still testing interceptors. With AspectJ you would have been done in 30 minutes, it is very easy to configure in my experience. Anyway, let me not waste any more time with this question. Good luck to you. – kriegaex Mar 02 '18 at 00:35
  • I meat that I'm still testing with AspectJ, but its compilation and testing is driving crazy, some tutorials use annotations, others *.aj files (I'm using JDeveloper). You are not wasting time if you are good at teaching – KESO Mar 02 '18 at 16:20
  • You might have noticed that I have answered dozens (hundreds?) of AspectJ questions here over the years. I have no idea if I am good at teaching, but I think I could help a few people. I had no idea you were testing with AspectJ because until now you talked about Spring AOP. If you show me your setup in a little [MCVE](http://stackoverflow.com/help/mcve) incl. Maven build, I will try to help. But probably that is worth a new question. BTW, many of my answers here already include Maven POMs. – kriegaex Mar 03 '18 at 03:11
  • As for JDeveloper, I never used it and do not know how well it integrates with AspectJ. I suggest you use Eclipse for that job. AspectJ is an Eclipse project, so the AspectJ integration is better than any other IDE's. But with some limitations it also works nicely in IntelliJ IDEA. Both IDEs can automatically import and configure AspectJ when importing a Maven project using it via AspectJ Maven plugin. – kriegaex Mar 03 '18 at 03:13