java - 启用 spring aop 回避依赖注入(inject)

标签 java spring dependency-injection aspectj spring-aop

我有一个简单的 spring bean(S2 操作类的 bean),它使用 Autowiring 来注入(inject)像往常一样工作的依赖项。然而,当我将此 bean 的方法之一置于 spring aop 中时,例如 @Before建议使用 JDK/cglib 代理,跳过依赖项注入(inject),使我的变量未设置。

我知道这与 spring aop 通过代理完成连接点的方式有关,但我无法从文档中找到有关此特定问题的任何内容。我错过了什么?

如果有人需要看一下,我也可以发布上下文 xml,但它确实非常简单,它包含的只是几个用于操作类的 bean、一个用于方面的 bean 和一个 <aop:aspectj-autoproxy>设置。

答案:

错误如下:

<action name="sampleAction" class="com.example.s2.SampleAction" method="actionMethod">

class属性必须引用 spring 应用程序上下文中的 spring bean。

更新 1:

  1. 我尝试了 JDK 代理和 CGLib,这两种情况都会出现相同的问题。
  2. 使用 bean 属性注入(inject)依赖项(而不是 Autowiring )也没有帮助。
  3. 我使用 spring 4.0.0.RELEASE 和 struts2.1.3。

更新 2(包含代码):

<!-- ======================== Spring context annotation support======================== -->
    <context:annotation-config />
    <bean id="testAspect" class="com.example.s2.aop.TestAspect" />

    <!-- ======================== AspectJ configuration======================== --> 
    <!-- Proxy-target-class must be set true if the object being proxied does not implement even a single interface (or if the advised method
           does not come from an interface. Setting this to true tells spring to use cglib for proxying -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

  <!-- ======================== Spring managed beans ======================== -->
    <bean id="sampleActionBean"
            class="com.example.s2.SampleAction"
            scope="prototype">
    </bean>

    <!-- use a single offer service manager instance throughout the course of the context -->
    <bean id="dependency1" class="com.example.s2.DependencyProviderFactory" factory-method="getDependency1Instance" />

TestAspect 类:

@Aspect
public class TestAspect
{
  private static final Logger SLOGGER = Logger.getLogger(TestAspect.class);
  @Before ("actionAnnotatedPointCut() && (publicActionMethodPointCut() || protectedActionMethodPointCut())")
  public void checkUserAccess(JoinPoint pJoinPoint) throws Throwable
  {
    SLOGGER.smartError("Intercepted:", pJoinPoint.getSignature().getName());
  }

  @AfterThrowing (value = "actionAnnotatedPointCut() && (publicActionMethodPointCut() || protectedActionMethodPointCut())", throwing = "pException")
  public void handlePortletException(JoinPoint pJoinPoint, Exception pException)
  {
    SLOGGER.smartError("Method threw error:", pJoinPoint.getSignature().getName(), "| Exception:", pException);
  }

  @Pointcut ("@annotation(org.apache.struts2.convention.annotation.Action")
  public void actionAnnotatedPointCut() {}

  @Pointcut ("execution(public String *(..))")
  public void publicActionMethodPointCut() {}

  @Pointcut ("execution(protected String *(..))")
  public void protectedActionMethodPointCut() {}
}

被建议的类(SampleAction):

public class SampleAction
{
  @Autowired
  private Dependency1 dependency1;
  ...
}

struts.xml:

<constant name="struts.objectFactory" value="spring" />

<action name="sampleAction" class="com.example.s2.SampleAction" method="actionMethod">
  <result name="success">/pages/actionOutput.jsp</result>
</action>

更新 3:

我想我发现了问题,class action的在struts配置中必须是sampleActionBean 。我知道我是这样开始的,不确定什么时候又恢复了,抱歉。

最佳答案

Spring永远不会留下注入(inject)目标,即。 @Autowired 字段或方法,如 null。如果找不到要注入(inject)的 bean,它总是会抛出异常。

因此,错误一定是在其他地方。鉴于您拥有 Struts,我假设您可能正在查看由 Struts 管理的对象,并认为它们也由 Spring 管理。仅当您的 Spring-Struts 集成正确完成时,这才是正确的。

关于java - 启用 spring aop 回避依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21420628/

相关文章:

java - 谷歌应用引擎: 1000 records limit still present with JPA?

java - 启动简单的非基于 Web 的 Java 应用程序的官方 Spring Boot 方式是什么?

c++ - 对齐指针以具有功能 -> 运算符? C++

java - 使用 `java.nio.file.spi`来实现对远程文件系统的访问有意义吗?

java - 从 JButton 发送命令行命令?

java - 如何使用 Spring Batch 在作业中动态添加步骤

java - 通过 jdbc 模板使用两个数据源

c# - 通过引入工厂使依赖注入(inject)成为可选?

java - 使用 spring mvc 转换并保存日期

java - 让 Spring MVC 与 JodaModule 一起工作