简体   繁体   English

Null 存储库,即使实现了@Autowired

[英]Null repository even with @Autowired implemented

I have the following controller.我有以下 controller。

The following line works just fine:以下行工作得很好:

user = userRepository.selectUserByLogin(name);用户 = userRepository.selectUserByLogin(name);

It correctly returns the user.它正确地返回用户。

@Controller
public class TestController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(method = RequestMethod.GET, value = "/testpage")
    public String initTest() {

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = user.getUsername();
        
        User user = new User();
        user = userRepository.selectUserByLogin(name);

        return "";
    }
}

Now I want to move that code to a getLoggedUser method of a "Utilities" class.现在我想将该代码移动到“实用程序”class 的 getLoggedUser 方法。 This is how I did it.我就是这样做的。

Controller Controller

@Controller
public class TestController {

    @RequestMapping(method = RequestMethod.GET, value = "/testpage")
    public String initTest() {
        
        Utilities utilities = new Utilities();

        User user = new User();
        user = utilities.getLoggedUser();

        return "";
    }
}

Utilities实用程序

public class Utilities {
    
    @Autowired
    private UserRepository userRepository;  
    
    public User getLoggedUser() {
        
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = user.getUsername();
        
        User user = new User();
        user = userRepository.selectUserByLogin(name);
        
        return user;
    }
}

But when that is executed I'm getting the following error:但是当它被执行时,我收到以下错误:

Cannot invoke "UserRepository.selectUserByLogin(String)" because "this.userRepository" is null.无法调用“UserRepository.selectUserByLogin(String)”,因为“this.userRepository”是 null。

Why is it null if it is with the @Autowired notation?如果使用 @Autowired 表示法,为什么它是 null? Looks the same as in the original implementation that worked.看起来与原来有效的实现相同。

Spring is not going to be able to autowire your repository if you create an instance of Utilities class with new like: Utilities utilities = new Utilities();如果您使用以下新内容创建Utilities class 的实例,则 Spring 将无法自动装配您的存储库: Utilities utilities = new Utilities();

In order to do so you will have to add @Component or @Service annotation to your Utilities class:为此,您必须在Utilities class 中添加@Component@Service注释:

@Component
public class Utilities {

and then autowire it into your controller:然后将其自动连接到您的 controller:

@Controller
public class TestController {

    @Autowired
    private Utilities utilities;

    @RequestMapping(method = RequestMethod.GET, value = "/testpage")
    public String initTest() {
        User user = new User();
        user = utilities.getLoggedUser();

        return "";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM