繁体   English   中英

启用S​​pring AOP避免依赖注入

[英]Enabling spring aop sidesteps dependency injection

我有一个简单的spring bean(S2动作类的spring bean),它使用自动装配来注入依赖项,这些依赖项按照常规方式工作。 但是,当我使用JDK / cglib代理的@Before建议使该bean的方法之一进入spring aop时,跳过了依赖项注入,未设置我的变量。

我知道这与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属性注入依赖关系(而不是自动装配)也无济于事。
  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

我想我发现了问题,在struts配置中的action class必须是sampleActionBean 我知道我是从这种方式开始的,不确定恢复时的歉意。

春天永远不会离开注射目标,即。 @Autowired字段或方法,为null 如果找不到要注入的bean,它将始终引发异常。

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

暂无
暂无

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

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