Spring篇 一篇文章带你精通DI依赖注入

常规、Bean、Array、List等注入方式

一、先定义两个实体类
1、User类

下面的实体类上的注解,是我首先在pom文件中引入了Lombok依赖,关于这个依赖的使用,看可以参考我在注解专栏中的文章

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private  String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,Object> cards;
    private Set<String> wife;
    private String food;
    private Properties info;

}

2、Address类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
    private String address;

}

二、编写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.guohui.pojo.Address">
        <property name="address" value="北京王府井"/>
    </bean>
    <bean id="user" class="com.guohui.pojo.User">
        <!--        普通注入 value-->
        <property name="name" value="王五"/>
        <!--        Bean注入 ref-->
        <property name="address" ref="address"/>
        <!--        数组注入 array-->
        <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>
            </list>
        </property>
        <!--       Map注入 map和entry-->
        <property name="cards">
            <map>
                <entry key="身份证" value="123"/>
                <entry key="军官证" value="2213"/>
            </map>
        </property>
        <!--        set注入,value,空注入直接给一个空字符串即可-->
        <property name="wife">
            <set>
                <value>刘诗诗</value>
                <value>Q</value>
            </set>
        </property>
        <!--        NULL注入-->
        <property name="food">
            <null/>
        </property>
        <!--        Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">123123</prop>
                <prop key="姓名">王九</prop>
            </props>
        </property>
    </bean>
</beans>

三、控制台打印结果

C和P标签注入

这种标签注入的方式,现在用的比较少,但是不能排除你将来接触的项目比较老,里面还是用的标签注入,那么还是很有必要了解下

想要用这两个标签进行注入,首先先分别引入两条约束

P标签的:
xmlns:p="http://www.springframework.org/schema/p"
C标签的:
xmlns:c="http://www.springframework.org/schema/c"

实现方式:
一、P标签
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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    引入了P标签的约束后,P标签就相当与Properties标签-->
    <bean id="student" class="com.guohui.pojo.Student" p:name="张三" p:address="北京"/>
    
    </beans>

2、控制台

二、C标签
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"
       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标签的约束后,P标签就相当与Properties标签-->
<!--    <bean id="student" class="com.guohui.pojo.Student" p:name="张三" p:address="北京"/>-->

<!--    C标签就相当与有参构造器注入的方式-->
  <bean id="student" class="com.guohui.pojo.Student" c:name="李四" c:address="河北"/>

</beans>

2、控制台


注意:想要使用P和C两个标签,首先必须在配置文件上导入约束!!!

至此,关于Spring的依赖注入就讲解完毕,后续会持续更新,欢迎交流和指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Be explorer

若认可笔者文章,手头富裕望支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值