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;//该属性的getset方法把接口中的功能实现了

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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值