文章标题

来源:互联网 发布:php 使用ftp上传文件 编辑:程序博客网 时间:2024/06/07 20:29


通过@Value注解读取.properties配置内容


[java] view plain copy
print?
  1. @Controller  
  2. @RequestMapping(“/value”)  
  3. public class ValuePropertyController extends ApplicationController{  
  4.       
  5.     @Value(“#{configProperties[‘jdbc.jdbcUrl’]}”)  
  6.     private String jdbcUrl;   
  7.       
  8.     @RequestMapping  
  9.     public String value(){  
  10.         System.out.println(jdbcUrl);  
  11.         return “”;  
  12.     }  
  13. }  
@Controller@RequestMapping("/value")public class ValuePropertyController extends ApplicationController{    @Value("#{configProperties['jdbc.jdbcUrl']}")    private String jdbcUrl;     @RequestMapping    public String value(){        System.out.println(jdbcUrl);        return "";    }}


applicationContext.xml

[html] view plain copy
print?
  1. <bean id=“configProperties” class=“org.springframework.beans.factory.config.PropertiesFactoryBean”>  
  2.        <property name=“locations”>  
  3.            <list>  
  4.                <value>classpath:database.properties</value>  
  5.            </list>  
  6.        </property>  
  7.     </bean>  
  8.     <bean id=“propertyConfigurer” class=“org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer”>  
  9.         <property name=“properties” ref=“configProperties” />  
  10.     </bean>  
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">       <property name="locations">           <list>               <value>classpath:database.properties</value>           </list>       </property>    </bean>    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">        <property name="properties" ref="configProperties" />    </bean>

database.properties

[html] view plain copy
print?
  1. jdbc.jdbcUrl=jdbc:mysql://localhost:3306/commentDemo?useUnicode=true&characterEncoding=UTF-8  
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/commentDemo?useUnicode=true&characterEncoding=UTF-8



    </article>