我想将@Autowired注释用于"Aspect".我想在我的方面注入一个存储库但是当我尝试调用我的自动连接类的方法时,会发生NullPointException.
@Aspect public class AspectSecurity { @Autowired private UserRepository userRepository; @After("execution(public * dash.*.*Controller.*(..))") public void authentication(JoinPoint jp) throws UnauthorizedException { System.out.println("SECURITY !"); System.out.println(userRepository.findAll().toString()); } }
我已经尝试@Component
在我的方面类上面添加,但我有同样的错误.
如果我不使用方面类,但@Controller
我可以毫无问题地调用我的存储库.
一些文档说明了使用xml文件的spring配置,但是使用spring boot我没有这些文件.
这是我的pom.xml的一部分,它调用了aspectJ插件:
org.codehaus.mojo aspectj-maven-plugin 1.4 true 1.6 ignore ${compiler.version} UTF-8 false org.springframework spring-aspects compile test-compile org.aspectj aspectjrt ${aspectj.version} org.aspectj aspectjtools ${aspectj.version}
这是我的Application类:
package dash; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
这里调用方面的Controller类:
package dash.user; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import dash.GenericController; @Controller @RequestMapping("/user") public class UserController extends GenericController { @Autowired private UserRepository repository; @RequestMapping("/findAll") public @ResponseBody User create( @RequestParam(value="login", required=true) String login, @RequestParam(value="password", required=true) String password) { System.out.println(login); System.out.println(password); System.out.println("Users found with findAll():"); System.out.println("-------------------------------"); for (User user : repository.findAll()) { System.out.println(user); } return repository.findOne("root"); } }
注意:我已经尝试@EnableAspectJAutoProxy
在我的应用程序类上面添加
谢谢你的帮助