2015-03-19 25 views
5

Come dice il titolo, sto tentando di utilizzare Typesafe Configuration Properties per caricare un elenco di oggetti DataSourceConfig. Ho Lombok per setter/getterAvvio a molla @ConfigurationProperties non caricato

L'applicazione principale annotazioni di classe

@Slf4j 
@SpringBootApplication 
@EnableConfigurationProperties 
public class Application { 

La POJO configurazione

@Data 
public class DataSourceConfig { 
    private String key; 
    private String dbname; 
    private String dbpath; 
} 

Il file yml

tenantdb: 
    dataSourceConfig: 
     - 
      key: default 
      dbpath: file:eventstore/jdbc/database 
      dbname: defaultdb 
     - 
      key: other 
      dbpath: file:eventstore/jdbc/other 
      dbname: dslfjsdf 

Infine, la classe di configurazione primavera con l'annotazione @ConfigurationProperties.

@Configuration 
@Profile("hsqldb") 
@ImportResource(value = { "persistence-config.xml" }) 
@Slf4j 
@ConfigurationProperties(prefix="tenantdb", locations={"datasources.yml"}) 
public class HsqlConfiguration { 


    private List<DataSourceConfig> dataSourceConfig = new ArrayList<>(); 

    @Bean 
    public List<DataSourceConfig> getDataSourceConfig() { 
     return dataSourceConfig; 
    } 

Con la configurazione sopra, ottengo:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hsqlConfiguration': Could not bind properties to [unknown] (target=tenantdb, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException 
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:303) 
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:250) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initia 

Ho provato varie combinazioni. Se cambio l'annotazione in @ConfigurationProperties(prefix="tenantdb.dataSourceConfig"), non ricevo l'errore ma List<DataSourceConfig> è vuoto.

AIUTO !!

+0

mie proprietà di configurazione sono annotati con '@ Component' e si riempie durante la scansione dei componenti, hanno ci hai provato? Anche altre due cose, dove si trova 'datasources.yml' e perché 'getDataSourceConfig' è annotato come bean? –

+0

'datasources.yml' si trova nella radice del classpath. 'getDataSourceConfig' è annotato come bean in modo da poterlo anche inserire altrove. – Raghu

+1

Ho provato a giocare con il tuo codice, ho creato il test e ho ottenuto l'elenco di 2 'DataSourceConfig' come previsto. L'unica cosa è che sono vuoti (hanno 'nullo' per' chiave', 'dbname' e' dbpath'. Ho fornito setter su quella classe ed è vincolato bene, potrebbe essere quello? –

risposta

5

È necessario utilizzare le proprietà di configurazione come POJO semplice con solo getter e setter e disporre di HsqlConfiguration separato che ha queste proprietà iniettate.

Qualcosa di simile a questo:

@Component 
@ConfigurationProperties(prefix="tenantdb", locations={"datasources.yml"}) 
public class TenantDbProperties { 

    //DataSourceConfig is POJO with key, dbpath and dbname 
    private List<DataSourceConfig> dataSourceConfigs;  

    public List<DataSourceConfig> getDataSourceConfigs(){ 
     return dataSourceConfigs; 
    } 

    public void setDataSourceConfigs(List<DataSourceConfig> dataSourceConfigs){ 
     this.dataSourceConfigs = dataSourceConfigs; 
    } 
} 

E in classe separata avere questa proprietà iniettato come:

@Configuration 
@Profile("hsqldb") 
@ImportResource(value = { "persistence-config.xml" }) 
@Slf4j 
public class HsqlConfiguration { 

    @Autowired 
    private TenantDbProperties tenantDbProperties; 

    //code goes here where you can use tenantDbProperties.getDataSourceConfigs() 
} 
Problemi correlati