“@inject”-ed attribute remains null

前端 未结 2 2129
天涯浪人
天涯浪人 2021-02-19 04:14

I am trying to inject a service into my bean but it is always null. I get the following error: WELD-001000 Error resolving property userBean against base null.

2条回答
  •  甜味超标
    2021-02-19 04:37

    The error message could be a hint, that the JVM wasn't able to create an instance of UserBean. The following is some guessing and would have to be proven:

    Dependecy Injection requires a dependency injector, a tool that injects an instance of UserService into a UserBean. In your code you're already using this injected instance during instantiation of the bean: you call the injected service in the constructor.

    If the dependency injector starts it's work after the bean is created, then the call to the service inside the constructor will raise a NullPointerException (because service is still null at that time). It's worth checking that by trying to catch NPEs in the UserBean constructor for a moment. If you catch one - voilà - the dependency injector starts runnning after the bean has been created and, as a consequence, we can't use injected services during class instantiation (= in the constructor)


    Workaround idea: implement a small service provider helper class - inner class could work:

    public class UserBean implements Serializable {
        static class UserServiceProvider {
          @Inject static UserService service;
        }
    
        // ... 
    
        public UserBean() {
          this.user = UserServiceProvider.service.findUser("kaas"); 
        }
    
        // ...
    }
    

    Untested but could work - the service should be injected in the provider class before you use it in the beans constructor.

提交回复
热议问题