Auto Scan
@Component
@Value
package test.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("myConfig") // This must have public class myConfig { @Value("${userAddress}") private String address; @Value("${isUse}") private String isUser; public String getAddress() { return address; } public Boolean isUse() { if (isUser == null) { return false; } return Boolean.valueOf(isUser); } }
[test.properties] file content
userAddress=192.168.1.110 isUse = true
<!-- The automatically scanned package name should be added to the package name of the myConfig class--> <context:component-scan base-package="demo;test.config"></context:component-scan> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:test.properties</value> <value>classpath:test2.properties</value> </list> </property> </bean>