006_P名称空间的属性注入
生活随笔
收集整理的這篇文章主要介紹了
006_P名称空间的属性注入
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 通過引入p名稱空間完成屬性的注入
1.1. 普通屬性: p:屬性名="值"。
1.2. 對象屬性: p:屬性名-ref="值"。
2. P名稱空間的引入
3. 使用p名稱空間
4. p標(biāo)簽實例
4.1. 新建一個名為SpringDI_P的Java工程, 拷入Spring相關(guān)包
4.2. 創(chuàng)建Teacher.java
package com.lywgames.bean;public class Teacher {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + "]";}}4.3. 創(chuàng)建Clazz.java
package com.lywgames.bean;public class Clazz {private Teacher teacher;public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}@Overridepublic String toString() {return "Clazz [teacher=" + teacher + "]";}}4.4. 創(chuàng)建Test.java
package com.lywgames;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lywgames.bean.Clazz; import com.lywgames.bean.Teacher;public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(context.getBean(Teacher.class).toString());System.out.println(context.getBean(Clazz.class).toString());context.close();} }4.5. 在src目錄下創(chuàng)建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="teacher" class="com.lywgames.bean.Teacher" p:id="901" p:name="xiaocui"/> <bean id="clazz" class="com.lywgames.bean.Clazz" p:teacher-ref="teacher"/> </beans>4.6. 運行項目
總結(jié)
以上是生活随笔為你收集整理的006_P名称空间的属性注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 005_Spring的属性注入
- 下一篇: 007_SpEL表达式