Spring快速复习--配置文件(二)

125 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第30天,点击查看活动详情

Bean 的依赖注入分析

通过set方法注入

1、在要注入的对象中设置属性和对应的set方法

image-20220609110608148

2、在spring配置文件中赋值

image-20220609110847400

3、测试

image-20220609110347733

有参数构造进行注入

1、在要注入的对象中设置属性和有参构造

为要注入的对象加入有参构造

为后面在Spring配置文件中用有参构造给属性赋值

image-20220609111318844

2、在spring配置文件中赋值

<?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">
​
    <bean id="userDao" class="com.heima.impl.UserDaoImpl">
        <constructor-arg name="name" value="ls"/>
        <constructor-arg name="sex" value="woman"/>
    </bean></beans>

3、测试

image-20220609111127534

注入字面量

null值

和set方法设置属性值一样,设置null值,只需要在property里加两个null标签即可

<bean id="userDao" class="com.heima.impl.UserDaoImpl">
    <property name="name" value="zs"/>
    <property name="sex">
        <null></null>
    </property>
</bean>

属性值包含特殊符号

//方法一:转义字符
<property name="address" value="&lt;北京&dt;"></property>
//方法二:CDATA
<property name="address">
    <value>    <![CDATA[<北京>]]>    </value>
</property>

注入外部bean

不使用Spring情况

image-20220609120545803

使用Spring情况

把依赖对象当成E性并设置它的set方法

在spring配置文件中,我们依然可以用set主入E性的形式注入外部bean

image-20220609120938131

<bean id="UserDaoImpl" class="com.heima.impl.UserDaoImpl">
    <property name="userDaoImpl2" ref="userDaoImpl2"/>
</bean>
​
<bean id="userDaoImpl2" class="com.heima.impl.UserDaoImpl2"/>

image-20220609121131304

自动装配

<bean class="com.itheima.dao.impl.BookDaoImpl"/>
<!--autowire属性:开启自动装配,通常使用按类型装配-->
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>
  • 自动装配用于引用类型依赖注入,不能对简单类型进行操作
  • 使用按类型装配时( byType )确保容器中相同类型的bean唯一,常用
  • 使用按名称装配时( byName )确保容器中唯一名称的bean,因变量名与配置耦合,不推荐使用自动装配
  • 优先级低于setter注入与构造器注入,同时出现时自动装配配置失效

第三方对象管理

前面我们学的都是自己的类啊对象用ioc管理

如果是第三方的类啊对象啊我们怎么管理呢?

下面用druid连接池对象做测试:

1、引入依赖,在spring配置文件创建bean

image-20220610164818000

2、加载配置文件,获取bean

image-20220610165006369

加载properties文件

1、开启context命名空间

image-20220610170147046

2.使用context空问加载properties文件

    <context:property-placeholder location="jdbc.properties"/>

3、使用占位符${}读取properties文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            ">
​
    <context:property-placeholder location="jdbc.properties"/>
​
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
    </bean>
​
</beans>

4、测试

怎么测试它是否读取成功呢?

我们可以写个dao接口和实现类,在实现类中写个属性并给它设置set方法

之后在配置文件中给这个属性通过set注入值为读取的外部jdbc.properties文件的值,看是否打印成功即可

image-20220610171245694

image-20220610171302491

测试成功

image-20220610171318703