有人可以直接在@ConfigurationProperties
方法上提供关于如何使用@Bean
注释的MWE吗?
我已经看到无数的例子用于类定义 - 但还没有@Bean
方法的例子。
@Bean
方法 所以,我认为有一种可能性和预期用途 - 但不幸的是我无法弄明白。
答案 0 :(得分:22)
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return new DataSource();
}
这里DataSource类有proprties url,username,password,driverClassName,因此spring boot将它们映射到创建的对象。
DataSource类的示例:
public class DataSource {
private String url;
private String driverClassName;
private String username;
private String password;
//getters & setters, etc.
}
换句话说,这与使用构造型注释初始化某些bean具有相同的效果(@ Component,@ Service等) e.g。
@Component
@ConfigurationProperties(prefix="spring.datasource")
public class DataSource {
private String url;
private String driverClassName;
private String username;
private String password;
//getters & setters, etc.
}
答案 1 :(得分:0)
24.8.1 Third-party Configuration
除了使用function custom_wc_translations( $translated_text, $text, $domain ) {
if(is_checkout()){
switch ($translated_text) {
case 'Your order' :
$translated_text = "Your Order <a class='ck-order-cart-link' href='/cart/'>Edit Cart</a>";
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'custom_wc_translations', 20, 3 );
注释类之外,您还可以在公共@ConfigurationProperties
方法上使用它。当您想将属性绑定到控件之外的第三方组件时,这样做特别有用。
要通过Environment属性配置Bean,请在其Bean注册中添加@Bean
,如以下示例所示:
@ConfigurationProperties
使用另一个前缀定义的任何属性都以类似于前面的AcmeProperties示例的方式映射到该AnotherComponent bean。
答案 2 :(得分:0)
您可以按以下方式使用@ConfigurationProperties
实体模型
public class MY_ENTITY {
private String prop1;
private String prop2;
// setter & getter & toString()
}
Bean方法
@Configuration
public class MyClass {
@Bean
@ConfigurationProperties(prefix = "my.entity")
public MY_ENTITY getContract() {
return new MY_ENTITY()
.setProp1("prop1111111")
.setProp2("prop2222222")
;
}
@Bean(name = "contract2")
@ConfigurationProperties(prefix = "my.entity2")
public MY_ENTITY getContract2() {
return new MY_ENTITY()
.setProp1("prop1111.2222")
.setProp2("prop2222.222")
;
}
}
application.properties
my.entity.prop1=2120180023
my.entity.prop2=CUSTOMER_NAME111
my.entity2.prop1=9994494949
my.entity2.prop2=CUSTOMER_NAME222
SpringBootApplication
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
@Qualifier("contract2")
private MY_ENTITY myEntity;
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(myEntity);
}
}
答案 3 :(得分:0)
我找到了以下解决方案:即我们在应用程序yaml中有几节,而在appConfig中我们很有趣:
appConfig:
version: 1.0_alpha
environment: ${spring.profiles}
dbDriver: ${spring.datasource.driver-class-name}
dbUrl: ${spring.datasource.url}
keyCloak:
serverOne:
host: http://xx.xx.xxx.xxx:8080
baseUrl: ${appConfig.keyCloak.serverOne.host}/auth/realms/master
clientId: api-service-agent
clientSecret: f00955443-d123-4cfe-90d3-e3ff3b214aaffe
serviceUsername: service-user
servicePassword: 1234567890
serverTwo:
host: http://xx.xxx.xxx.xxx:8080
baseUrl: ${appConfig.keyCloak.serverTwo.host}/auth/realms/wissance
clientId: api-service-agent
clientSecret: a20ddf0-56fa-4991-85bc-114377eeffddcc
serviceUsername: service-user
servicePassword: 1234567890
using:
baseUrl: ${appConfig.keyCloak.serverTwo.baseUrl}
clientId: ${appConfig.keyCloak.serverTwo.clientId}
clientSecret: ${appConfig.keyCloak.serverTwo.clientSecret}
serviceUsername: ${appConfig.keyCloak.serverTwo.serviceUsername}
servicePassword: ${appConfig.keyCloak.serverTwo.servicePassword}
我们想拆分常用设置并使用KeyCloak设置,因此我实现了以下方案:
我制作以下KeyCloakConfig类(不带@ConfigurationProperties批注)以使用身份验证服务器设置进行存储:
@Configuration
public class KeyCloakConfig {
public KeyCloakConfig(){
}
public KeyCloakConfig(String baseUrl, String clientId, String clientSecret, String username, String password) {
this.baseUrl = baseUrl;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.username = username;
this.password = password;
}
public String getBaseUrl(){
return baseUrl;
}
public void setBaseUrl(String baseUrl){
this.baseUrl = baseUrl;
}
public String getClientId(){
return clientId;
}
public void setClientId(String clientId){
this.clientId = clientId;
}
public String getClientSecret(){
return clientSecret;
}
public void setClientSecret(String clientSecret){
this.clientSecret = clientSecret;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
@Value("${appConfig.keyCloak.using.baseUrl}")
private String baseUrl;
@Value("${appConfig.keyCloak.using.clientId}")
private String clientId;
@Value("${appConfig.keyCloak.using.clientSecret}")
private String clientSecret;
@Value("${appConfig.keyCloak.using.serviceUsername}")
private String username;
@Value("${appConfig.keyCloak.using.servicePassword}")
private String password;
}
和AppConfig类,它们使用版本驱动程序和url以及KeyCloakConfig作为属性来保存通用设置,例如版本,环境:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class AppConfig {
public AppConfig(){
}
public AppConfig(String apiVersion, String environment, String databaseDriver, String databaseUrl){
this.apiVersion = apiVersion;
this.environment = environment;
this.databaseDriver = databaseDriver;
this.databaseUrl = databaseUrl;
}
public String getEnvironment(){
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
public String getDatabaseDriver(){
return databaseDriver;
}
public void setDatabaseDriver(String databaseDriver) {
this.databaseDriver = databaseDriver;
}
public String getDatabaseUrl(){
return databaseUrl;
}
public void setDatabaseUrl(String databaseUrl) {
this.databaseUrl = databaseUrl;
}
public String getApiVersion(){
return apiVersion;
}
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
public KeyCloakConfig getKeyCloakConfig(){
return keyCloakConfig;
}
public void setKeyCloakConfig(KeyCloakConfig keyCloakConfig){
this.keyCloakConfig = keyCloakConfig;
}
@Value("${appConfig.version}")
private String apiVersion;
@Value("${appConfig.environment}")
private String environment;
@Value("${appConfig.dbDriver}")
private String databaseDriver;
@Value("${appConfig.dbUrl}")
private String databaseUrl;
@Autowired
private KeyCloakConfig keyCloakConfig;
}