正文
spring注入静态成员变量提示invalid setter method
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
果然还是不够细心啊,被坑一晚上..
一个极其简单的小程序,但是需要通过xml文件配置注入一个值,唯一的特别是要注入的属性是类中的静态成员变量..
如下,然后自动生成get和set方法..坑就从此开始了...
public class Food{
private static String desc; public static String getDesc(){
return desc;
} public static void setDesc(String desc){
Food.desc = desc;
}
}
然后xml中如下配置:
<bean id="food" class="xxxk.Food" >
<property name="desc" value="食物" />
</bean>
然后启动就会报找不到set方法的错误...反复对比set方法的名字,无任何异常..然后一晚上就这样报废了...
最后发现自动生成的方法会带static修饰符,而spring的set方法不能是static的,跟一下源码能发现,虽然各种值和名字都能获取到,但是其中有一个resolvedDescriptor获取不到.
去掉set方法前面的static修饰符即可..