spring注入靜態成員變數提示invalid setter method,invalidsetter

來源:互聯網
上載者:User

spring注入靜態成員變數提示invalid setter method,invalidsetter

果然還是不夠細心啊,被坑一晚上..

一個極其簡單的小程式,但是需要通過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修飾符即可..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.