首页 > 其他分享 >6.DI依赖注入

6.DI依赖注入

时间:2023-01-15 20:35:32浏览次数:36  
标签:依赖 return String DI void private address public 注入

1.构造器注入

  前面已经使用过

2.set注入【重点】

  依赖注入:set注入  

    依赖:bean对象的创建依赖于spring容器

    注入:bean对象中的所有属性由spring容器注入

  环境搭建:

    (1)导入依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.20</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.20</version>
    </dependency>
</dependencies>

   (2)编写实例

Address.java

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 + '\'' +
                '}';
    }
}

Student .java

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> cards;
    private Set<String> games;
    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> getCards() {
        return cards;
    }

    public void setCards(Map<String, String> cards) {
        this.cards = cards;
    }

    public Set<String> getGames() {
        return games;
    }

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

    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 +
                ", cards=" + cards +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

3.ApplicationContext.xml

<?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">
    
    
</beans>

4. 注入的实现

<bean id="address" class="com.zuo.dao.Address">
    <property name="address" value="左家旁峪村"/>
</bean>
<bean id="studentName" class="com.zuo.dao.Student">
    <!--普通值的注入 value -->
    <property name="name" value="zuozhikun"/>
    <!--bean的注入 ref-->
    <property name="address" ref="address"/>
    <!--数组-->
    <property name="books">
        <array>
            <value>水浒传</value>
            <value>三国演义</value>
            <value>西游记</value>
            <value>红楼梦</value>
        </array>
    </property>
    <!--list-->
    <property name="hobbys">
        <list>
            <value>唱</value>
            <value>跳</value>
            <value>篮球</value>
            <value>rap</value>
        </list>
    </property>
    <!--map-->
    <property name="cards">
        <map>
            <entry key="身份证" value="37032319910200817"/>
            <entry key="姓名" value="左志锟"/>
        </map>
    </property>
    <!--集合-->
    <property name="games">
        <set>
            <value>LOL</value>
            <value>CF</value>
            <value>PUBG</value>
        </set>
    </property>
    <!--null-->
    <property name="wife" value=""/>
    <!--property-->
    <property name="info">
        <props>
            <prop key="性别">男</prop>
            <prop key="电话号码">19558986127</prop>
            <prop key="身高">178</prop>
        </props>
    </property>
</bean>

3.拓展方式注入

(1)p命名空间注入

引入配置信息

xmlns:p="http://www.springframework.org/schema/p"

编写bean

<!--p命名空间注入,可以直接注入属性值:property-->
<bean id="user" class="com.zuo.dao.User" p:name="zuo" p:pwd="123456" />

测试

@Test
public void test2(){
    //获取spring容器
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    User user = (User) context.getBean("user");
    System.out.println(user.toString());
}

 官方简介

  

(2)c命名空间注入

引入配置信息

xmlns:c="http://www.springframework.org/schema/c"

编写bean

<!--c命名空间注入,通过构造器注入:constructor-arg-->
<bean id="user" class="com.zuo.dao.User" c:name="song" c:pwd="123456" />

测试

@Test
public void test2(){
    //获取spring容器
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    User user = (User) context.getBean("user");
    System.out.println(user.toString());
}

 官方简介

 

 4.Bean作用域

(1)singleton

  单例模式(spring默认机制,每次从容器中get的对象都是同一个对象)

  显示设置单例模式:

<bean id="user2" class="com.zuo.dao.User" c:name="song" c:pwd="123456" scope="singleton"/>

(2)prototype

  原型模式(每次从容器中get 的对象都为一个新的对象)

  设置原型模式:

<!--c命名空间注入,通过构造器注入:constructor-arg-->
<bean id="user2" class="com.zuo.dao.User" c:name="song" c:pwd="123456" scope="prototype"/>

 

测试:

@Test
public void test3(){

    //获取spring容器
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

    User user1 = (User) context.getBean("user2");
    User user2 = (User) context.getBean("user2");

    System.out.println(user1==user2);
}

 

(3)其他

  request,session,application,websocket只能在web开发中使用!!

 

标签:依赖,return,String,DI,void,private,address,public,注入
From: https://www.cnblogs.com/zuok/p/17053162.html

相关文章

  • windows terminal 配置从当前目录打开 startingDirectory (vscode终端中按下wt后直接打
    我的需求我不想用vscode的终端运行项目,我想在windowsterminal里运行项目我想在vscode的终端里按下wt,然后直接打开一个新的windowsterminal窗口,而且船新的wt窗口的目......
  • SMU Winter 2023 Round #3 (Div.2)
    B.三元组题目:给定一个长度为n的数列a,对于一个有序整数三元组(i,j,k),若其满足1≤i≤j≤k≤n并且ai+aj=ak,则我们称这个三元组是「传智的」。现在请你计算,有......
  • QT之对话框QDialog
    本文章目录​​系列文章目录​​​​一、基本概念​​​​1.1标准对话框​​​​1.2自定义消息框​​​​二、模态对话框​​​​三、非模态对话框​​​​四、消息对话......
  • VC无法打开包括文件:“afxcontrolbars.h”: No such file or directory
       今天用VS2008编译别人的一个MFC工程时,出现了一个错误,提示如下:无法打开包括文件:“afxcontrolbars.h”:Nosuchfileordirectory//截图如下--------------------......
  • nginx配置安全漏洞之CRLF注入漏洞
    CRLF漏洞原理参考:https://www.leavesongs.com/PENETRATION/Sina-CRLF-Injection.htmlCRLF是”回车+换行”(\r\n)的简称。在HTTP协议中,HTTPHeader与HTTPBody是用两个CRL......
  • [disruptor]02-disruptor代码阅读
    环形队列这个环形队列的原理很简单,前面已经介绍过,再次进行简略说明:​​cap​​大小必须是​​2​​的​​N​​次方索引可以一直递增访问元素时索引需要对​​cap​​取模实......
  • [disruptor详解]01-disruptor原理
    ​​disruptor详解​​​​disruptorCpp-IPC​​disruptor原理使用循环队列,且要求队列大小为2的N次方,以满足位运算快速计算索引的要求(比取模速度快)使用原子变量记录生产者......
  • binary.BigEndian.PutUint32
    binary.BigEndian.PutUint32()uint32类型的数字在字节流切片上占4个字节PutUint32()专门用来处理固定长度的数字 相对:binary.BigEndian.Uint32 参考文章:https://w......
  • Android开发学习之路--Android Studio项目目录结构简介
       既然已经搭建好环境了,那就对AndroidStudio中项目目录结构做个简单的了解了,这里以最简单的Hello工程为例子,新建好工程后看如下三个工程视图:1、Android工程manifest......
  • Android开发学习之路--MAC下Android Studio开发环境搭建
       一些资源可以参考如下网站,个人觉得不错:   资源提供网站: ​​http://www.androiddevtools.cn/​​   1、下载androidstudio。  下载好之后解压缩后,打......