I'm trying to inject a bean into a Controller but looks like Spring is not using the bean.xml file.
Here is the code:
The controller
@RestController
public class AppController {
private final EventService eventService;
private List<String> categories;
public AppController(final EventService eventService) {
this.eventService = eventService;
}
}
The interface of the object to inject
public interface EventService {
// some methods
}
Its implementation
public class MyEventService {
private final String baseURL;
public MyEventService(String baseURL){
this.baseURL = baseURL;
}
}
If I annotate MyEventService with @Service Spring tries to inject it into the Controller but complains about baseURL not being provided (No qualifying bean of type 'java.lang.String' available). So I created a bean.xml file under src/main/resources
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="eventService" class="xyz.MyEventService">
<constructor-arg type="java.lang.String" value="fslkjgjbflgkj" />
</bean>
<bean id="categories" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>music</value>
<value>comedy</value>
<value>food</value>
</list>
</constructor-arg>
</bean>
</beans>
but that doesn't seem to work as if I remove @Service from MyEventService Spring complains about not finding a bean for eventService.
Am I missing something?
Spring Boot heavily relies on Java Config.
Check the docs on how to declare @Component, @Service etc
In your case:
@Service
public class MyEventService implements EventService {
private final String baseURL;
@Autowired
public MyEventService(Environment env){
this.baseURL = env.getProperty("baseURL");
}
}
And in you /src/main/resources/application.properties
baseURL=https://baseUrl
And then
@RestController
public class AppController {
private final EventService eventService;
@Autowired
public AppController(final EventService eventService) {
this.eventService = eventService;
}
}
The @Autowired will tell Spring Boot to look in the components you declared with @Component, @Service, @Repository, @Controller etc
To inject the categories, I really suggest you declared a CategoriesService (with @Service) that gets the categories from a config file or just hardcode them in the CategoriesService class (for prototyping).
There are 2 issues with what you are trying
The reason your xml bean definition is not picked up is because you didn't injected the service in you controller,since you are using annotation for your controller you need to tell you controller how the service needs to be injected, to fix this just autowire your service
@RestController
public class AppController {
@Autowired
private final EventService eventService;
}
When your are using annotation for construct-arg based spring bean you need to set value as well, like you are doing in your xml.
public class MyEventService {
private final String baseURL;
public MyEventService(@Value("#{your value here")String baseURL){
this.baseURL = baseURL;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With