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

1.controller层@Value("#{configProperties['jdbc.jdbcUrl']}")  

@Controller
@RequestMapping("/value")
public class ValuePropertyController extends ApplicationController{
    
	@Value("#{configProperties['jdbc.jdbcUrl']}")
	private String jdbcUrl; 
	
	@RequestMapping
	public String value(){
		System.out.println(jdbcUrl);
		return "";
	}
}

 2.spring配置文件

<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>

 3.注意写法

猜你喜欢

转载自yuhaiwei-bj.iteye.com/blog/2268375