2021.11.4 Spring依赖注入三种方式

本文介绍Spring框架中的三种依赖注入方式:通过set方法注入、通过构造方法注入和通过静态工厂注入。并通过具体示例展示了每种注入方式的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

部门类

package com.xu.entity;

public class Depart {
    private String dname;
    private String dvalue;

    @Override
    public String toString() {
        return "Depart{" +
                "dname='" + dname + '\'' +
                ", dvalue='" + dvalue + '\'' +
                '}';
    }

    public Depart() {
    }

    public Depart(String dname) {
        this.dname = dname;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getDvalue() {
        return dvalue;
    }

    public void setDvalue(String dvalue) {
        this.dvalue = dvalue;
    }

    public Depart(String dname, String dvalue) {
        this.dname = dname;
        this.dvalue = dvalue;
    }
}

用户类

package com.xu.entity;


import java.util.*;

public class User {
    private String name;
    private Depart depart;
    private String[] books;
    private List<String> hobbys;
    private Map<String, String> card;
    private Set<String> games;
    private Properties info;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Student:" +
                "name='" + name + '\'' + "\n" +
                "         address=" + depart.toString() + "\n" +
                "         books=" + Arrays.toString(books) + "\n" +
                "         hobbys=" + hobbys + "\n" +
                "         card=" + card + "\n" +
                "         games=" + games + "\n" +
                "         info=" + info + "\n";
    }

    public String getName() {
        return name;
    }

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

    public Depart getDepart() {
        return depart;
    }

    public void setDepart(Depart depart) {
        this.depart = depart;
    }

    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> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }
}

静态工厂类

package com.xu.utils;

import com.xu.entity.Depart;

public class DepartFactory {
    public static Depart createdepart(){
        Depart de=new Depart();
        de.setDname("研发部门");
        de.setDvalue("666666");
        return de;
    }
}

配置文件

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    1.通过set注入-->
    <bean id="user" class="com.xu.entity.User">
        <property name="name" value="徐总"></property>
        <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="123456"/>
                <entry key="银行卡" value="123456"/>
            </map>
        </property>
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>贪吃蛇</value>
            </set>
        </property>
        <property name="info">
            <props>
                <prop key="学号">123456789</prop>
                <prop key="性别"></prop>
                <prop key="姓名">小明</prop>
            </props>
        </property>
        <property name="depart" ref="departs">
        </property>
    </bean>
    <bean id="departs" class="com.xu.entity.Depart">
        <property name="dname" value="研发部门"></property>
        <property name="dvalue" value="20211104"></property>
    </bean>
    <!--    2.通过构造方法注入-->
    <bean id="user2" class="com.xu.entity.User">
        <constructor-arg index="0" type="java.lang.String" value="小徐"></constructor-arg>
    </bean>
    <!--    3.通过静态工厂注入-->
    <bean id="factory" class="com.xu.utils.DepartFactory" factory-method="createdepart"></bean>
    <bean id="user3" class="com.xu.entity.User">
        <property name="depart" ref="factory"></property>
    </bean>
    <!--    bean里面的属性值-->

</beans>

测试类

import com.xu.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUserDepart {
//    1.通过set注入
    @Test
    public void test(){
        ClassPathXmlApplicationContext contest = new ClassPathXmlApplicationContext("userdepart.xml");
        User user = (User) contest.getBean("user");
        System.out.println(user.toString());
    }
//    2.通过构造方法注入
    @Test
    public void test2(){
        ClassPathXmlApplicationContext contest = new ClassPathXmlApplicationContext("userdepart.xml");
        User user = (User) contest.getBean("user2");
        System.out.println(user.getName());
    }
//    3.通过静态工厂注入
    @Test
    public void test3(){
        ClassPathXmlApplicationContext contest = new ClassPathXmlApplicationContext("userdepart.xml");
        User user = (User) contest.getBean("user3");
        System.out.println(user.getDepart());
    }
}

在pom.xml文件中配置

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.9</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
</dependency>
<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
</dependency>

总结:

1.通过set注入

2.通过构造方法注入

3.通过静态工厂注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值