`

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.

 

 

 

 

 

 

 

0
1
分享到:
评论
2 楼 cainiao1923 2016-12-14  
java-lxm 写道
写文章贴个图也不好好贴


....难道是网站的问题,编辑的时候是OK的啊
1 楼 java-lxm 2016-12-14  
写文章贴个图也不好好贴

相关推荐

    Spring.net框架

    t.InvokeMember(prop.propertyName, ........BindingFlags.SetProperty, null, o, new Object[] {p})利用反射机制对创建出来的对象设置属性值。 我们的Factory就是利用这种方式根据配置文件动态加载程序集,动态创建...

    Spring 3.x 中文开发手册.pdf

    http://static.springsource.org/spring/docs/3.1.0.RELEASE/spring-framework-reference/html/new-in-3.1.html 1、基于annotation的cache服务,这个非常好 这个最早源于spring2.x时代的spring-modules项目中的...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    winter-boot:Spring Boot样式-PHP8框架。 仅带有注释(属性)的轻松依赖注入和面向方面的魔术

    (根据开发) ... 受Spring Boot的启发,因此,您可以以这种样式构建PHP应用程序。... 依赖项注入是由具有PHP8属性(注释)的框架管理的 样例服务 #[Service] class UserServiceImpl implements UserService

    carcar.sql

    org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值. 3.编写DEMO 1.控制器 @Controller public class HelloController { private ...

    MyBatis 需要注意的地方junit注解

    (1)mapper标签的namespace属性:指定该xml对应的dao层接口的路径 3.spring-mybatis.xml:spring集成mybatils的配置文件 (1)配置sqlSessionFactory指定要操作的数据库,以及mapper.xml的所在目录 (2)配置指定...

    千方百计笔试题大全

    105、spring工作机制及为什么要用? 24 106、HttpSession session = request.getSession() 24 107、getParameter与 getAttribute的区别? 24 108、以下哪一个不是赋值符号? 25 109、以下哪个不是Collection的子接口?...

    java面试宝典

    105、spring工作机制及为什么要用? 24 106、HttpSession session = request.getSession() 24 107、getParameter与 getAttribute的区别? 24 108、以下哪一个不是赋值符号? 25 109、以下哪个不是Collection的子接口?...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public class ...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public ...

    Java面试宝典2010版

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 19、Jdo是什么? 20、什么是spring的IOC AOP 21、STRUTS的工作流程! 22、spring 与EJB...

    最新Java面试宝典pdf版

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...

    Java面试笔试资料大全

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...

    JAVA面试宝典2010

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...

    Java面试宝典-经典

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...

    java面试题大全(2012版)

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...

    Java面试宝典2012版

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 ...

    java面试宝典2012

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 137 19、Jdo是什么? 137 20、什么是spring的IOC AOP 137 21、STRUTS的工作流程! 137 22、...

    Java面试宝典2012新版

    18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...

Global site tag (gtag.js) - Google Analytics