新聞中心
Spring中怎么獲取其他Bean的屬性值,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一 配置
二 Bean
1 Person
package org.crazyit.app.service;public class Person{ private int age; private Son son; // age的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } // son的setter和getter方法 public void setSon(Son son) { this.son = son; } public Son getSon() { return this.son; }}
2 Son
package org.crazyit.app.service;public class Son{ private int age; // age的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } public String toString() { return "Son[age=" + age + "]"; }}
三 測試類
package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class SpringTest{ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); System.out.println("系統(tǒng)獲取的son1:" + ctx.getBean("son1")); System.out.println("系統(tǒng)獲取son2:" + ctx.getBean("son2")); System.out.println("系統(tǒng)獲取theAge的值:" + ctx.getBean("theAge")); System.out.println("系統(tǒng)獲取theAge2的值:" + ctx.getBean("theAge2")); }}
四 測試結果
系統(tǒng)獲取的son1:Son[age=11]系統(tǒng)獲取son2:Son[age=11]系統(tǒng)獲取theAge的值:11系統(tǒng)獲取theAge2的值:30
關于Spring中怎么獲取其他Bean的屬性值問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
分享題目:Spring中怎么獲取其他Bean的屬性值-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://www.ef60e0e.cn/article/dggsgj.html