Spring 注入静态变量 解决方案(之我见)
先说下我现在使用的版本:Spring3.0 annotation写的
今天写项目遇到了一个问题:想通过jstl自定义函数来实现页面上面的一部分内容。通过Spring注入后发现报如下错误:
@Resource annotation is not supported on static fields
上网搜寻发现,Spring注解注入不支持静态变量注入,遂想办法解决。
?
首先看到的方法是有一个
@Resourceprivate HibernateTemplate hibernateTemplate;private static TalentsDaoImplTest td;@PostConstructpublic void init() { td = this; td.hibernateTemplate = this.hibernateTemplate;}@Testpublic void testStatic() { Talents talents = td.hibernateTemplate.get(Talents.class, 1L); System.out.println(talents.getAge());}?
?
发这篇只是发现,可能是这个问题出现的或者是使用的人比较少,但是这个解决方案真的不太好找,我就总结下来,希望可以给大家提供帮助。
还有就是我这样是可以解决了,不知道大家是否还有更好的解决办法,欢迎大家共同来分享。
1 楼 firefly.li 2012-04-12 不用这么麻烦,注入静态变量时,set方法只要不设置为静态的就可以了。 2 楼 irfen 2012-04-16 firefly.li 写道不用这么麻烦,注入静态变量时,set方法只要不设置为静态的就可以了。