如何将 Spring-boot 服务注入 aspectj class?

How inject a Spring-boot service into a aspectj class?

我有问题.. 我正在创建 aspectj class 并进入我的 class 我需要访问 spring-boot 服务,但是当我尝试使用@Autowired 或通过构造函数注入它我有一个错误:

"Could not autowire. No beans of 'UserService' type found"

这是我的 class:

package com.ingressolive.bar.aop.interceptor;


@Aspect
@Configuration
public class TenantAspect {
    private final Logger log = LoggerFactory.getLogger(this.getClass());


    private final Environment env;

    @Autowired
    private UserService userService;


    public TenantAspect(Environment env) {
        this.env = env;

    }

    @Before("execution(* com.ingressolive.bar.service.*.*(..))")
    public void aroundExecution(JoinPoint pjp) {
        log.debug("##################################### Entered here !!!!!!!!!!!!!!!!!!!!!!!!!!");

    }
}

有人可以帮助我吗?

您可以尝试使用 Component 而不是 Configuration 吗?我正在使用这样的方面,并且自动装配工作得很好。

package com.ingressolive.bar.aop.interceptor;

@Aspect
@Component
public class TenantAspect {
   ...
}

也许您需要寻找其他配置问题,例如配置文件,未加载 xml 配置?如果您的 bean 有任何 xml 配置,请考虑使用以下模式:

package com.yourpackage.config;

@Configuration
@ImportResource(
        locations = {
                "classpath:/your-extra-config-1.xml",
                "classpath:/your-extra-config-2.xml",
        })
public class AppConfig {
    ...
}