[英]SpringBoot Configuration with application.yml
I have a little SpringBoot Application, which can execute different functions via OpenLdap. 我有一个小的SpringBoot应用程序,可以通过OpenLdap执行不同的功能。
getUser
createUser
deleteUser
etc.
That works fine. 很好 Now i want to create an application.Yml, where i can manage different environments with different credentials.
现在,我想创建一个application.Yml,可以在其中使用不同的凭据管理不同的环境。 I read some tutorials, but i still have some understanding problems.
我读了一些教程,但是我仍然有一些理解上的问题。 Actually my code looks like that:
实际上我的代码如下所示:
UserController: UserController:
...
protected static String serverURL = "xxxxx:90xx";
protected static String LdapBindDn = "cn=admin, xxxxx";
protected static String LdapPassword = "xxxx";
...
@RequestMapping(value = "/{userid:.+}",method = RequestMethod.GET,consumes="application/json",produces = "application/json")
public UserData getUser(@PathVariable String userid) {
DirContext context = connectToLdap();
//some operations...
context.close();
return user;
}
... // same for the other functions
My plan is now, that i want to specify the credentials in an extra application.yml instead of at the beginning of the UserController (see above). 我现在的计划是,我想在额外的application.yml中指定凭据,而不是在UserController的开头(请参见上文)。
Then i have created an application.yml in the src/main/resources: 然后我在src / main / resources中创建了一个application.yml:
# Actual environment
spring:
profiles.actives: development
---
# Dev Profile
spring:
profiles: dev
datasource:
serverUrl: ldaps://xxxxxx:90xx
AdminName: xxxx
AdminPassword: xxxxxx
BaseDN: xxxxx
---
# Production Profile
spring:
profiles: prod
datasource:
serverUrl: ldaps://xxxx2:90xx
AdminName: xxxxx2
AdminPassword: xxxxx2
BaseDN: xxxxxx
Now i need to call this configuration. 现在,我需要调用此配置。 I have read in one tutorial ( http://therealdanvega.com/blog/2017/06/26/spring-boot-configuration-using-yaml ) that i have to create an extra class "ApplicationProperties" for the properties of the .yml file.
我已经阅读了一个教程( http://therealdanvega.com/blog/2017/06/26/spring-boot-configuration-using-yaml ),必须为该类的属性创建一个额外的类“ ApplicationProperties”。 yml文件。
@Component
@ConfigurationProperties("datasource")
public class ApplicationProperties {
private String serverURL;
private String adminName;
private String adminPassword;
private String baseDN;
// Getter-/Setter-Methods
} }
Now i need to define my variables from the beginning with the values from the .yml, right? 现在,我需要使用.yml中的值从头开始定义变量,对吗? I went back to my UserController and tried something like that:
我回到我的UserController并尝试了类似的方法:
private String serverURL;
private String adminName;
private String adminPassword;
private String baseDN;
@Autowired
ApplicationProperties appProp;
@RequestMapping(value = "/{userid:.+}",method = RequestMethod.GET,consumes="application/json",produces = "application/json")
public UserData getUser(@PathVariable String userid) {
DirContext context = connectToLdap();
//some operations...
context.close();
return user;
}
... // same for the other functions
private DirContext connectToLdap(){
System.out.prinln(appProp.getServerURL());
System.out.prinln(appProp.getAdminName());
System.out.prinln(appProp.getAdminPassword());
.... // Code for the Ldap connection
}
But the variable "appProp" is still empty. 但是变量“ appProp”仍然为空。 I know, that here is somewhere a big understanding problem.
我知道,这是一个很大的理解问题。 I don't know how to call these properties from the .yml file.
我不知道如何从.yml文件调用这些属性。
Thanks for every help in advance! 预先感谢您的每一个帮助!
You can get properties from your .yml
file by creating a config class: 您可以通过创建配置类从
.yml
文件中获取属性:
application.yml
datasource:
serverUrl: ldaps://xxxxxx:90xx
adminName: xxxx
adminPassword: xxxxxx
baseDN: xxxxx
ApplicationConfig.java
class : ApplicationConfig.java
类:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "datasource")
public class ApplicationConfig {
private String serverUrl;
private String adminName;
private String adminPassword;
private String baseDN;
//getters setters
}
Then you can call your ApplicationConfig
class 然后,您可以调用
ApplicationConfig
类
@Autowired
public ApplicationConfig app;
public UserData getUser(@PathVariable String userid) {
DirContext context = connectToLdap();
//some operations...
appProp.getServerUrl();
appProp.getAdminName();
context.close();
return user;
}
And I recommend you to create profile based properties as spring boot picks them automatically, like application-{profile}.{properties|yml}
You can create application-production.yml
file and set your profile by adding @Profile("production")
annotation in your class. 并且我建议您创建基于配置文件的属性,因为Spring Boot会自动选择它们,例如
application-{profile}.{properties|yml}
您可以创建application-production.yml
文件并通过添加@Profile("production")
设置您的配置文件您班上的注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.