<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 第一步,在Spring的头部添加p命名空间,这就引入了P命名空间了,可以使用P命名空间了--> <!-- p冒号加属性值就可以赋值使用了--><bean id="Dog" class="com.powernode.spring6.Bean.Dog" p:age="3" p:name="小花" p:birth-ref="Date"/><bean id="Date" class="java.util.Date"/> </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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 第一步,在Spring的头部添加p命名空间,这就引入了P命名空间了,可以使用P命名空间了--> <!-- p冒号加属性值就可以赋值使用了--><bean id="Dog" class="com.powernode.spring6.Bean.Dog" p:age="3" p:name="小花" p:birth-ref="Date"/><bean id="Date" class="java.util.Date"/> </beans>
package com.powernode.spring6.Bean;import java.util.Date;public class Dog {private String name;private int age;private Date birth;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", birth=" + birth +'}';}//p空间注入底层还是set注入,只不过p命名空间注入可以让spring配置变得更简单,一旦set方法不存在就会报错了public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setBirth(Date birth) {this.birth = birth;} }package com.powernode.spring6.Bean;import java.util.Date;public class Dog {private String name;private int age;private Date birth;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", birth=" + birth +'}';}//p空间注入底层还是set注入,只不过p命名空间注入可以让spring配置变得更简单,一旦set方法不存在就会报错了public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setBirth(Date birth) {this.birth = birth;} }
@Testpublic 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));}@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)); }