我有一个简单的 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:
- 我尝试了 JDK 代理和 CGLib,这两种情况都会出现相同的问题。
- 使用 bean 属性注入(inject)依赖项(而不是 Autowiring )也没有帮助。
- 我使用 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/