spring @Aspect 不注入依赖项

发布于 2024-10-14 04:39:22 字数 789 浏览 4 评论 0原文

我正在使用 Maven、Spring 和 AspectJ 进行编译时编织

我的 AspectJ Advisor 看起来像这样

@Aspect
public class LoggingInterceptor {
  private LogManager logManager;
  public void setLogManager(LogManager logManager) {
    this.logManager = logManager;
  }
  .....
} 

我的 applicationContext.xml 看起来像这样

<!--configures the AspectJ aspect and indicates which Spring context should be used when giving advice-->
<context:spring-configured />

<aop:aspectj-autoproxy/>

<!--<context:component-scan base-package="com.reverb" />-->

<bean id="loggingInterceptor" class="com.myapp.interceptor.LoggingInterceptor">
    <property name="logManager" ref="logManager" />
</bean>

LogManager 始终为空......

I am using compile time weaving using maven, spring and aspectj

my aspectj advisor looks like this

@Aspect
public class LoggingInterceptor {
  private LogManager logManager;
  public void setLogManager(LogManager logManager) {
    this.logManager = logManager;
  }
  .....
} 

My applicationContext.xml look like this

<!--configures the AspectJ aspect and indicates which Spring context should be used when giving advice-->
<context:spring-configured />

<aop:aspectj-autoproxy/>

<!--<context:component-scan base-package="com.reverb" />-->

<bean id="loggingInterceptor" class="com.myapp.interceptor.LoggingInterceptor">
    <property name="logManager" ref="logManager" />
</bean>

The logManager is always null....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美男兮 2024-10-21 04:39:22

我没有看到您的 logManager 在任何地方定义。即使是,@Aspect 也不会自动符合注入条件。事实上,您有 2 个对象 - 一个是 LoggingInterceptor 类型的 bean,另一个是实际处理 AOP 的切面。但方面不是豆。

为了使其工作,您需要为 定义 factory-method="aspectOf"参见此处< /a> 了解更多信息。

I don't see your logManager to be defined anywhere. Even if it is, @Aspects are not automatically eligible for injection. In fact what happens is that you have 2 objects - one is a bean of type LoggingInterceptor, and the other is the aspect, which actually handles the AOP. But the aspect is not a bean.

In order to make this work, you'd need to define factory-method="aspectOf" for your <bean>. See here for more info.

铁轨上的流浪者 2024-10-21 04:39:22

使用 java 配置,它看起来像:

@Configuration
@EnableSpringConfigured
public class AspectConfig {
}

不要忘记:

  • 添加 @Configurable 注释

在方面

@Aspect
@Configurable
public class CounterAspect { 
    @Inject
    private CounterService counter;
    //...
} 
  • Add 'org.springframework:spring-aspects' as编译依赖

  • 添加META-INF/aop.xml

内容:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>   
    <!-- add to debug: options="-showWeaveInfo -verbose -debug"-->
    <weaver>
        <include within="com..*"/>
    </weaver>    
    <aspects>
        <aspect name="com.your.package.CounterAspect"/>
    </aspects>    
</aspectj>
  • 使用 -javaagent:/path-to-aspectj/aspectjweaver-1.8.10.jar 启用 javaagent

With java configuration it will be look like:

@Configuration
@EnableSpringConfigured
public class AspectConfig {
}

Don't forget to:

  • Add @Configurable annotation

on aspect:

@Aspect
@Configurable
public class CounterAspect { 
    @Inject
    private CounterService counter;
    //...
} 
  • Add 'org.springframework:spring-aspects' as compile dependency

  • Add META-INF/aop.xml

with content:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>   
    <!-- add to debug: options="-showWeaveInfo -verbose -debug"-->
    <weaver>
        <include within="com..*"/>
    </weaver>    
    <aspects>
        <aspect name="com.your.package.CounterAspect"/>
    </aspects>    
</aspectj>
  • Enable javaagent with something like -javaagent:/path-to-aspectj/aspectjweaver-1.8.10.jar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文