Spring 引导:自动装配错误
Spring Boot: Autowired error
我正在尝试创建一个简单的 Spring 引导应用程序,它将名称作为 URL 中的参数(例如 http://localhost:8080/hello/john),并以英文或德语问候语页面("Hello John" 或 "Hallo John")。
我在 Ubuntu 14.04 下使用 IntelliJ Idea 2017.3.3。
为此,我创建了一个 Spring Intializr 项目和一个名为 GreetingService 的接口:
package com.springboot.configuration.service;
import org.springframework.stereotype.Component;
@Component
public interface GreetingService {
String sayHello(String name);
}
该接口将在两个类、GrretingServiceEnglish和GreetingServiceGerman中实现如下:
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;
}
}
和
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;
}
}
要选择必须使用哪个,application.properties 中有一个条目:
spring.profiles.active="english"
我在控制器中进行了自动装配:
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);
}
}
申请是:
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);
}
}
当我运行应用程序时出现错误:
com.springboot.configuration.controller.GreetingController 中的字段 greetingService 需要找不到类型 'com.springboot.configuration.service.GreetingService' 的 bean。
怎么了?你能帮帮我吗?
您的活动配置文件设置不正确 - 您没有在 application.properties
文件中引用字符串。在这种情况下,您的活动配置文件实际上是 `"english"。如果您将组件 class 注释为:
就可以了
@Profile("\"english\"")
在控制台日志中可以看到:
2018-01-18 10:43:51.322 INFO 23212 --- [ main] c.s.configuration.ProfilesApplication : The following profiles are active: "english"
解决方案
只需在 application.properties
中取消引用 spring.profiles.active
:
spring.profiles.active=english
它会如您所愿地工作。
2018-01-18 10:47:31.155 INFO 890 --- [ main] c.s.configuration.ProfilesApplication : The following profiles are active: english
关于 i18n 的一句话
我想这个项目只是一个测试 Spring 引导功能(如配置文件等)的游乐场。如果您对国际化您的应用程序感兴趣,请使用 LocaleChangeInterceptor
之类的东西,它使用 messages_en.properties
+ messages_de.properties
来定义您的翻译并在单个组件中使用它们。你可以在这里找到一个很好的介绍:http://www.baeldung.com/spring-boot-internationalization
我正在尝试创建一个简单的 Spring 引导应用程序,它将名称作为 URL 中的参数(例如 http://localhost:8080/hello/john),并以英文或德语问候语页面("Hello John" 或 "Hallo John")。
我在 Ubuntu 14.04 下使用 IntelliJ Idea 2017.3.3。
为此,我创建了一个 Spring Intializr 项目和一个名为 GreetingService 的接口:
package com.springboot.configuration.service;
import org.springframework.stereotype.Component;
@Component
public interface GreetingService {
String sayHello(String name);
}
该接口将在两个类、GrretingServiceEnglish和GreetingServiceGerman中实现如下:
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;
}
}
和
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;
}
}
要选择必须使用哪个,application.properties 中有一个条目:
spring.profiles.active="english"
我在控制器中进行了自动装配:
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);
}
}
申请是:
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);
}
}
当我运行应用程序时出现错误:
com.springboot.configuration.controller.GreetingController 中的字段 greetingService 需要找不到类型 'com.springboot.configuration.service.GreetingService' 的 bean。
怎么了?你能帮帮我吗?
您的活动配置文件设置不正确 - 您没有在 application.properties
文件中引用字符串。在这种情况下,您的活动配置文件实际上是 `"english"。如果您将组件 class 注释为:
@Profile("\"english\"")
在控制台日志中可以看到:
2018-01-18 10:43:51.322 INFO 23212 --- [ main] c.s.configuration.ProfilesApplication : The following profiles are active: "english"
解决方案
只需在 application.properties
中取消引用 spring.profiles.active
:
spring.profiles.active=english
它会如您所愿地工作。
2018-01-18 10:47:31.155 INFO 890 --- [ main] c.s.configuration.ProfilesApplication : The following profiles are active: english
关于 i18n 的一句话
我想这个项目只是一个测试 Spring 引导功能(如配置文件等)的游乐场。如果您对国际化您的应用程序感兴趣,请使用 LocaleChangeInterceptor
之类的东西,它使用 messages_en.properties
+ messages_de.properties
来定义您的翻译并在单个组件中使用它们。你可以在这里找到一个很好的介绍:http://www.baeldung.com/spring-boot-internationalization