依赖注入

31 篇文章 2 订阅

1.有两个组件A和B,A依赖于B,且A有一个方法要用到importantMethod使用到了B如下:


package rely;
public class A {
    public void importantMethod() {
    B b=...//get an instance B
        b.usefulMethod();
    }

 

}

 

通过上述例子,依赖注入需要分别创建对象A和对象B

2.为了让框架进行依赖注入,程序员需要编写特定的set或构建方法:

set方式:

package rely;
public class A {
private B b;
    public void importantMethod() {
        b.usefulMethod();
    }
    public void setB() {
    this.b=b;
    }

}

构造方式:

package rely;
public class A {

private B b;
public A() {
this.b=b;
}
public void importantMethod() {
b.ud=sefulMethod();
}

}

注:Spring管理的对象成为bean

3.实例构造

通过构造器创建bean实例

<beans   ......>

      <bean name="product" class="app15.bean.product"></bean>

</beans>

通过工厂方法创建一个bean实例:

<bean id="calendar" class="java.util.Calendar"

factory-method="getInstance"

/>

4.构造举例:

package constructor;
public class product {
   private String name;
   private String description;
   private float price;
   
public product() {
super();
}


public product(String name, String description, float price) {
super();
this.name = name;
this.description = description;
this.price = price;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getDescription() {
return description;
}


public void setDescription(String description) {
this.description = description;
}


public float getPrice() {
return price;
}


public void setPrice(float price) {
this.price = price;
}
   

}

<!--通过带参数的构造器来初始化类  -->
<bean name="featrueProduct" class="app15a.bean,Product">
     <constructor-arg name="name" value="Ultimate Olive Oil"/>
     <constructor-arg name="description"   value="the oil on the market"/>
     <constructor-arg naem="price" value="9.95"/>

</bean>

 <!--Spring还支持指数方式(索引方式)传递参数-->
 <bean name="featrueProduct" class="app15a.bean.Product">
      <constructor-arg index="0" value="olive oil"/> 
      <constructor-arg index="1" value="market"/>
      <constructor-arg index="2" value="9.95"/>
 </bean>

5.注入举例:

package constructor;
public class Address {
    private String Street;
    private String City;
public Address(String street, String city) {
super();
Street = street;
City = city;
}
public String getStreet() {
return Street;
}
public void setStreet(String street) {
Street = street;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}

}

package constructor;


public class employee {
      private String firstName;
      private String lastName;
      private String homeAdress;
public employee(String firstName, String lastName, String homeAdress) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.homeAdress = homeAdress;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getHomeAdress() {
return homeAdress;
}
public void setHomeAdress(String homeAdress) {
this.homeAdress = homeAdress;
}
      
}

构造注入:

<!--constructor的构造方式注入  -->
<?xml version="1.0" encoding="UTF-8"?>
<bean name="simpleAddrss" class="constructor.Address">
   <constructor-arg name="street" value="changan"/>
   <constructor-arg name="City" value="xian"/>

</bean>

setter注入:

<!--setter的方式依赖注入  -->
<bean name="employee1" class="constructor.employee">
   <properties name="homeAddress" ref="simpleAddress"/>
   <properties name="firstName" value="Junir"/>
   <properties name="lastName" value="Moore"/>
</bean>

注意点:通过setter注入时,需要使用无参构造函数。

当无任何构造函数时,默认有“无参构造函数”;

存在有参构造函数时,需要显示写出“无参构造函数”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值