spring中bean的注入依赖方式
spring中对bean的注入方法。来获得bean对象,以及该类的属性的值
以该Dept类为例子
package org.www.bean;
public class Dept {??? private Integer deptid;??? ?private String deptname;
private Integer deptnum;???? private String deptdesc;????? public Dept() {}
public Dept(Integer deptid, String deptname, Integer deptnum,
?????????????? String deptdesc) {
????????????? this.deptid = deptid;???? this.deptname = deptname;
??????? this.deptnum = deptnum;?? this.deptdesc = deptdesc;
}......get和set方法略}
1、? 不带参的构造子注入方式::
通过不带参数的构造方法得到的对象。
比如说:<bean id="dept1" class="org.www.bean.Dept" />???????
解析该application.xml文件就会获得该对象
ApplicationContext ctx = new ClassPathXmlApplicationContext(? "applicationContext.xml");
????? Dept d = (Dept) ctx.getBean("dept1");// 从spring容器以对象的唯一编号和名称找到对象返回
?? System.out.println(d);// 结果为:org.www.bean.Dept@b66cc
2、? 通过带参的构造子注入方式:
需要在该类中有有带惨的构造方法
? <bean id="dept3" class="org.www.bean.Dept">
???? <constructor-arg type="java.lang.Integer" value="1"></constructor-arg>
???? <constructor-arg type="java.lang.String" value="市场部"></constructor-arg>
???? <constructor-arg type="java.lang.Integer" value="2"></constructor-arg>
???? <constructor-arg type="java.lang.String" value="行政相关工作!"></constructor-arg></bean>获得方式如上。
3、? set方法注入
需要在该类中有set方法
?? <bean id="add" class="org.www.bean.Address">
????? <property name="deptid" value="1"></property>
????? <property name="deptname" value="市场部"></property>
????? <property name="deptnum" value="22"></property>
????? <property name="deptdesc" value="市场相关"></property>
?? </bean>
获得方式如上。
4、接口注入方法
现在一般写dao层和service层的时候一般使用接口编程,而使用接口编程可以很好的解耦
下载模拟的是接口注入:
在dao层:
package org.www.dao;
import java.util.List;
public interface Peopledao {
?? void setPeoples(List peoples);//??传递一个参数
?? List getPeoples();??????????? //返回一个集合
}
在daoi层中,也就是dao的实现类
package org.www.daoi;
import java.util.List;
import org.www.dao.Peopledao;
public class Peopeldaoi implements Peopledao {
@SuppressWarnings("unused")
private List peoples;//该属性的get和set方法把接口中的功能实现了
public Peopeldaoi() {}
public List getPeoples() {
??????? return peoples;}
public void setPeoples(List peoples) {
??????? this.peoples = peoples;}}
在service层
package org.www.service;
import java.util.List;
public interface Service {
public List getPeoples();
}
在servicei层,service实现类
package org.www.servicei;
import java.util.List;
import org.www.dao.Peopledao;
import org.www.service.Service;
public class Servicei implements Service {
private Peopledao dao;//调用了dao,该dao属性是接口
public Servicei() {}
public List getPeoples() {?? List list = this.getDao().getPeoples();? return list;}
public Peopledao getDa? return dao;}
public void setDao(Peopledao dao) {? this.dao = dao;}}
看这个实现类,定义的是private Peopledao dao;
通过set方法注入的是该dao的实现类
在application.xml文件中的配置是:
先定义的是dao的实现类:
?<bean id="peopledaoi" class="org.www.daoi.Peopeldaoi">
???? <property name="peoples"><!-- 通过set容器依赖注入,即反调set方法 -->
?????? <list>
??????? <ref local="people"/><!-- local指的是在本xml文件中 -->
??????? <bean class="org.www.bean.People">
?????????? <property name="pid" value="2"></property>
?????? ?? <property name="name"><value>李四</value></property>
??? ?????? <property name="sex" value="女"></property>
??? ?????? <property name="birdthday" ref="date"></property>
?? ???????<property name="address" ref="add"></property>
??????? </bean>
??????? <ref bean="people"/><!-- 在所有的xml文件中取找 -->
?????? </list>
???? </property>
?? </bean>
在service的实现类中,定义的dao属性变量,指向该dao的实现类
?? <bean class="org.www.servicei.Servicei" id="service">
?
??? <property name="dao" ref="peopledaoi"></property>
?
?? </bean>
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?