spring 注入static属性

news/2024/7/5 5:31:53
网上好多方法都是错误的,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.

 

 

 

 

 

 

 


http://www.niftyadmin.cn/n/4235434.html

相关文章

springboot mysql脚本_springboot配置mysql连接的实例代码

一&#xff1a;导入pmo.xm配置包mysql库连接、druid连接池、mybatis组件mysqlmysql-connector-javaruntimecom.alibabadruid-spring-boot-starter1.1.10org.mybatis.spring.bootmybatis-spring-boot-starter${mybatis.version}配置扫描文件src/main/java**/ *.xmltruesrc/main/…

往非sping管理的类里注入spring对象

http://stackoverflow.com/questions/310271/injecting-beans-into-a-class-outside-the-spring-managed-context 写道You can do this: ApplicationContext ctx ... YourClass someBeanNotCreatedBySpring ... ctx.getAutowireCapableBeanFactory().autowireBeanProperties(so…

stringbuilder 编码_从 String.getBytes 理解 Java 编码和解码

原码&#xff0c;补码&#xff0c;反码因为原码&#xff0c;补码&#xff0c;反码比较简单&#xff0c;我这里粘贴一个例子进行展示。Unicode 和 UTF-8 的关系Uincode 是一个字符集。它规定了我们使用到的字或符号的码点(code point)。码点使用 16 进制保存。Uincode 字符集规定…

持久化对象在Hibernate三状态解析

在Hibernate持久化对象可以划分为三种状态&#xff0c;分别是瞬态(Transient)&#xff0c;持久态(Persistent)&#xff0c;脱管态(Detached)。持久化对象的三种状态是可以相互转化的。本文向大家介绍Hibernate持久化对象&#xff0c;可能好多人还不了解Hibernate持久化对象&…

学无止境·MySQL⑤(存储函数、存储过程)

存储函数和存储过程试题 试题一1、创建一个可以统计表格内记录条数的存储函数 &#xff0c;函数名为count_sch()2、创建一个存储过程avg_sai&#xff0c;有3个参数&#xff0c;分别是deptno&#xff0c;job&#xff0c;接收平均工资&#xff0c;功能查询emp表dept为30&#xff…

(转)hibernate 二级缓存配置

在applicationContext.xml文件中添加以下代码&#xff1a; <prop key"hibernate.cache.use_second_level_cache">true</prop> <!--设置缓存机制为二级缓存 --> <prop key"hibernate.cache.use_query_cache">true</prop> …

类似layui的前端框架_浅谈SSM+接口自动化框架结合搭建测试数据平台

一、搭建SSM框架网上有很多这方面的帖子供大家参考&#xff0c;我挑了几篇供大家参考&#xff0c;有兴趣的可以了解下&#xff1a;https://www.jianshu.com/p/fdf1c2ddf201http://www.uml.org.cn/j2ee/201904192.asp二、搭建接口自动化框架所用的框架是javatestngmaven&#xf…

(转)blob和text区别

&#xff08;mysql 是没有clob的&#xff09; &#xff08;大多数方面&#xff0c;可以将BLOB列视为能够足够大的VARBINARY列。同样&#xff0c;可以将TEXT列视为VARCHAR列。&#xff09; text分为4种类型&#xff1a;TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT&#xff0c;分别对应…