Spring02-配置及依赖注入(DI)

官方文档:
https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/index.html

Alias别名:

如果添加了别名,我们就可以用别名或原名获取到这个对象

<alias name="user" alias="QWER"/>

Bean的配置:

id:bean的唯一标识符,也就是相当于对象名
class:bean对象所对应的全限定名:包名+类名
name:也是别名,可以同时取多个别名(用逗号、分号或空格分隔)

<bean id="user" class="com.it.pojo.User" name="u2,u3 u4;u5">
    ...
</bean>

import:

导入其他配置文件
一般用于团队开发,可以将多个配置文件合并为一个




依赖注入:

1)构造器注入:

涉及的标签:
用来定义构造函数的参数,其属性可大致分为两类:

寻找要赋值给的字段
index: 指定参数在构造函数参数列表的索引位置
type: 指定参数在构造函数中的数据类型
name: 指定参数在构造函数中的变量名,最常用的属性
指定赋给字段的值
value: 给基本数据类型和String类型赋值
ref: 给其它Bean类型的字段赋值,ref属性的值应为配置文件中配置的Bean的id

<!-- 使用Date类的无参构造函数创建Date对象 -->
<bean id="now" class="java.util.Date" scope="prototype"></bean>

<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl">
	<constructor-arg name="name" value="myname"></constructor-arg>
	<constructor-arg name="age" value="18"></constructor-arg>
	<!-- birthday字段为已经注册的bean对象,其id为now -->
	<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>

2)set方式注入(重要):

涉及的标签:
,用来定义要调用set方法的成员. 其主要属性可大致分为两类:

指定要调用set方法赋值的成员字段
name:要调用set方法赋值的成员字段
指定赋给字段的值
value: 给基本数据类型和String类型赋值
ref: 给其它Bean类型的字段赋值,ref属性的值应为配置文件中配置的Bean的id

实体类:

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 Properties info;
    private String wifi;
    
    getter...
    setter...
    toString...

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
         http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 普通值注入,直接使用value赋值 -->
    <bean id="student" class="com.it.pojo.Student">
        <property name="name" value="小明"/>
    </bean>
</beans>
<bean id="address" class="com.it.pojo.Address"/>
<bean id="student" class="com.it.pojo.Student">
    <!-- 普通值注入,直接使用value赋值 -->
    <property name="name" value="小明"/>

    <!-- bean注入,引用类型需要通过ref引用 -->
    <property name="address" ref="address"/>

    <!-- 数组注入 -->
    <property name="books">
        <array>
            <value>西游记</value>
            <value>三国演义</value>
        </array>
    </property>

    <!-- List注入 -->
    <property name="hobbies">
        <list>
            <value>学习</value>
            <value>学习</value>
            <value>学习</value>
        </list>
    </property>

    <!-- set注入 -->
    <property name="games">
        <set>
            <value>LOL</value>
            <value>COD</value>
            <value>FN</value>
        </set>
    </property>

    <!-- Map注入 -->
    <property name="card">
        <map>
            <entry value="手机号码" key="15521259749"/>
            <entry value="学生号" key="18551119015"/>
        </map>
    </property>

    <!-- null注入 -->
    <property name="wifi">
        <null/>
    </property>
    
    <!-- Properties注入 -->
    <property name="info">
        <props>
            <prop key="driver">...</prop>
            <prop key="url">...</prop>
            <prop key="username">...</prop>
            <prop key="password">...</prop>
        </props>
    </property>
</bean>

3)拓展方式注入:

p命名空间:

The p-namespace lets you use the bean element’s attributes (instead of nested elements) to describe your property values collaborating beans, or both.
通过p-namespace,可以(直接)使用bean元素的属性(而不是嵌套的元素)来描述与bean协作的属性值

使用:
首先在xml配置文件中导入xml约束

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

直接在bean标签中调用属性

<bean id="user" class="com.it.pojo.User" p:age="15" p:name="小明"/>
<bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="someone@somewhere.com"/>
    </bean>

    <bean name="p-namespace" class="com.example.ExampleBean"
        p:email="someone@somewhere.com"/>

两者等价


c命名空间:

Similar to the p-namespace, the c-namespace, introduced in Spring 3.1, allows inlined attributes for configuring the constructor arguments rather then nested constructor-args elements.
通过构造方法来注入参数

使用:
在xml配置文件中导入xml约束

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

_0,_1代表构造方法中的参数顺序

<bean id="user2" class="com.it.pojo.User" c:_0="小红" c:_1="15"/>

Bean的作用域:

在这里插入图片描述
1)单例模式(singleton scope)(默认)
在这里插入图片描述
2)原型模式(prototype scope)
每次从容器中getBean时都会创建一个新的对象
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值