当前位置:   article > 正文

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

value读取properties文件内容

首先得有一个.properties文件,里面的内容如下

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/commentDemo?useUnicode=true&characterEncoding=UTF-8
  • 1

第二,在spring配置文件中加载properties文件

<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> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第三,在java程序中,获取配置文件中的属性信息

@Controller  
@RequestMapping("/value")  
public class ValuePropertyController extends ApplicationController{  

    @Value("#{configProperties['jdbc.jdbcUrl']}")  
    private String jdbcUrl;   

    @RequestMapping  
    public String value(){  
        System.out.println(jdbcUrl);  
        return "";  
    }  
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/939271
推荐阅读
相关标签
  

闽ICP备14008679号