0

Language/frameworks : Java8/Spring 4.3.9/openjpa 2.4.3

Being a Spring newbie, I suspect I am not applying the proper pattern here.

The scenario is something like what is shown below. However, I cannot get objectB to initialize and a Null Pointer exception is thrown. I suspect that the problem is that Spring is unable to initialize class A but I cannot set the "@Component" annotation to class A as it does not have a default constructor. Is there a workaround for this scenario ? A pattern that should be followed perhaps ? This is legacy code that is being retrofitted with Spring.

@ContextConfiguration(locations = { "classpath:/context.xml" })
ClassTestA{

    @Test
    public void TestA(){

        :
        :
        A a = new A ("A", "B", "C", "D");
        :
        :
     // Following line, Throws a null pointer exception in the doSomething() method.
        someobject.someMethod(a.doSomething());         

    }

}

class A {

    @Autowired  
    private B objectB;


    public A (string t, string m, string x, string y)
    {
        // variable inits

    }

    // C class represents a database table Entity 
    public C doSomething()
    {
        C c = new C();

        c.someWork(objectB.method()); // throws a null pointer exception because obJectB is null !

    }       

}

@Component
public class B
{
      // No constructor



}
user1554876
  • 192
  • 3
  • 19
  • If you can share your xml file then i can give you correct answer. You can't use static keyword with @autowired annotation. You can try without static keyword. You can checkout given link to understand why we cannot autowire static fields in spring. https://stackoverflow.com/questions/10938529/why-cant-we-autowire-static-fields-in-spring/21875258 – Gaurav Priyadarshi Jun 18 '19 at 21:43
  • @Gaurav - Removed the static keyword but the problem persists. – user1554876 Jun 19 '19 at 01:54
  • "Null Pointer exception is thrown" - where? "I cannot set the "@Component" annotation to class A as it does not have a default constructor" - it will need one, except you can autowire the constructor parameters as well, eg `@Value`s from a config file. – daniu Jun 19 '19 at 04:50

1 Answers1

0

In your test you have created object of class A by using new keyword. Once you do it, spring will not take care of dependencies in class A, so it won't autowire class B object. For spring to autowire the dependencies of class, the class itself should be manged by spring. Another way around will be to write a method annotated with @Bean, and inside that method you can create object of class A with any constructor of your choice with default values, this way you autowire class A as well in your test instead of using new keyword. You don't have to then annotate class A with @Component.

@Bean
public A getA() {
   return new A(null,null,null,null);
}

You can write above code in a class annotated with @Configuration, so that spring will read and create of object of A and put it in container.

You can use both XML & annotation based configuration at same time. Just need to add a tag in sprint.xml (Please search tag on google)

In your test you can say

@Autowired
private A aObject
Rohit
  • 146
  • 1
  • 5