当前位置: 首页 > 知识库问答 >
问题:

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

后星河
2023-03-14

我有个问题..我正在创建一个aspectj类,在我的类中我需要访问一个spring-boot服务,但是当我尝试使用@AutoWired或通过构造函数注入它时,我出现了一个错误:

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 !!!!!!!!!!!!!!!!!!!!!!!!!!");

    }
}

有人能帮帮我吗?

共有1个答案

景俊语
2023-03-14

您能尝试使用组件而不是配置吗?我正在使用像这样的方面,自动连接工作完美。

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 {
    ...
}
 类似资料:
  • 我有一些UserService对用户实体进行操作。我创建了自己的注释和ConstraintValidator类作为实现 我需要将UserService注入到ConstraintValidator中。而且,正如spring文档所说,在注册bean之后: 不幸的是,它对我没有作用。我在字段(在实现ConstraintValidator的类内)使用NullPointerException累加stackt

  • 问题内容: authService.getLoggedin()返回false或true取决于用户是否登录。然后,我想不允许他们进入网址,如果不允许的话。 但是我收到此错误:错误:[$ injector:modulerr]由于以下原因无法实例化模块WikiApp:[$ injector:unpr]未知提供程序:authService 问题答案: 在配置阶段,您只能要求提供程序($ routeProv

  • 我试图在方法注释上创建一个Aeyj切入点,但我总是用不同的方法失败。我使用的是aspectj自动代理(我在Spring上下文中没有配置其他编织)。我的类如下所示: 所以我想知道为什么aspectj不会创建切入点。我设法使用执行(**(…)使其工作抛出一些exc)这对我来说很好,但我仍然想知道我做错了什么。 另外,由于是在接口中定义的,我指定了实现类的注释,有没有办法让它以这种方式工作?其他代理机制

  • 在我的Spring Boot Application中,我用一个调用存储过程的方法实现了以下类。 由于调用存储过程需要数据库连接,所以我需要从应用程序加载这些相应的属性值。特性: 基于以下关于类似问题的文章,Spring boot—应用程序中的自定义变量。属性,并在您自己的类和Spring Boot@ConfigurationProperties示例中使用Spring Boot配置属性,我为我的类

  • 我正在开发一个cron,我正在使用一个web客户端向一个rest API发送一个post请求。我不想让嵌入式服务器保持开机状态,因为cron需要在几秒钟内完成其任务。但当我不使用服务器时: 更新:我试过了 但是获取由:java.lang.IllegalStateException:org.springframework.boot.web.reactive.context.AnnotationCon

  • 并编写了一个示例spring cloud etflix eureka maven项目,该项目在http://localhost:8761上运行良好 我在spring boot微服务主类上都使用了注释@EureKadisCoveryClient和@SpringBootApplication 我使用了注释@enableeurekaserver和@springbootapplication