Spring 通过@Value给静态变量赋值

// 1、必须加上注解
@Component
public class Constants implements Serializable {	
	
	//2、定义静态变量
	public static boolean isDevMode;
	
	//3、赋值操作
	@Value("${isDevModeStr}")
	public void isDevModeStr(String isDevModeStr) {
		Constants.isDevMode="true".equalsIgnoreCase(isDevModeStr)?true:false;
	}
}

config.properties :

isDevModeStr=true

其他配置:

applicationContext.xml :

<!-- 确保 Constants.java  文件能被扫描到 -->
<context:component-scan base-package="com.xxx.yyy" />

<!-- 读取属性文件 -->
<context:property-placeholder  location="classpath:/properties/config.properties"
	 ignore-unresolvable="true" />

猜你喜欢

转载自blog.csdn.net/xiaojin21cen/article/details/84965183