spring 注入static属性

网上好多方法都是错误的,google之,发现
http://stackoverflow.com/questions/11324372/how-to-make-spring-inject-value-into-a-static-field 写道

You have two possibilities:

  1. non-static setter for static property/field;
  2. using org.springframework.beans.factory.config.MethodInvokingFactoryBean to invoke a static setter.

In the first option you have a bean with a regular setter but instead setting an instance property you set the static property/field.

publicvoid setTheProperty(Object value){ foo.bar.Class.STATIC_VALUE = value;}

but in order to do this you need to have an instance of a bean that will expose this setter (its more like an workaround).

In the second case it would be done as follows:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"><property name="staticMethod" value="foo.bar.Class.setTheProperty"/><property name="arguments"><list><ref bean="theProperty"/></list></property></bean>

On you case you will add a new setter on the Utils class:

publicstatic setDataBaseAttr(Properties p)

and in your context you will configure it with the approach exemplified above, more or less like:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"><property name="staticMethod" value="foo.bar.Utils.setDataBaseAttr"/><property name="arguments"><list><ref bean="dataBaseAttr"/></list></property></bean>

 其中方法二 应该比较实用,就是配置xml,配置staticMethod,以及instance

也可以这么玩

http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field 写道
publicclassSample{publicstaticString name;@PostConstructpublicvoid init(){ name = privateName;}@Value("${my.name}")privateString privateName;publicString getPrivateName(){return privateName;}publicvoid setPrivateName(String privateName){this.privateName = privateName;}}

 但是下面有大神说了  Firs of all, public static non-final fields are evil. Spring does not allow injecting to such fields for a reason.    

because   Non-final means you can modify the field value, which, for a static field, implies handling thread concurrency – a.k.a. pain in the stack.

未经允许不得转载:IT圈子 » spring 注入static属性

赞 (0)
分享到:更多 ()
  1. 疤痕修复需要多少钱http://www.dfhon.com/news/bhxfjg/注射式微整形的价格http://www.dfhon.com/news/st-xw/眉毛整形多少钱http://www.dfhon.com/news/mb-xw/激光脱毛价格表http://www.dfhon.com/news/jgtmjg/整形价格表http://www.dfhon.com/zhengxingjiage/下颌角整形http://www.dfhon.com/news/xhjzxjg/种植头发多少钱http://www.dfhon.com/zhengxingjiage/toufazhongzhi/chengdu/做整容手术要多少钱http://www.dfhon.com/zhengxingjiage/面部除皱术多少钱http://www.dfhon.com/zhengxingjiage/mianbuchuzhou/shenzhen/面美网www.mianmeiw.comhttp://blog.sina.com.cn/u/5933322552http://blog.sina.com.cn/u/5976108650http://blog.sina.com.cn/u/5872804292

  2. 疤痕修复需要多少钱http://www.dfhon.com/news/bhxfjg/注射式微整形的价格http://www.dfhon.com/news/st-xw/眉毛整形多少钱http://www.dfhon.com/news/mb-xw/激光脱毛价格表http://www.dfhon.com/news/jgtmjg/整形价格表http://www.dfhon.com/zhengxingjiage/下颌角整形http://www.dfhon.com/news/xhjzxjg/种植头发多少钱http://www.dfhon.com/zhengxingjiage/toufazhongzhi/chengdu/做整容手术要多少钱http://www.dfhon.com/zhengxingjiage/面部除皱术多少钱http://www.dfhon.com/zhengxingjiage/mianbuchuzhou/shenzhen/面美网www.mianmeiw.comhttp://blog.sina.com.cn/u/5933322552http://blog.sina.com.cn/u/5976108650http://blog.sina.com.cn/u/5872804292