使用@Autowired与AspectJ和Springboot

 潮流Fee_750 发布于 2023-01-31 13:56

我想将@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
                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在我的应用程序类上面添加

谢谢你的帮助

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有