依赖注入02(Dependency Injection,DI)

本文介绍了Spring框架中的构造器注入(c命名空间)与属性注入(p命名空间)在bean.xml配置中的应用,通过实例演示了如何在User类中分别使用这两种方式设置初始值,以及如何在测试类中获取并打印这些注入的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2、构造器注入

在我的前面博客里面,讲到了构造器。
参考:https://blog.youkuaiyun.com/beyond_csdn/article/details/115098064

3、拓展方式注入

3.1、p命名空间注入

  1. 编写实体类;

    package com.beyond.pojo;
    
    public class User {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
  2. 编写bean.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"
           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命名空间注入,可以直接注入属性值相当于property-->
        <bean id="user" class="com.beyond.pojo.User" p:name="遇见狂神说" p:age="16"/>
    
    </beans>
    
  3. 测试。

    import com.beyond.pojo.Student;
    import com.beyond.pojo.User;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        @Test
        public void test2(){
            ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
            User user = context.getBean("user", User.class);
            System.out.println(user);
        }
    }
    

3.2、c命名空间

  1. 编写实体类;

    package com.beyond.pojo;
    
    public class User {
        private String name;
        private int age;
    
        public User() {
        }
    
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
  2. 编写bean.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"
           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">
    
        <!--c命名空间注入,通过构造器注入相当于constructor-arg-->
        <bean id="user2" class="com.beyond.pojo.User" c:_0="秦疆" c:age="18"/>
        
    </beans>
    
  3. 测试。

    import com.beyond.pojo.Student;
    import com.beyond.pojo.User;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        @Test
        public void test2(){
            ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
            User user = context.getBean("user2", User.class);
            System.out.println(user);
        }
    }
    

注意:p命名空间和c命名空间不能直接使用,需要在bean.xml中导入约束!

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值