0

Spring boot 2.1.4 Autowired works well during startup but not when create new Pojo instance, see code below

Pojo

@Component
public class Pojo {

private ConfigController c;
@Autowired
public void setProperty(ConfigController cp) { this.c = cp; }     

public Pojo () {};

public String getVal()  { return c.geItem1(); }

}

ConfigController class definition

@Configuration
@Component
public class ConfigController {

private String item1;
private String item2;

public ConfigController () {};

public String geItem1() 
    { return this.item1; }

public void setItem1(String s) 
    { this.item1 = s; }

Main

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

@Autowired
private ConfigController c;

public static void main(String[] args) {
    SpringApplication.run(SpringBootConsoleApplication.class, args);
}

@Override
public void run(String... args) {
    Pojo p = new Pojo();
    System.println(p.getVal()); // Runtime error here with java.lang.NullPointerException

}
}

p seems to be instantiated but @Autowired doesn;t seem to be working.

Any clue what I am not doing right?

Additional Note Thank you for your replies - I understand that the pojo has the be instantiated by Spring - thanks for the clarification, I get it now. Having said this, I have exactly the same issue with the RestController - see below. p is automatically mapped by Spring to a new Pojo but the configuration in this pojo is not autowired.

@RestController
public class MyController {

@GetMapping("/my-end-point")
OtherPojo getOtherPojo(@RequestBody Pojo p) 
{
System.println(p.getVal()); // Runtime error here with java.lang.NullPointerException
// ... more code
}
Ioan
  • 1
  • 1
  • Of course it won't. Spring will only inject into instances it creates, if you create instances outside the scope of Spring (like you do) nothing will be injected. – M. Deinum Apr 24 '19 at 05:33
  • Possible duplicate of [SpringBootApplication does not Autowire my Services](https://stackoverflow.com/questions/55078382/springbootapplication-does-not-autowire-my-services) – Ryuzaki L Apr 24 '19 at 16:25
  • Understood, but I have the same problem with the RestController - see an example of code below that also ends with Null Pointer. In any event, thanks for your help! – Ioan Apr 25 '19 at 15:50

3 Answers3

0

the thing you have to consider is when you want to use spring dependency injection always let spring container create the object , by @Autowire or from 'SpringContext' the other way it won't work . to solve your problem here you must inject Pojo in run method too With @Autowire.

mehran qorbani
  • 61
  • 1
  • 2
  • 8
  • Thanks for your answer, it works indeed in the main. As a matter of fact, my original problem is with the rest controller - see below - I assume this code is in the Spring context? p gets instantiated automatically by Spring when I pass the correct JSON but the configuration is not injected – Ioan Apr 25 '19 at 15:45
0

your approach is incorrect. With your initial question - Part 1: You are creating new Pojo() yourself. So it won't auto inject yeah? I guess you got that already.

Part2: You are creating Pojo from your HTTP request body and not from the container anyway. It means that you have a Json request with structure of Pojo and you are expecting it to Autowire itself. Back to basics: Autowire works only when you Autowire or inject the component or the parent object with in turn injects the child component. Correct your Json request!

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

I think I get it now - the IoC will keep track of components created within the IoC container at startup - everything else is not Thank you all Ioan

Ioan
  • 1
  • 1