Spring使用C命名空间实现注入数值信息-----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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    第一步,在Spring的配置文件头部添加c空间的抬头-->
<!--    <bean id="People" class="com.powernode.spring6.Bean.People" c:_0="张三" c:_1="20" c:_2="true"/>-->
<!--    <bean id="People1" class="com.powernode.spring6.Bean.People" c:_0="小红" c:_1="20" c:_2="true"/>-->
<!--    C空间注入是基于构造方法的-->
    <bean id="People" class="com.powernode.spring6.Bean.People" c:age="20" c:name="张三" c:sex="true"></bean>
</beans>
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    第一步,在Spring的配置文件头部添加c空间的抬头-->
<!--    <bean id="People" class="com.powernode.spring6.Bean.People" c:_0="张三" c:_1="20" c:_2="true"/>-->
<!--    <bean id="People1" class="com.powernode.spring6.Bean.People" c:_0="小红" c:_1="20" c:_2="true"/>-->
<!--    C空间注入是基于构造方法的-->
    <bean id="People" class="com.powernode.spring6.Bean.People" c:age="20" c:name="张三" c:sex="true"></bean>
</beans>
package com.powernode.spring6.Bean;

public class People
{
    private String name;
    private int age;
    private boolean sex;
    //C命名空间是基于构造方法注入的,c命名空间注入是基于构造方法的
    public People(int age,String name, boolean sex) {
        this.age = age;
        this.name = name;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}
package com.powernode.spring6.Bean;

public class People
{
    private String name;
    private int age;
    private boolean sex;
    //C命名空间是基于构造方法注入的,c命名空间注入是基于构造方法的
    public People(int age,String name, boolean sex) {
        this.age = age;
        this.name = name;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}
    @Test
    public void TestSpecial()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        MathBean MathBean = applicationContext.getBean("MathBean",MathBean.class);
        System.out.println(MathBean);
        applicationContext = new ClassPathXmlApplicationContext("Spring-p.xml");
        System.out.println(applicationContext.getBean("Dog",Dog.class));
        applicationContext = new ClassPathXmlApplicationContext("Spring-c.xml");
        People people = applicationContext.getBean("People", People.class);
        System.out.println(people);
    }
@Test
public void TestSpecial()
{
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
    MathBean MathBean = applicationContext.getBean("MathBean",MathBean.class);
    System.out.println(MathBean);
    applicationContext = new ClassPathXmlApplicationContext("Spring-p.xml");
    System.out.println(applicationContext.getBean("Dog",Dog.class));
    applicationContext = new ClassPathXmlApplicationContext("Spring-c.xml");
    People people = applicationContext.getBean("People", People.class);
    System.out.println(people);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值