【Spring第四篇】DI注入以及c、p命名空间

本文详细介绍了如何在Spring框架中使用DI(依赖注入)进行实体类的bean配置,包括p命名空间直接注入属性值和c命名空间通过构造器注入。通过示例展示了User、Address和Student实体类的注入方式,涵盖了基本类型、数组、集合及复杂类型的注入,并提供了测试代码以验证配置效果。此外,还讲解了Spring的singleton和prototype两种作用域的区别。
摘要由CSDN通过智能技术生成

DI注入以及c、p命名空间


编写实体类

Address

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

}

Student

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String, String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

}

User

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private String name;
    private int age;

}

将实体类都注入到bean中,编程一个个bean

p命名空间、c命名空间

userbean.xml 用于将实体类User注入到spring容器中,交给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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<!--    p 命名空间注入,可以直接注入属性的值 property-->
    <bean id="user" class="com.kk.pojo.User" p:name="王五" p:age="22"/>


    
<!--    c 命名空间注入  需要构造器  construct-args -->
<!--    singleton 单例模式  Spring的默认机制-->
<!--    prototype原型模式 每次从容器中get的时候,每一次都会产生一个新对象 -->
    <bean id="user2" class="com.kk.pojo.User" c:name="赵六" c:age="20" scope="prototype"/>

</beans>

注意:需要导入以下xmlns

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

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

p命名空间

 p 命名空间注入,可以直接注入属性的值 property

c命名空间

c 命名空间注入  需要构造器  construct-args 
singleton 单例模式  Spring的默认机制
prototype原型模式 每次从容器中get的时候,每一次都会产生一个新对象 

测试各种类型的注入

仔细阅读注解

beans.xml 用于将实体类Address,Student注入到spring容器中,交给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-3.0.xsd">


    <bean id="address" class="com.kk.pojo.Address">
        <property name="address" value="广东"/>
    </bean>

    <bean id="student" class="com.kk.pojo.Student">
<!--        第一种:普通值注入,使用value注入-->
        <property name="name" value="小六"/>

<!--        第二种:bean注入,使用ref,其中name里边的参数是写bean中的id参数-->
        <property name="address" ref="address"/>


<!--        数组注入-->
        <property name="books">
            <array>
                <value>Java程序设计</value>
                <value>SSM</value>
                <value>微服务</value>
            </array>
        </property>


<!--        List注入-->
        <property name="hobbys">
            <list>
                <value>敲代码</value>
                <value>赚钱</value>
                <value>唱歌</value>
                <value>运动</value>
            </list>
        </property>


<!--        map注入-->
        <property name="card">
            <map>
                <entry key="学号" value="2011101**"/>
                <entry key="身份证" value="4418 **** **** ****..."/>
            </map>
        </property>

<!--        set注入-->
        <property name="games">
            <set>
                <value>CSOL</value>
                <value>王者</value>
            </set>
        </property>


<!--        空 null 注入-->
        <property name="wife">
            <null/>
        </property>

<!--        Properties注入-->
        <property name="info">
            <props>
                <prop key="邮箱">30666@qq.com</prop>
                <prop key="qq">30666</prop>
                <prop key="性别"></prop>
            </props>
        </property>
        
    </bean>



</beans>

property中的name里边的参数对应的是实体类中的属性字段


测试:

public class Test {

    @org.junit.Test
    public void testAddress(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Address address = (Address) context.getBean("address");
        System.out.println(address);
    }


    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());

    }


    @org.junit.Test
    public void test2(){
      ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user2", User.class);
        System.out.println(user==user2);//结果为  false 这就是单例模式 singleton

    }

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-si7vZOEQ-1649573024969)(C:\Users\30666\AppData\Roaming\Typora\typora-user-images\image-20220310143143176.png)]


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不易撞的网名

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值