依赖注入方式总结
一、 依赖注入的意义:让组件依赖于抽象,当组件要与其他实际对象发生依赖关系时,通过抽象来注入依赖的实际对象。
二、 依赖注入的方式:
1、 例:person类:
private String name;
private String sex;
private Integer age;
private Date birthday;
public PersonServiceBean() {
super();
System.out.println("构造器初始化对象");
// TODO Auto-generated constructor stub
}
public PersonServiceBean(String name, String sex, Integer age, Date birthday) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
publicvoid setName(String name) {
this.name = name;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
publicvoid setAge(Integer age) {
this.age = age;
}
publicvoid setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return"PersonServiceBean [name=" + name + ", sex=" + sex + ", age="
+ age + ", birthday=" + birthday + "]";
}
Bean.xml:(主体)
<bean id="personServiceBean" class="cn.csdn.service.PersonServiceBean" >
<!-- 依赖注入方式 -->
<!-- 欧诺个过方式1 set注入 -->
<property name="name">
<value>zhangsan</value>
</property>
<property name="sex">
<value>男</value>
</property>
<property name="age">
<value>28</value>
</property>
<!-- 出生日期 -->
<property name="birthday" ref="date">
<!-- 写一个内部bean
<bean class="java.util.date"/>-->
</property>
</bean>
<bean id="date" class="java.util.Date">
<property name="year">
<value>1988</value>
</property>
<property name="month">
<value>11</value>
</property>
<property name="date">
<value>22</value>
</property>
</bean>
测试类:
Public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:bean.xml");
PersonServiceBeanpersonServiceBean =(PpersonServiceBean) context.getBean("personServiceBean ");
System.out.println(personServiceBean.toString);
}
2、 构造器注入:(1)bean1.xml
<bean id="personServiceBean1" class="cn.csdn.service.PersonServiceBean" >
<!-- 构造器注入 -->
<constructor-arg index="0">
<value>zhangsan</value>
</constructor-arg>
<constructor-arg index="1">
<value>男</value>
</constructor-arg>
<constructor-arg index="2">
<value>28</value>
</constructor-arg>
<constructor-arg index="3">
<bean class="java.util.Date"/>
</constructor-arg>
</bean>
Public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:bean1.xml");
PersonServiceBeanpersonServiceBean=(PersonServiceBean) context.getBean("personServiceBean1");
System.out.println(personServiceBean.toString);
}
(2)、bean1.xml
<bean id="personServiceBean" class="cn.csdn.service.PersonServiceBean" >
<!-- 构造器注入 -->
<constructor-arg type="java.lang.String" value="sss"/>
<constructor-arg type="java.lang.String" value="男"/>
<constructor-arg type="java.lang.Integer" value="90"/>
<constructor-arg type="java.util.Date">
<ref bean="date"/>
</constructor-arg>
</bean>
<bean id="date" class="java.util.Date"/>
测试:
Public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:bean1.xml");
PersonServiceBeanpersonServiceBean =(PersonServiceBean) context.getBean("personServiceBean");
System.out.println(personServiceBean.toString);
}
3.teacherServiceBean
publicclass TeacherServiceBean {
private List<String> list;
private Set<String>set;
private Map<String,String>map;
public List<String> getList() {
returnlist;
}
publicvoid setList(List<String> list) {
this.list = list;
}
public Set<String> getSet() {
returnset;
}
publicvoid setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
returnmap;
}
publicvoid setMap(Map<String, String> map) {
this.map = map;
}
public TeacherServiceBean() {
super();
// TODO Auto-generated constructor stub
}
public TeacherServiceBean(List<String> list, Set<String> set,
Map<String, String> map) {
super();
this.list = list;
this.set = set;
this.map = map;
}
@Override
public String toString() {
return"TeacherServiceBean [list=" + list + ", set=" + set + ", map=" + map
+ "]";
}
}
Bean2.xml
<bean id="teacherServiceBean" class="cn.csdn.service.TeacherServiceBean" >
<property name="list">
<list><value>HP电脑</value>
<value>dell电脑</value>
<value>联想电脑</value></list>
</property>
<property name="set">
<set>
<value>java编程</value>
<value>net编程</value>
</set>
</property>
<property name="map">
<map>
<entry>
<key>
<value>001</value>
</key>
<value>java编程</value>
</entry>
<entry>
<key>
<value>002</value>
</key>
<value>net编程</value>
</entry>
</map>
</property>
</bean>
测试类:
publicvoid test() {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:bean2.xml");
TeacherServiceBean teacherService=(TeacherServiceBean) context.getBean("teacherServiceBean");
System.out.println("------------------list----集合");
List<String> list = teacherService.getList();
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println("------------------set----集合");
Set<String> set = teacherService.getSet();
for(String str:set){
System.out.println(str);
}
System.out.println("------------------map----集合");
Map<String,String> map = teacherService.getMap();
Set<Entry<String,String>> mset=map.entrySet();
for(Entry entry:mset){
System.out.println(entry.getKey()+"1111111"+entry.getValue());
}
}
本文详细介绍了依赖注入的概念及其在Java中的两种实现方式:setter注入和构造器注入,并展示了如何使用XML配置文件来完成依赖注入。
4558

被折叠的 条评论
为什么被折叠?



