Using @Autowired with AspectJ and Springboot

前端 未结 2 1658
眼角桃花
眼角桃花 2021-01-17 16:05

I want to use @Autowired annotation into an "Aspect". I want to inject a repository in my aspect but when I try to call a method of my autowired class a NullPointE

2条回答
  •  星月不相逢
    2021-01-17 16:56

    Here is my configuration:

    @Component
    @Aspect
    public class WebControllerAop {
        @Autowired
        private PersonService personService;
        //匹配com.zkn.learnspringboot.web.controller包及其子包下的所有类的所有方法
        @Pointcut("execution(* com.zkn.learnspringboot.web.controller..*.*(..))")
        public void executeService(){
    
        }
    }
    
    @RestController
    @EnableAutoConfiguration
    @ComponentScan
    public class FirstExample {
        public static void main(String[] args) {
            SpringApplication.run(FirstExample.class, args);
        }
    }
    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.0.RELEASE
    
    
    
          org.springframework.boot
          spring-boot-starter-aop
      
    

    It can work well.

提交回复
热议问题