3

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?

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

As of Spring Framework 4.3, an Autowired annotation on such a constructor is no longer necessary if the target bean defines only one constructor, to begin with. However, if several constructors are available and there is no primary/default constructor, at least one of the constructors must be annotated with Autowired in order to instruct the container which one to use. See the discussion on constructor resolution for details. docs.spring.io/spring-framework/docs/current/reference/html/…
1

There are 2 issues with what you are trying

  1. 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; } 
  2. 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; } } 

1 Comment

I've already tried with Autowired, but Spring says it cannot find a bean for eventService. The only way I get it (almost) working is annotating MyEventService with Service: in that case the problem is withe baseURL. In a previous project I had all the beans defined in a xml file and no annotations and it worked fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.