Spring PropertyPlaceholderConfigurer load from DB

后端 未结 1 665

Is it possible to load properties from DB with Custom Spring PropertyPlaceholderConfigurer? and is it also possible that datasource provided to custom PropertyPlaceholderCon

1条回答
  •  误落风尘
    2020-12-05 06:49

    Spring Context XML

    
    
        
            
            
            
            
                
                    classpath:db.properties
                
            
        
    
     
            ${imosdb.driver}
            ${imosdb.url}
            ${imosdb.username}
            ${imosdb.password}
            ${imosdb.initial_pool_size}
            ${imosdb.max_pool_size}
            ${imosdb.min_pool_size}
            
            
            
            
            
            
            
        
    
        
            
            
            
            
            
            
    
            
                
                    classpath:static.properties
                    file:static.properties
                
            
    
        
    
        
    
        
        
        
    
    
    

    Java PlaceholderClass

    import java.util.Properties;
    
    import javax.sql.DataSource;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    
    public class DbPropertySourcesPlaceholderConfigurer extends PropertyPlaceholderConfigurer 
    {
       private static final String DEFAULT_DATASOURCENAME = "dataSource";
       private static final String DEFAULT_DBTABLENAME = "property";
       private static final String DEFAULT_DBKEYCOLUMNNAME = "key";
       private static final String DEFAULT_DBVALUECOLUMNNAME = "value";
       String dataSourceName;
       String dbTableName;
       String dbKeyColumnName;
       String dbValueColumnName;
       @Override
       public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
       {
    
          DataSource dataSource = (DataSource) beanFactory.getBean(getDataSourceName());
         // DbProperties dbProps = new DbProperties(dataSource);
          final Properties dbProps = new Properties();
          dbProps.put("app.version", "v3");
          setProperties(dbProps);
          super.postProcessBeanFactory(beanFactory);
       }
    
       public String getDataSourceName() {
          return dataSourceName==null?DEFAULT_DATASOURCENAME:dataSourceName;
       }
    
       public void setDataSourceName(String dataSourceName) {
          this.dataSourceName = dataSourceName;
       }
    
    
    }
    

    Special thanks to writer of following pages.

    http://ykchee.blogspot.com.tr/2012/09/spring-31-loading-properties-for-xml.html

    http://blog.javaforge.net/post/31720600427/configuring-spring-based-web-application-from-database

    http://www.javacodegeeks.com/2012/11/spring-3-1-loading-properties-for-xml-configuration-from-database.html

    0 讨论(0)
提交回复
热议问题