我使用的是Spring Boot,并且有两个非常相似的服务,我想在application.yml
中配置。
配置看起来大致如下:
serviceA.url=abc.com
serviceA.port=80
serviceB.url=def.com
serviceB.port=8080
是否可以创建一个用@ConfigurationProperties
注释的类并在注入点设置前缀?
e.g。
@Component
@ConfigurationProperties
public class ServiceProperties {
private String url;
private String port;
// Getters & Setters
}
然后在服务本身:
public class ServiceA {
@Autowired
@SomeFancyAnnotationToSetPrefix(prefix="serviceA")
private ServiceProperties serviceAProperties;
// ....
}
很遗憾,我还没有在文档中找到有关此类功能的内容......非常感谢您的帮助!
答案 0 :(得分:4)
我实现了与你尝试的几乎相同的事情。 首先,注册每个属性bean。
@Bean
@ConfigurationProperties(prefix = "serviceA")
public ServiceProperties serviceAProperties() {
return new ServiceProperties ();
}
@Bean
@ConfigurationProperties(prefix = "serviceB")
public ServiceProperties serviceBProperties() {
return new ServiceProperties ();
}
并且在服务中(或将使用属性的某个地方)放置@Qualifier并指定哪个属性将自动连接。
public class ServiceA {
@Autowired
@Qualifier("serviceAProperties")
private ServiceProperties serviceAProperties;
}
答案 1 :(得分:2)
在这篇文章Guide to @ConfigurationProperties in Spring Boot之后,您可以创建一个没有注释的简单类:
- (id)initWithName:(NSString*)newName
{
PYMIDIManager* manager = [PYMIDIManager sharedInstance];
MIDIEndpointRef newEndpoint;
OSStatus error;
SInt32 newUniqueID;
// This makes sure that we don't get notified about this endpoint until after
// we're done creating it.
[manager disableNotifications];
MIDIDestinationCreate ([manager midiClientRef], (CFStringRef)newName, midiReadProc, self, &newEndpoint);
// This code works around a bug in OS X 10.1 that causes
// new sources/destinations to be created without unique IDs.
error = MIDIObjectGetIntegerProperty (newEndpoint, kMIDIPropertyUniqueID, &newUniqueID);
if (error == kMIDIUnknownProperty) {
newUniqueID = PYMIDIAllocateUniqueID();
MIDIObjectSetIntegerProperty (newEndpoint, kMIDIPropertyUniqueID, newUniqueID);
}
MIDIObjectSetIntegerProperty (newEndpoint, CFSTR("PYMIDIOwnerPID"), [[NSProcessInfo processInfo] processIdentifier]);
[manager enableNotifications];
self = [super initWithMIDIEndpointRef:newEndpoint];
ioIsRunning = NO;
return self;
}
然后使用@Bean批注创建@Configuration类:
public class ServiceProperties {
private String url;
private String port;
// Getters & Setters
}
最后,您可以得到如下属性:
@Configuration
@PropertySource("classpath:name_properties_file.properties")
public class ConfigProperties {
@Bean
@ConfigurationProperties(prefix = "serviceA")
public ServiceProperties serviceA() {
return new ServiceProperties ();
}
@Bean
@ConfigurationProperties(prefix = "serviceB")
public ServiceProperties serviceB(){
return new ServiceProperties ();
}
}
答案 2 :(得分:0)
除了JavaBean验证之外,Javvanos的示例正常运行。
我在其中一个属性上添加了@NotNull注释:
public class ServiceProperties {
@NotNull
private String url;
private String port;
// Getters & Setters
}
结果,应用程序启动失败并显示以下错误消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target ch.sbb.hop.commons.infrastructure.hadoop.spark.SparkJobDeployerConfig@730d2164 failed:
Property: url
Value: null
Reason: may not be null
Action:
Update your application's configuration
删除注释后,应用程序将使用正确的属性绑定启动。 总之,我认为JavaBean验证存在一个问题,即未获得正确初始化的实例,这可能是由于缺少配置方法上的代理所致。
答案 3 :(得分:-2)
@ConfigurationProperties anotation具有设置前缀配置的字段。这是我的例子:
@Component
@ConfigurationProperties(prefix = "b2gconfig")
public class B2GConfigBean {
private String account;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
private String key;
}