I am trying to understand how the Autowired works with really basic examples... when it comes to typical controller/service/dao it works as expected but when I wanted to create sth else I've been struggling with some NullPointers...
I have two classes: Car, Engine and starting one.
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Car {
private int power;
@Autowired
Engine engine;
public String showEngineName() {
return engine.getName();
}
}
@Getter
@Setter
@Component
public class Engine {
private String name = "super engine";
public String getName() {
return name;
}
}
@SpringBootApplication
public class EduApplication {
public static void main(String[] args) {
Car car = new Car();
String engineName = car.showEngineName();
System.out.println(engineName);
SpringApplication.run(EduApplication.class, args);
}
}
When I start the application, the car is initialized but the engine inside is null... Can someone explain to me why? Shouldn't be there an engine with name, super engine''?
Regards!