<bean id="configuration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:configuration.properties</value> </list> </property> </bean>
@Component public class ValueDemo { @Value("#{configuration['key']}") private String value; public String getValue() { return value; } }
或者带默认值
@Component public class ValueDemo { @Value("#{ @configuration['key'] ?: 'helloworld' }")private String value; public String getValue() { return value; } }
或者默认值是某个类的静态变量
@Component public class ValueDemo { @Value("#{ @configuration['key'] ?: T(org.apache.shiro.web.servlet.SimpleCookie).DEFAULT_MAX_AGE}") private String value; public String getValue() { return value; } }