Reputation: 23
I'm just starting off with Spring , I have created a basic Spring boot web app with - Thymeleaf templates, JPA and Mysql.
When I use the autowiring and annotate all the classes everything works. Here is an example of the working code.
Simple product class with the @Component annotation
Simple DAO class with the @service annotation
Basic controller with the @Autowired annotation to get DAO instance
So when I call the get method , I'm able to see a list of products: 2 products displayed
Now when I transition to the java configuration , this stops working. I commented out the annotations and added a new java config file.
the @Autowiring annotation also removed from the Controller
Added a java class with the @Configuration, see config file:
Just to make sure the context loaded the beans, I took the configurable context that was returned by the run method and checked if the bean was loaded.
Get the DAO bean and call the method to get the Product line items
This also works as I could see the object was not null. Also I could get a count of the products in the output immediately after the application was started.
Everything is ok till here and the application has started. But now when I access the URL - I get an error
Summary This example is only being done to understand Spring and is not following proper DDD practices.
Here is a summary of the steps, the issues are in step 4(b)
Upvotes: 0
Views: 663
Reputation: 7131
Declare a constructor for HomeController
as follows
@Controller
public class HomeController{
private final ProductDao dao;
public HomeController(ProductDao dao){
this.dao = dao;
}
//... rest of the code
}
Reference : Spring Boot Reference Documentation - Spring Beans and Dependency Injection
If a bean has one constructor, you can omit the @Autowired
Note : Share actual code instead of images for reference.
Upvotes: 1