Reputation: 61
I have below project structure
My code files are as belows
DemoApplication.java
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
CarListGrabber grabber = new CarListGrabber();
grabber.grabCarsWithName("Test");
}
}
NewCarRepository.java
@Repository
public interface NewCarRepository extends CrudRepository<NewCar, Long> {
}
NewCar.java
@Entity
@Table(name = "new_car_details")
public class NewCar {
// member variables with default constructor
}
CarListGrabber.java
@Service
public class CarListGrabber {
@Autowired
private NewCarRepository newCarRepository;
// someOtherStuff
}
Even though I have used annotations @Repository, @Service I am getting null repository object in service.
Upvotes: 0
Views: 556
Reputation: 12245
You are instantiating a new CarListGrabber
with:
CarListGrabber grabber = new CarListGrabber();
it will not make injections you need to inject also your grabber
, like:
@Autowired
CarListGrabber grabber;
Upvotes: 1