春季导入的Jar的bean属性被应用程序bean定义覆盖。

huangapple go评论69阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2020年9月4日 14:12:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63735769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定