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?