<?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);
}