You have two possibilities:
- non-static setter for static property/field;
- 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
也可以这么玩
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.
相关推荐
t.InvokeMember(prop.propertyName, ........BindingFlags.SetProperty, null, o, new Object[] {p})利用反射机制对创建出来的对象设置属性值。 我们的Factory就是利用这种方式根据配置文件动态加载程序集,动态创建...
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项目中的...
mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...
(根据开发) ... 受Spring Boot的启发,因此,您可以以这种样式构建PHP应用程序。... 依赖项注入是由具有PHP8属性(注释)的框架管理的 样例服务 #[Service] class UserServiceImpl implements UserService
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值. 3.编写DEMO 1.控制器 @Controller public class HelloController { private ...
(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的子接口?...
105、spring工作机制及为什么要用? 24 106、HttpSession session = request.getSession() 24 107、getParameter与 getAttribute的区别? 24 108、以下哪一个不是赋值符号? 25 109、以下哪个不是Collection的子接口?...
现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public class ...
现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public ...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 19、Jdo是什么? 20、什么是spring的IOC AOP 21、STRUTS的工作流程! 22、spring 与EJB...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 ...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 137 19、Jdo是什么? 137 20、什么是spring的IOC AOP 137 21、STRUTS的工作流程! 137 22、...
18、Spring 的依赖注入是什么意思? 给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、...