如何在Spring引导应用程序中使用配置文件?
我想在Spring引导应用程序中使用配置文件。我不知道怎么做 如何修改以下代码如何在Spring引导应用程序中使用配置文件?,spring,spring-boot,Spring,Spring Boot,我想在Spring引导应用程序中使用配置文件。我不知道怎么做 如何修改以下代码 package com.example.demo; import org.springframework.stereotype.Component; @Component public class EnglishGreeting implements HelloWorldService { @Override public String greeting() { // TODO A
package com.example.demo;
import org.springframework.stereotype.Component;
@Component
public class EnglishGreeting implements HelloWorldService {
@Override
public String greeting() {
// TODO Auto-generated method stub
return "hello world";
}
}
package com.example.demo;
import org.springframework.stereotype.Component;
@Component
public class SpanichGreeting implements HelloWorldService {
@Override
public String greeting() {
// TODO Auto-generated method stub
return "hola monda";
}
}
有什么建议吗?如果您打算根据配置文件选择一个实现,则需要添加注释
@Component
@Profile("en")
public class EnglishGreeting implements HelloWorldService
@Component
@Profile("es")
public class SpanichGreeting implements HelloWorldService {
使用
-Pes
运行程序将启用es
配置文件,并且在自动连线时将使用西班牙语实现。除了添加@profile(“en”)
和@profile(“es”)
之外,您必须创建名为application-en.properties和application-es.properties的属性文件
可以在每个文件中创建属性,例如:
application en.properties
greeting.text=hello world
greeting.text=hola monda
应用程序属性
greeting.text=hello world
greeting.text=hola monda
在您的服务中添加一个接受该值的变量
像这样:
@Component
public class SpanishGreeting implements HelloWorldService {
@Value("${greeting.text})"
String greeting
@Override
public String greeting() {
// TODO Auto-generated method stub
return this.greeting;
}
}
有关
@Value
的更多信息,请点击此处:您似乎没有对代码中的配置文件进行任何操作,因此我们也不了解问题所在。到底出了什么问题?