一 通过构造函数注入
set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选的,构造注入的优势是通过构造强制依赖关系,不可能实例化不完全的或无法使用的bean。
二 举例
1 Employee
package com.hsp.constructor;
public class Employee {
private String name;
private int age;
public Employee(String name) {
System.out.println("Employee(String name) 函数被调用..");
this.name = name;
this.age = age;
}
public Employee(String name, int age) {
System.out.println("Employee(String name, int age) 函数被调用..");
this.name = name;
this.age = age;<