Spring Bean 命名空间
1. 介绍¶
通过命名空间为bean赋值,简化配置文件中属性声明的写法
2. 导入命名空间¶
==xmlns:p="http://www.springframework.org/schema/p"==
<?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">
3. 添加配置¶
<!--命名空间--> <bean id="person8" class="com.cmz.bean.Person" p:id="5" p:name="王五" p:age="25" p:gender="男" p:date="2020/02/28"></bean>
4. 示例¶
ioc5.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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--命名空间--> <bean id="person" class="com.cmz.bean.Person" p:id="5" p:name="王五" p:age="25" p:gender="男" p:date="2020/02/28"></bean> </beans>
MyTest5.class
import com.cmz.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author summer * @create 2020-02-22 23:45 */ public class MyTest5 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ioc5.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); } }
输出
Person{id=5, name='王五', age=25, gender='男', date=Fri Feb 28 00:00:00 CST 2020}