【spring教程之二】spring注入xml中带参数的构造函数

1、续上文,如果想在注入bean文件的时候,,传入参数到构造函数中。主要需要修改的就是spring.xml配置文件中的配置方法。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xmlns:context=""xmlns:aop=""xmlns:tx=""xsi:schemaLocation=""> <bean id="do" class="com.test.pro.Student"> <constructor-arg value="19"></constructor-arg> </bean></beans>2、我们把类修改成了学生类,Student.java,其中没有默认的构造函数,而是一个新的带参数的构造。

package com.test.pro;public class Student {private int age;public Student(int age){this.age=age;}public void speaking(){System.out.println("我的年龄是:"+age);}}3、输出结果

获致幸福的不二法门是珍视你所拥有的遗忘你所没有的。

【spring教程之二】spring注入xml中带参数的构造函数

相关文章:

你感兴趣的文章:

标签云: