Spring:DI依赖注入的几种方式

本文内容纲要:Spring:DI依赖注入的几种方式

据我所学,spring实现依赖注入(DI)的方式分为三大类:基于构造器(构造方法)的依赖注入、基于setter的依赖注入、其他方式(c命名空间、p命名空间等)。其中推荐使用setter方法注入,这种注入方式也是最多人使用的。

下面我们通过代码来举例三种注入方式:

1.基于构造器(构造方法)的依赖注入

这种方式是通过实体类中的构造方法来完成属性的赋值,所以实体类中必须含有带参的构造方法!

首先先编写实体类User,如下:

public class User {

private int id;

private String name;

private int age;

private String pwd;

public User() {

}

public User(int id, String name, int age, String pwd) {

this.id = id;

this.name = name;

this.age = age;

this.pwd = pwd;

System.out.println("User的全参构造");

}

@Override

public String toString() {

return "User{" +

"id=" + id +

", name='" + name + '\'' +

", age=" + age +

", pwd='" + pwd + '\'' +

'}';

}

}

然后我们就可以编写配置文件了,一共有三种方式,这三种方式都是一样的,掌握一种便可以了,推荐使用方式一。

方式一:通过属性名称赋值

<bean id="user" class="com.kuang.pojo.User">

<constructor-arg name="id" value="0000"/>

<constructor-arg name="age" value="21"/>

<constructor-arg name="pwd" value="123456"/>

<constructor-arg name="name" value="张三"/>

</bean>

方式二:通过下标赋值

注意:这里index就是实体类中属性的索引,从0开始,实体类中从上到下第一个属性的索引为0,第二个属性的索引为1....以此类推。

<!--通过下标赋值-->

<bean id="user" class="com.kuang.pojo.User">

<constructor-arg index="0" value="0000"/>

<constructor-arg index="1" value="张三"/>

<constructor-arg index="2" value="21"/>

<constructor-arg index="3" value="123456"/>

</bean>

方式三:通过类型赋值

<bean id="user" class="com.kuang.pojo.User">

<constructor-arg type="int" value="0000"/>

<constructor-arg type="java.lang.String" value="张三"/>

<constructor-arg type="int" value="21"/>

<constructor-arg type="java.lang.String" value="123456"/>

</bean>

2.基于setter的依赖注入

这种方式是通过实体类中的setter方法来完成属性的赋值的,所以实体类中必须有各个属性的setter方法!

Student实体类如下:

ImageImage

public class Student {

private String name;

private Address address;

private String[] books;

private List<String> hobbys;

private Map<String,String> card;

private Set<String> game;

private String wife;

private Properties info;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Address getAddress() {

return address;

}

public void setAddress(Address address) {

this.address = address;

}

public String[] getBooks() {

return books;

}

public void setBooks(String[] books) {

this.books = books;

}

public List<String> getHobbys() {

return hobbys;

}

public void setHobbys(List<String> hobbys) {

this.hobbys = hobbys;

}

public Map<String, String> getCard() {

return card;

}

public void setCard(Map<String, String> card) {

this.card = card;

}

public Set<String> getGame() {

return game;

}

public void setGame(Set<String> game) {

this.game = game;

}

public String getWife() {

return wife;

}

public void setWife(String wife) {

this.wife = wife;

}

public Properties getInfo() {

return info;

}

public void setInfo(Properties info) {

this.info = info;

}

@Override

public String toString() {

return "Student{" +

"name='" + name + '\'' +

", address=" + address +

", books=" + Arrays.toString(books) +

", hobbys=" + hobbys +

", card=" + card +

", game=" + game +

", wife='" + wife + '\'' +

", info=" + info +

'}';

}

}

代码过长就折叠了

Address实体类如下:

ImageImage

public class Address {

private String address;

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@Override

public String toString() {

return "Address{" +

"address='" + address + '\'' +

'}';

}

}

View Code

配置文件的代码如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="Address" class="com.kuang.pojo.Address">

    <property name="address" value="北京"/>

  </bean>

<bean id="student" class="com.kuang.pojo.Student">

<property name="name" value="张三"/>

<property name="address" ref="Address" />

<property name="books">

<array>

<value>红楼梦</value>

<value>西游记</value>

<value>水浒传</value>

<value>三国演义</value>

</array>

</property>

<property name="hobbys">

<list>

<value>游泳</value>

<value>跑步</value>

<value>爬山</value>

</list>

</property>

<property name="card">

<map>

<entry key="身份证" value="123456789987654"/>

<entry key="银行卡" value="654873264654879"/>

</map>

</property>

<property name="game">

<set >

<value>英雄联盟</value>

<value>王者荣耀</value>

<value>穿越火线</value>

</set>

</property>

<property name="wife" >

<null/>

</property>

<property name="info">

<props>

<prop key="学号">1665487956</prop>

<prop key="性别">男</prop>

<prop key="姓名">小明</prop>

</props>

</property>

</bean>

</beans>

3.其他方式(c命名空间、p命名空间等)

本文内容总结:Spring:DI依赖注入的几种方式

原文链接:https://www.cnblogs.com/bear7/p/12527486.html

以上是 Spring:DI依赖注入的几种方式 的全部内容, 来源链接: utcz.com/z/295809.html

回到顶部