小编dau*_*ruy的帖子

@Inject和@PostConstruct不能以单例模式工作

我有一个课程如下:

public class UserAuthenticator {

    private static UserAuthenticator authenticator = 

    @Inject
    private UserRepository userRepository;

    @PostConstruct
    public void init() {
        List<User> allUsers = userRepository.findAll();
        for (User user : allUsers) {
            users.put(user.getEmail(), user.getPassword());
            serviceKeys.put(user.getServiceKey(), user.getEmail());
        }
    }

    public static UserAuthenticator getInstance() {
        if (authenticator == null) {
            authenticator = new UserAuthenticator();
        }
        return authenticator;
    }
}
Run Code Online (Sandbox Code Playgroud)

我打电话的时候

UserAuthenticator authenticator = UserAuthenticator.getInstance();
Run Code Online (Sandbox Code Playgroud)

init() 方法未被调用,而userRepository是 null

我的Web应用程序在JBOSS EAP 6.3中运行.

这是怎么造成的,我该如何解决?

java singleton dependency-injection java-ee postconstruct

5
推荐指数
1
解决办法
3715
查看次数