Sandman42
Sandman42

Reputation: 75

Spring Boot: Autowired error

I'm trying to create a simple Spring Boot application, which takes the name as a parameter in the URL (say http://localhost:8080/hello/john) and greets with an English or a German greeting page ("Hello John" or "Hallo John").

I'm using IntelliJ Idea 2017.3.3 under Ubuntu 14.04.

To do that I've created a Spring Intializr project, and an interface called GreetingService:

package com.springboot.configuration.service;

import org.springframework.stereotype.Component;

@Component
public interface GreetingService {
    String sayHello(String name);
}

This interface will be implemented in two classes, GrretingServiceEnglish and GreetingServiceGerman as follows:

package com.springboot.configuration.service;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("english")
public class GreetingServiceEnglish implements GreetingService{

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

and

package com.springboot.configuration.service;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("german")
public class GreetingServiceGerman implements GreetingService{

    @Override
    public String sayHello(String name) {
        return "Hallo " + name;
    }
}

To choose which one has to be used, there's an entry in application.properties:

spring.profiles.active="english"

and in a controller I do an Autowiring:

package com.springboot.configuration.controller;

import com.springboot.configuration.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/", method = RequestMethod.GET)
public class GreetingController {

    @Autowired
    private GreetingService greetingService;

    @RequestMapping(path = "hello/{name}")
    public String sayHello(@PathVariable(name = "name") String name){

        return greetingService.sayHello(name);
    }
}

The Application is:

package com.springboot.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProfilesApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProfilesApplication.class, args);
    }
}

When I run the application I have an error:

Field greetingService in com.springboot.configuration.controller.GreetingController required a bean of type 'com.springboot.configuration.service.GreetingService' that could not be found.

What's wrong? Can you help me please?

Upvotes: 1

Views: 148

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42184

Your active profile setting is not correct - you don't quote strings in application.properties file. In this case your active profile is literally `"english". It would be OK if you annotate your component class with:

@Profile("\"english\"")

You can see it in the console log:

2018-01-18 10:43:51.322  INFO 23212 --- [           main] c.s.configuration.ProfilesApplication    : The following profiles are active: "english"

Solution

Simply unquote spring.profiles.active in your application.properties:

spring.profiles.active=english

and it will work as you expect.

2018-01-18 10:47:31.155  INFO 890 --- [           main] c.s.configuration.ProfilesApplication    : The following profiles are active: english

One word about i18n

I guess this project is just a playground for testing Spring Boot features (like profiles etc.). If you are interested in internationalizing your application use things like LocaleChangeInterceptor that uses messages_en.properties + messages_de.properties to define your translations and use them in a single component. You can find a good introduction here: http://www.baeldung.com/spring-boot-internationalization

Upvotes: 1

Related Questions