Spring mvc中@Value

@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

<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

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

 

posted on 2017-11-03 15:31  任性的大萝卜  阅读(326)  评论(0编辑  收藏  举报

导航