三个注意事项:
自动扫描
@Component
@Value
【test.properties】文件内容
自动扫描
@Component
@Value
package test.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("myConfig") // 这个一定要有
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】文件内容
userAddress=192.168.1.110
isUse=true
<!-- 自动扫描的包名 这里要加入 myConfig类的包名 -->
<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>