Spring中通过构造方法传参数

1.实体类

package com.xbq.bean;
import
java.util.Timer; /** * @ClassName: Student * @Description: TODO 学生实体类 * @author xbq * @version 1.0 * @date 2017-3-21 上午10:53:49 */ public class Student { private int id; private String name; private Timer timer; private String address; public Student() { super(); } public Student(String name, Timer timer) { super(); this.name = name; this.timer = timer; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Timer getTimer() { return timer; } public void setTimer(Timer timer) { this.timer = timer; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", timer=" + timer + ", address=" + address + "]"; } }

2.application.xml

省略...............

<
bean id="student" class="com.xbq.bean.Student"> <constructor-arg name="name" value=""/> <constructor-arg name="timer" ref="timerBean"/> </bean> <bean id="timerBean" class="java.util.Timer"/>

3.测试

    可以使用
    @Resourse注入student,如:
    @Resourse
    private Student student;

    也可以使用
    Student student = (Student)springBeanUtil.getBeanName("student") 取出注入的student 
   (注:)springBeanUtil.getBeanName 是自己写的一个从spring容器中取出注入的bean,见:http://www.cnblogs.com/xbq8080/p/6593348.html
原文地址:https://www.cnblogs.com/xbq8080/p/6593321.html