接口注入
Instance.properties(该文件应放在classes文件夹下)文件的内容如下:
Student=com.sias.Student;
public class StudentFactory{
Properties pro=new Properties();
pro.load(new FileInputStream("Instance.properties"));
String studentname=(String)pro.get(Student);
Student stu=(Student)Class.forName(studentname).newInstance();
return stu;
}
get/set方法注入
在applicationContext.xml配置文件中,用bean元素的子元素property
property写法如下:
<property name=""><ref bean=""/></property>
或<property name=""><ref local=""/></property>
或<proerty name="" ref="">
附:
idref和ref区别:
idref将对应bean的id值注入,如果对应的id不存在,部署时出错,这是其与value的重要区别
ref将对应bean的类的对象注入
构造子方法注入
在applicationContext.xml配置文件中,用bean元素的子元素construtor-arg
construtor-arg写法如下:
<construtor-arg>
<ref bean=""/>
</construtor-arg>
附:
ref属性的bean和local区别
bean:在所有的spring配置文件中查询bean元素
local:只在本配置文件中查询bean元素