九点钟☆方向

记录成长点滴

欢迎来到我的个人博客~


Spring笔记(二) Spring 依赖注入

目录

依赖注入的概念

依赖注入DI(Dependency Injection )它是 spring 框架核心 ioc 的具体实现 。

我们的程序在编写时, 通过控制反转, 把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。 ioc 解耦只是降低他们的依赖关系,但不会消除。 例如:我们的业务层仍会调用持久层的方法。那这种业务层和持久层的依赖关系, 在使用 spring 之后, 就让 spring 来维护了。简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

依赖注入的方式

构造方法注入

就是使用类中的构造函数,给成员变量赋值 。

  1. 类:
public class User implements Serializable {
    private String name;
    private Integer age;
    private Date birthday;

    public User(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
}
  1. 配置文件:
<?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="user" class="com.mg.entity.User">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <!--age是Integer类型的,会自动类型转换-->
        <constructor-arg name="age" value="22"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
</beans>

<!--
constructor-arg标签:
属性:
    index:指定参数在构造函数参数列表的索引位置,从0开始
    type:指定参数在构造函数中的数据类型
    name:指定参数在构造函数中的名称(常用)
    =======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
    value:它能赋的值是基本数据类型和 String 类型
    ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
-->

set方法注入

顾名思义,就是在类中提供需要注入成员的 set 方法

  1. 类:
public class User implements Serializable {
    private String name;
    private Integer age;
    private Date birthday;
    //生成set
}
  1. 配置文件:
<?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="user" class="com.mg.entity.User">
        <property name="name" value="张三"></property>
        <property name="age" value="27"></property>
        <property name="birthday" ref="now"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
</beans>

<!--
property标签:
属性:
    name:找的是类中 set 方法后面的部分。如上面类中age自动生成的set方法会是setAge,则name属性写age,
如果手动将set方法改为setUserAge,则name属性的值应为userAge
    ref:给属性赋值是其他 bean 类型的
    value:给属性赋值是基本数据类型和 string 类型的

实际开发中,此种方式用的较多
-->

使用 p 名称空间注入数据(本质还是调用 set 方法)

此种方式是通过在 xml 中导入 p 名称空间,使用 p:propertyName 来注入数据,它的本质仍然是调用类中的 set 方法实现注入功能

  1. public class User implements Serializable {
        private String name;
        private Integer age;
        private Date birthday;
        //生成set
    }
    
  2. 配置文件

    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:p="http://www.springframework.org/schema/p"
           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="user" class="com.mg.entity.User" p:name="张三" p:age="23" p:birthday-ref="now"/>
        <bean id="now" class="java.util.Date"></bean>
    </beans>
    

依赖注入的数据类型

以set注入为例

类:

public class AccountServiceImpl implements IAccountService {
    private String name;
    private Integer age;
    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;
    private IAccountDao accountDao;
    //生成set
}

8种基本类型及包装类+String

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
	<property name="name" value="张三"></property>
    <property name="age" value="21"></property>
</bean>

数组

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
    <property name="myStrs">
        <array>
            <value>AA</value>
            <value>BB</value>
        </array>
    </property>
</bean>

<!--
在注入集合数据时,只要结构相同,标签可以互换,比如array可以换成list或者set
可以互换的   List结构的:array,list,set
			Map结构的:map,props	
比如:
<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
        <property name="name" value="张三"></property>
        <property name="age" value="21"></property>
        <property name="myStrs">
            <list> 此处换成list或者set标签一样可以
                <value>AA</value>
                <value>BB</value>
            </list>
        </property>
    </bean>
-->

List集合

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
    <property name="myList">
        <list>
            <value>1111</value>
            <value>2222</value>
        </list>
    </property>
</bean>

<!--
这里写<value>标签是因为list的泛型是String,如果不带泛型,或者泛型是set则会写成
<property name="myList">
    <list>
        <set>
            <value>111</value>  这里的标签要在根据Set的泛型来定
            <value>222</value>
        </set>
    </list>
</property>
这种情况也适用于其他的标签
-->

set集合

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
    <property name="mySet">
        <set>
            <value>hello</value>
            <value>你好</value>
        </set>
    </property>
</bean>

Map

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
    <property name="myMap">
        <map>
            <entry key="A" value="123"></entry>
            <entry key="B" value="456"></entry>
        </map>
    </property>
    <!--
	<property name="myMap">
        <map>
            <entry key="A">
                <value>123</value>  这种方式也可以,但不如上面简便
            </entry>
            <entry key="B">
                <value>456</value>
            </entry>
        </map>
    </property>
	-->
</bean>

Properties

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
    <property name="myProps">
        <props>
            <prop key="A">AAAA</prop>
            <prop key="B">BBBB</prop>
        </props>
    </property>
</bean>

自建类型

<bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.mg.dao.impl.AccountDaoImpl"></bean>

附:完整配置文件

<?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">
    <!-- 配置 service -->
    <bean id="accountService" class="com.mg.service.impl.AccountServiceImpl">
        <property name="name" value="张三"></property>
        <property name="age" value="21"></property>
        <property name="myStrs">
            <array>
                <value>AA</value>
                <value>BB</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>1111</value>
                <value>2222</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>hello</value>
                <value>你好</value>
            </set>
        </property>
        <property name="myMap">
            <map>
                <entry key="A">
                    <value>123</value>
                </entry>
                <entry key="B">
                    <value>456</value>
                </entry>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key="A">AAAA</prop>
                <prop key="B">BBBB</prop>
            </props>
        </property>
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!-- 配置 dao -->
    <bean id="accountDao" class="com.mg.dao.impl.AccountDaoImpl"></bean>
</beans>

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦