Home > Net >  @Autowired dependency has null members
@Autowired dependency has null members

Time:11-14

My Config class:

@Configuration
public class TimeOutConfig {

    @Value("${value1}")
    private int value1;

    @Value("${value2}")
    private int value2;

    @Bean()
    User boTimeOut() {
        System.out.println("In BOTimeOutConfig");
        BeanTest b1 = new BeanTest(value1, value2);
        User u1 = new User(b1);
        return u1;
    }


    @Value("${value3}")
    private int value3;

    @Value("${value4}")
    private int value4;

    @Bean(name = "BSETimeout")
    User getBSEUser() {
        System.out.println("In BSETimeOutConfig");
        User u2 = new User( new BeanTest(value3, value4));
        return u2;
    }

}

BeanTest Class:

@Component
public class BeanTest {

    private int v1;
    private int v2;

   BeanWebClient b1;

    public BeanTest(){}

    public BeanTest(int v1, int v2){
        this.v1=v1;
        this.v2=v2;
        beanWebClient();
        
    }
    public BeanWebClient beanWebClient() {
        //System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: " v1 v2);
        b1 = new BeanWebClient(v1,v2);
        System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: " b1.getV1() " " b1.getV2());
        return b1;
    }

    public void message()
    {
        b1.webClientMessage();
        //System.out.println("V1= " v1 " V2= " v2);
    }
}

BeanWebClient Class:

@Component
public class BeanWebClient {

    private int v1;
    private int v2;
    public BeanWebClient(int v1, int v2){

        this.v1=v1;
        this.v2=v2;
        System.out.println("Received from BeanTest " v1 " " v2);
    }

    public BeanWebClient(){

    }

    public int getV1() {
        return v1;
    }
    public int getV2() {
        return v2;
    }

    public void webClientMessage(){

        System.out.println("In BeanWebClient class, printing from autowired dependency " v1 " " v2);
    }

}

My User class:

@Component
public class User {

    @Autowired
    private BeanTest beanTest;



    public User(BeanTest beanTest){

        this.beanTest=beanTest;
    }

    public void message()
    {

        beanTest.message();
    }


    public User() {

        //System.out.println("user create... hashCode :"   this.hashCode());
    }
}

Finally, My Service class:

@Service
public class BeanService {

    @Autowired
    @Qualifier("boTimeOut")
    private User boUser;

    @Autowired
    @Qualifier("BSETimeout")
    private User bseUser;

    public void printBO()
    {
        boUser.message();

    }
    public void printBSE()
    {
        bseUser.message();
    }

}

I was trying to understand how components and autowiring dependencies work in spring boot. In my User class, the autowired BeanTest class is initialized, during configuration- all the necessary print statements are executed. But when I call the functions of the Service class, The autowired User dependency has null for beanTest and its members. As opposed to this, when I just create the BeanTest object in my User class everything works as expected. This is what I mean:

@Component
public class User {

    BeanTest beanTest;



    public User(BeanTest beanTest){

        this.beanTest=beanTest;
    }

    public void message()
    {

        beanTest.message();
    }


    public User() {

        //System.out.println("user create... hashCode :"   this.hashCode());
    }
}

I sort of understand that The bean autowired in User class is taken from the container is null. Since Im Autowiring specific User dependency in my service class, I don't understand why it's not working. Can someone please explain If and How I can autowire the BeanTest class into User class correctly. Sorry for so many code files.

Outputs This is the Output I get When I create the BeanTest object manually without @Autowire

In BOTimeOutConfig
Received from BeanTest 10 20
In BeanTest class beanWebClient() method execution v1 and v2: 10 20
In BSETimeOutConfig
Received from BeanTest 30 40
In BeanTest class beanWebClient() method execution v1 and v2: 30 40
2021-11-13 14:37:58.161  WARN 404383 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-13 14:37:58.613  INFO 404383 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-13 14:37:58.622  INFO 404383 --- [           main] c.paytmmoney.errordb.ErrordbApplication  : Started ErrordbApplication in 4.04 seconds (JVM running for 4.379)
2021-11-13 14:38:07.768  INFO 404383 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-13 14:38:07.768  INFO 404383 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-13 14:38:07.769  INFO 404383 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
In /test method
In BeanWebClient class, printing from autowired dependency 10 20
In /test method
In BeanWebClient class, printing from autowired dependency 30 40

When I autowire BeanTest Class this is the Output:

In BOTimeOutConfig
Received from BeanTest 10 20
In BeanTest class beanWebClient() method execution v1 and v2: 10 20
In BSETimeOutConfig
Received from BeanTest 30 40
In BeanTest class beanWebClient() method execution v1 and v2: 30 40
2021-11-13 14:40:36.072  WARN 404724 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-13 14:40:36.585  INFO 404724 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-13 14:40:36.596  INFO 404724 --- [           main] c.paytmmoney.errordb.ErrordbApplication  : Started ErrordbApplication in 4.353 seconds (JVM running for 4.75)
2021-11-13 14:40:45.081  INFO 404724 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-13 14:40:45.081  INFO 404724 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-13 14:40:45.082  INFO 404724 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
In /test method
2021-11-13 14:40:45.204 ERROR 404724 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.paytmmoney.errordb.repository.BeanTest.message(BeanTest.java:42) ~[main/:na]
    at com.paytmmoney.errordb.repository.User.message(User.java:22) ~[main/:na]
    at com.paytmmoney.errordb.service.BeanService.printBO(BeanService.java:21) ~[main/:na]

I'm using a rest controller to call functions in the Service class.
THANK YOU!!!

CodePudding user response:

Given that you are creating BeanTest and User instances on your own in TimeOutConfig, please remove all @Component and @Autowired annotations from BeanTest, BeanWebClient and User as follows:

public class BeanTest {

    private int v1;
    private int v2;
    BeanWebClient b1;

    public BeanTest(){}

    public BeanTest(int v1, int v2){
        this.v1=v1;
        this.v2=v2;
        beanWebClient();
    }
    
    public BeanWebClient beanWebClient() {
        //System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: " v1 v2);
        b1 = new BeanWebClient(v1,v2);
        System.out.println("In BeanTest class beanWebClient() method execution v1 and v2: " b1.getV1() " " b1.getV2());
        return b1;
    }

    public void message() {
        b1.webClientMessage();
        //System.out.println("V1= " v1 " V2= " v2);
    }
}
public class BeanWebClient {

    private int v1;
    private int v2;
    
    public BeanWebClient(int v1, int v2) {}
        this.v1=v1;
        this.v2=v2;
        System.out.println("Received from BeanTest " v1 " " v2);
    }

    public BeanWebClient(){ }

    public int getV1() {
        return v1;
    }
    
    public int getV2() {
        return v2;
    }

    public void webClientMessage(){
        System.out.println("In BeanWebClient class, printing from autowired dependency " v1 " " v2);
    }
}
public class User {

    private BeanTest beanTest;

    public User(BeanTest beanTest) {
        this.beanTest=beanTest;
    }
    
    public User() {
        //System.out.println("user create... hashCode :"   this.hashCode());
    }

    public void message() {
        beanTest.message();
    }
}
  • Related