英文:
Spring bean properties of imported Jar are overridden by application bean definition
问题
我在导入的JAR文件中有一个同名的类。
@Configuration
@ConfigurationProperties(prefix = "queues")
public class QueueProperties {
String queuename;
String queuemanager;
//Rest code
}
同样的类在JAR文件中也有相同的属性。
我在配置文件中为JAR的bean定义了一个bean。
@Bean
public com.jar.class.path getQueueProperties() {
return new com.jar.class.path.QueueProperties();
}
但是当应用程序启动时,它使用的是我的bean类的属性,而不是为JAR类bean定义的属性。
属性值保存在application.yml文件中。
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
jar:
class:
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
我想使用yml中为JAR文件定义的属性。这个问题有解决方法吗?
英文:
I have a class with same name in the imported jar file.
@Configuration
@ConfigurationProperties(prefix = "queues")
public class QueueProperties {
String queuename;
String queuemanager;
//Rest code
}
The same class with the same properties also there in the jar file.
I have given bean definition in my configuration file for the jar bean.
@Bean
public com.jar.class.path getQueueProperties() {
return new com.jar.class.path.QueueProperties();
}
But when the application started it is using the properties of my bean class instead of the properties defined for jar class bean.
Property values are kept in application.yml file.
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
jar:
class:
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
I want to use properties defined in yml for jar file bean. Could there be any solution around this?
答案1
得分: 0
你可以使用Spring配置文件。
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
---
spring:
profiles: jar
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
你可以在命令行中使用 --spring.profiles.active=jar
来使用你的jar配置。
你不应该将配置直接定义为普通的Bean,而应该像这样做:
@Configuration
public class MyConfiguration {
@Bean
@ConfigurationProperties(prefix = "queue")
public com.jar.class.path getQueueProperties() {
return new com.jar.class.path.QueueProperties();
}
}
public class QueueProperties {
String queuename;
String queuemanager;
//Rest code
}
英文:
You can use Spring profile.
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
---
spring:
profiles: jar
queue:
queueManager: 'queuemanager'
queuename: 'queuename'
You can use --spring.profiles.active=jar on command line to use your jar profile.
You should not use configuration as normal beans, do this:
@Configuration
public class MyConfiguration {
@Bean
@ConfigurationProperties(prefix = "queue")
public com.jar.class.path getQueueProperties() {
return new com.jar.class.path.QueueProperties();
}
}
public class QueueProperties {
String queuename;
String queuemanager;
//Rest code
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论