简体   繁体   English

Struts 1和Spring AOP

[英]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. 我有一个使用Struts 1制作的应用程序,它需要一些听觉,所以我想要的是使用方面,因此我不需要更改类,而只是“拦截”对方法的调用。

What I'm trying to do is using Spring AOP + Aspectj integration, but first of all I need to integrate Struts and Spring. 我正在尝试使用Spring AOP + Aspectj集成,但是首先我需要集成Struts和Spring。 I have been trying with several tutorials and similar questions here, but apparently they are meant mostly for Struts 2. 我在这里尝试了一些教程和类似的问题,但是显然它们主要是针对Struts 2的。

To be more specific, I'm doing this: 更具体地说,我正在这样做:

web.xml web.xml中

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

applicationContext.xml 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 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 的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. 我没有对struts-config.xml文件做任何事情。 The server load the application without any problem, but Spring is not loading and so the aspect. 服务器加载应用程序没有任何问题,但是Spring没有加载,因此方面。

Any idea of how to solve this? 有什么解决办法的想法吗?

If you want to apply AOP to a legacy, non-Spring application you do not introduce Spring just because of Spring AOP. 如果要将AOP应用于遗留的非Spring应用程序,则不会仅仅因为Spring AOP而引入Spring。 You directly use AspectJ and have several options here: 您可以直接使用AspectJ并在此处具有几个选项:

  • Compile the original application with the AspectJ compiler if you control the build of the original application, and if you don't: 如果您控制原始应用程序的构建,而没有控制原始应用程序的构建,则使用AspectJ编译器编译原始应用程序:
  • Post-compile time binary weaving creates AOP-enhanced copies of your original class files. 编译后的二进制编织会创建原始类文件的AOP增强副本。 Re-Package them and use them instead of the unwoven originals. 重新包装它们,并使用它们代替未编织的原稿。
  • Load-time weaving: A Java agent does the weaving during class-loading. 加载时编织:Java代理在类加载期间进行编织。

AspectJ is much more powerful than Spring AOP, faster (no proxies) and completely independent of Spring. AspectJ比Spring AOP功能强大得多,速度更快(没有代理),并且完全独立于Spring。 That is what you want to use. 那就是您要使用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM