how to pass values dynamically in config file

后端 未结 2 1225
夕颜
夕颜 2020-12-20 10:17

I want to pass the value in the value tag of the property dynamically from the DB. Is this possible in Spring? And how ?

For example in the configuration below.

相关标签:
2条回答
  • 2020-12-20 10:38

    You can write a another Java class and make it as a bean in Application context and using Spring Expression Language, you have evaluate and get output of the method call.

    XML configuration:

    <property name="serviceClass" 
                     value="#{webServiceInfoFromDB.wsdlUrl}" />
    <property name="username" 
                     value="#{webServiceInfoFromDB.username}" />
    

    WeServiceInfoFromDB.java class:

    class WebServiceInfoFromDB {
    
       public String getWsdlUrl() {
          // Get the Wsdl URL from DB.
          return wsdlUrl;
       }
    
       public String getUsername(){
          // get the username from DB
          return username;
       }
    

    XML configuration in application context:

    <bean id="webServiceInfoFromDB" class="WebServiceInfoFromDB">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    
    0 讨论(0)
  • 2020-12-20 10:47

    You can setup property Place Holder with your properties file name that contains key-value pair.

    <context:property-placeholder location="config.properties" />

    The config.properties file looks like:

    wsdl.url = http://server:8080/ServiceAccessPoint
    webservice.username = guest
    webservice.passward = guest123
    

    Now you can modify your bean definition with ${key}

    <bean id="proxyFactory" 
    class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="${wsdl.url}"/>
    <property name="address" value="${wsdl.url}"/>
    <property name="username" value="${webservice.username}"/>
    <property name="password" value="${webservice.password}"/>
    

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