I am new to spring boot.
I am trying to understand Dependency Injection. I created a @Component
bean and try to get it's instance object into a simple POJO class.
Let's take a look at example:
// Planet.java
@Component
class Planet() {
private Long planetId;
private String planetName;
// ... getter-setter and constructor
}
since Planet object is annotated with @Component so it will be created by spring context container. Now I have created a simple POJO class....
// Species.java
class Species() {
private Long spId;
private String spName;
// I want to get the object of Planet from container
@Autowired
private Planet planet;
// getter-setter and constructor
}
Now when I create my Species object with new keyword(new Species()
) then...
- Planet Object is created by container as expected.
- But In my Species object planet property instead of referencing Planet object it reference to a null value (not expected)..
Since I am Autowiring planet property, it should get that object from container but It is not getting that object. Please let me know Where I am doing something wrong..
I also tried to create beans using @Configuaration
but still it's not working..