ITeye博客

Spring配置中的bean引用其它bean的属性值

czj4451 · 2013-01-23 15:24 · spring/ejb · 企业架构 · Java · Spring · XML · Bean · Markdown

这项功能在spring的3.0版本以上才支持,如果使用较早的版本(如2.5),会造成转换异常(如将String转换为int)以及不能解析赋值字符串。 需要的jar包:spring的核心包以及Apache的commons-logging包。 [code="java"] public class Polishing { int laboratory = 1; public int getLavatory(int lavatory) { return lavatory; } // Getters and setters are omitted } [/code] [code="java"] public class Freight { int laboratory; int slurry; int compensatory; // Getters and setters are omitted } [/code] beans.xml: [code="xml"] [/code] 测试类: [code="java"] public class Perplex { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Freight bean2 = (Freight) ctx.getBean("freight"); System.out.println(bean2.getLaboratory()); System.out.println(bean2.getSlurry()); System.out.println(bean2.getCompensatory()); } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。