![](/img/trans.png)
[英]Autowired service null in AspectJ aspect on a Spring Boot / Swing standalone app
[英]Spring autowired bean for @Aspect aspect is null
我有以下弹簧配置:
<context:component-scan base-package="uk.co.mysite.googlecontactsync.aop"/>
<bean name="simpleEmailSender" class="uk.co.mysite.util.email.simple.SimpleEmailSenderImplementation"/>
<aop:aspectj-autoproxy/>
然后我有一个方面:
@Aspect
public class SyncLoggingAspect {
@Autowired
private SimpleEmailSender simpleEmailSender
@AfterReturning(value="execution(* uk.co.mysite.datasync.polling.Poller+.doPoll())", returning="pusher")
public void afterPoll(Pusher pusher) {
simpleEmailSender.send(new PusherEmail(pusher));
}
}
这方面有效(我可以在 afterPoll 上打断点)但 simpleEmailSender 为空。 不幸的是,我找不到关于为什么会这样的明确文档。 (作为记录,我的 simpleEmailSender bean 存在并正确连接到其他类)以下事情让我感到困惑:
显然,您可以看出我不了解事情应该如何从头开始。
该方面是一个单例对象,是在 Spring 容器之外创建的。 XML 配置的解决方案是使用 Spring 的工厂方法来检索方面。
<bean id="syncLoggingAspect" class="uk.co.demo.SyncLoggingAspect"
factory-method="aspectOf" />
使用此配置,方面将被视为任何其他 Spring bean,并且自动装配将正常工作。
您还必须在 Enum 对象和其他没有构造函数的对象或在 Spring 容器外部创建的对象上使用工厂方法。
另一种选择是将@Configurable
添加到您的方面类而不是搞乱 XML。
为了让 Spring Boot 在 AspectJ 中使用 @Autowired,我找到了以下方法。 在配置类中添加您的方面:
@Configuration
@ComponentScan("com.kirillch.eqrul")
public class AspectConfig {
@Bean
public EmailAspect theAspect() {
EmailAspect aspect = Aspects.aspectOf(EmailAspect.class);
return aspect;
}
}
然后,您可以在方面类中成功自动装配您的服务:
@Aspect
public class EmailAspect {
@Autowired
EmailService emailService;
仅使用 java config 配置@Autowired (因此没有基于 XML 的配置)需要一些额外的工作,而不仅仅是将@Configuration添加到类中,因为它还需要 aspectOf 方法。
对我有用的是创建一个新类:
@Component
public class SpringApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
然后在你的方面结合使用@DependsOn @Configured 和@Autowired:
@DependsOn("springApplicationContextHolder")
@Configuration
@Aspect
public class SomeAspect {
@Autowired
private SomeBean someBean;
public static SomeAspect aspectOf() {
return SpringApplicationContextHolder.getApplicationContext().getBean(SomeAspect.class);
}
需要@DependsOn 是因为spring 无法确定依赖项,因为bean 是静态使用的。
我没有 50 位代表对一个问题发表评论,所以这里是另一个与@ Jitendra Vispute答案相关的答案。 官方 Spring 文档提到:
您可以在 Spring XML 配置中将方面类注册为常规 bean,或者通过类路径扫描自动检测它们 - 就像任何其他 Spring 管理的 bean 一样。 但是,请注意 @Aspect 注释对于类路径中的自动检测是不够的:为此,您需要添加一个单独的 @Component 注释(或者,根据 Spring 组件扫描器的规则,您需要添加一个符合条件的自定义构造型注释)。 来源:Spring '4.1.7.Release' 文档。
这意味着在您的配置上添加@Component 注释和@ComponentScan 将使@Jitendra Vispute 的示例工作。 对于 spring boot aop 示例,它起作用了,尽管我没有搞乱上下文刷新。 Spring Boot aop 示例:
应用:
package sample.aop;
@SpringBootApplication
public class SampleAopApplication implements CommandLineRunner {
// Simple example shows how an application can spy on itself with AOP
@Autowired
private HelloWorldService helloWorldService;
@Override
public void run(String... args) {
System.out.println(this.helloWorldService.getHelloMessage());
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleAopApplication.class, args);
}
}
应用程序还应该作为普通的 Spring Framework 应用程序运行,使用以下注释而不是 @SpringBootApplication:
和 AnnotationConfigApplicationContext 而不是 SpringApplication。
服务:
package sample.aop.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldService {
@Value("${name:World}")
private String name;
public String getHelloMessage() {
return "Hello " + this.name;
}
}
监控方面:
package sample.aop.monitor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ServiceMonitor {
@AfterReturning("execution(* sample..*Service.*(..))")
public void logServiceAccess(JoinPoint joinPoint) {
System.out.println("Completed: " + joinPoint);
}
}
这篇博文很好地解释了它。 由于方面单例是在 spring 容器之外创建的,因此您需要使用 factory-method="aspectOf" ,它只有在被 AspectJ(而不是 Spring AOP)编织后才可用:
注意 factory-method="aspectOf" 告诉 Spring 使用真正的 AspectJ(而不是 Spring AOP)方面来创建这个 bean。 因此,在编织方面之后,它有一个“aspectOf”方法。
以便 :
找不到匹配的工厂方法:工厂方法 'aspectOf()' - 这意味着该方面不是由 AspectJ 编织者编织的。
根据我对 spring 3.1 的经验,如果我不使用 @Autowired 而是使用传统的 setter 进行依赖项注入,它会被注入并在没有 aspectJ weaver 的情况下按预期工作。 尽管我遇到了单例方面的问题......它导致了'perthis'实例化模型。 .
将@Component 添加到方面类,您的依赖项应该会自动注入。 并为您的方面在 spring 上下文文件中的包添加上下文:组件扫描。
@Component
@Aspect
public class SomeAspect {
/* following dependency should get injected */
@Autowired
SomeTask someTask;
/* rest of code */
}
使用编译时编织,插件示例见: https : //github.com/avner-levy/minimal_spring_hibernate_maven_setup/blob/master/pom.xml
由于 Tobias/Willie/Eric 上面的注释,以下注释和 Spring 配置的组合对我有用:
班级:
package com.abc
@Configurable
@Aspect
public class MyAspect {
@Autowired
protected SomeType someAutoWiredField;
}
XML:
<context:spring-configured />
<context:component-scan base-package="com.abc" />
@Configurable(autowire = Autowire.BY_TYPE)
将此注释添加到您的Aspectj
类。 然后由Spring IOC处理。
对于在@Aspect 中使用@Autowired 的 Spring Boot,我的方法是使用 spring.factories 配置文件
在 src/main/resources 中创建一个名为 spring.factories 的文件
文件内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=pack.age.to.YourAspect,pack.age.to.YourDAO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.