Ja. Wenn Sie eine Datei application.properties
in src/main/resources/
benannter können Sie ihre Werte injizieren etwa so:
application.properties
foo=bar
Foo.class
@Component
public class Foo {
@Value("${foo}")
String foo; //equals 'bar'
//.......//
}
Alternativ können Sie nachschlagen Eigenschaften programmgesteuert
@SpringBootApplication
public class PropertyLookupApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(PropertyLookupApplication.class, args);
String foo = context.getEnvironment().getProperty("foo"); //equals 'bar'
}
}
aktualisieren
Wenn Sie application.properties
als Map
wollen, dann können Sie sicherstellen, dass PropertiesLoaderUtils
Dienstprogramm Klasse von Spring:
Resource resource = new ClassPathResource("application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
, dass ein Standard java.util.Properties
Objekt zurückgibt, die eine Karte ist.
Ich bin mir nicht sicher, was Sie versuchen zu tun. Sie können jedoch die Datei application.properties oder application.yml verwenden, um Eigenschaften im Spring-Boot anzugeben. Wenn Sie das Projekt in einer IDE mit dem Initialisierer spring.io erstellen, werden application.yml und bootstrap.yml automatisch im Verzeichnis src/java/resources erstellt. – techtabu