spring的各项配置、三种注入方式(Set注入、构造器注入,扩展方式注入)、bean的作用域

spring中的配置

在这里插入图片描述

1. bean

bean就是由ioc容器实例化的对象

2. beans

beans标签用于包含bean标签

3. alias

在这里插入图片描述

为bean的id取别名,别名可以有多个用 ;隔开

4. import

导入其他的xml配置文件,导入后只用读取本配置文件也可使用其他文件中的bean

5. description

添加一些javabean的描述信息,用处不大

spring注入

1. Set注入(最重要)

1.1、普通值注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VyEV4EIE-1659106612103)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729213829696.png)]

通过value注入

1.2、Bean注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZZ3lza6d-1659106612104)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729221807235.png)]

注入的bean要在配置文件中已经注册

1.3、数组、List、Set注入

在这里插入图片描述

结构都一样,只用修改property下面的标签名称即可

1.4、Map注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eJSBpTJ4-1659106612105)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729222106244.png)]

与上面三种不同的只是在map下用entry标签表示键值对

1.5、Properties

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xznF8Xza-1659106612106)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729222228060.png)]

与Map注入不同的是值需要写到prop括号中

1.6、空值注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q2frshsV-1659106612106)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729222347073.png)]

在该属性下直接写

2. 构造器注入

在这里插入图片描述

  • name是选择参数的名字然后用value注入
  • index是参数的顺序注入,第一个参数为0
  • type根据参数的类型注入

3. 扩展方式注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-etCOSGgJ-1659106612108)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729223009903.png)]

在spring官网找到相应的头文件导入,然后就可以直接在bean中用c命名空间注入或者p命名空间注入

网址:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-c-namespace

  • 注意:使用c命名空间注入时必须显示地定义有参、无参构造

项目结构:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E6jZsFFX-1659106612108)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729224059394.png)]

实体类address:

package com.yang.pojo;

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:

package com.yang.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    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> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    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 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.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

实体类user:

package com.yang.pojo;

public class User {
    private String name;
    private int age;

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

核心配置文件beans.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">
    <bean id="address" class="com.yang.pojo.Address">
        <property name="address" value="南充"/>
    </bean>
    <bean id="student" class="com.yang.pojo.Student">
        <!--    普通值注入  value  -->
        <property name="name" value="秀儿"/>
        <!--    bean注入  ref  -->
        <property name="address" ref="address"/>
        <!--    数组注入    -->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>红楼梦</value>
            </array>
        </property>
        <!--    List注入    -->
        <property name="hobbies">
            <list>
                <value>听音乐</value>
                <value>看电影</value>
                <value>打游戏</value>
                <value>敲代码</value>
            </list>
        </property>
        <!--    Set注入    -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CF</value>
                <value>QQ飞车</value>
            </set>
        </property>
        <!--    null    -->
        <property name="wife">
            <null/>
        </property>
        <!--    Map注入    -->
        <property name="card">
            <map>
                <entry key="饭卡" value="123"/>
                <entry key="水卡" value="456"/>
                <entry key="电卡" value="789"/>
            </map>
        </property>
        <!--    Properties    -->
        <property name="info">
            <props>
                <prop key="学号">20190001</prop>
                <prop key="性别"></prop>
                <prop key="姓名">秀儿</prop>
            </props>
        </property>
    </bean>

</beans>

beans01.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--  p 命名空间 注入 -->
    <bean id="user" class="com.yang.pojo.User" p:age="18" p:name="秀儿"/>
<!-- c 命名空间 注入 -->
    <bean id="user01" class="com.yang.pojo.User" c:age="20" c:name="秀哥"/>
    <alias name="user" alias="user2;user3"/>
</beans>

测试:

import com.yang.pojo.Student;
import com.yang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void Name(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);

    }
    @Test
    public void User(){
       ApplicationContext context = new ClassPathXmlApplicationContext("beans01.xml");
        User user = context.getBean("user01", User.class);
        System.out.println(user);


    }
}

bean的作用域

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2eoIN7Mr-1659106612109)(C:/Users/%E4%B8%83%E9%87%8C/AppData/Roaming/Typora/typora-user-images/image-20220729225128045.png)]

1. 单例模式(默认机制)

在这里插入图片描述

这两个get到的是同一个对象

    <bean id="student" class="com.yang.pojo.Student" scope="singleton">

2. 原型模式

这两个get到的不同的对象,每次get都会产生一个新对象

    <bean id="student" class="com.yang.pojo.Student" scope="prototype">

3. 后面的request、session、application、websocket只在web开发中使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值