Spring源碼學習——自定義標籤

1.自定義標籤步驟

  1. 建立一個須要擴展的組件
  2. 定義xsd文件描述組件內容
  3. 建立一個文件,實現BeanDefinitionParser接口,解析xsd文件中的定義和組件定義
  4. 建立handler文件,擴展NamespaceHandlerSupport,註冊組件到spring容器
  5. 編寫spring.handlers和spring.schemas文件

2.代碼以下

1.編寫pojo

public class User {
	
	private String name;
	private String sex;
	private int age;
//省略getter、setter

}

2.xsd文件描述組件內容

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.springtest.com/schema/user"
	xmlns:tns="http://www.springtest.com/schema/user" elementFormDefault="qualified">
	<!-- 表示數據類型等定義來自w3 -->
	<!--表示文檔中要定義的元素來自什麼命名空間 -->
	<!--表示此文檔的默認命名空間是什麼 -->
	<!--表示要求xml文檔的每個元素都要有命名空間指定 -->

	<!-- ……定義主體部分…… -->
	<element name="user">
		<complexType>
			<attribute name="id" type="string"></attribute>
			<attribute name="name" type="string"></attribute>
			<attribute name="sex" type="string"></attribute>
			<attribute name="age" type="int"></attribute>
		</complexType>
	</element>

</schema>

描述了一個新的targetNamespace,並定義了一個name是user的element,有id,name,sex,age屬性java

3.建立類,實現BeanDefinitionParser接口

package test.customtag;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

import com.model.User;

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
	// Element對應的類
	protected Class getBeanClass(Element element) {
		return User.class;
	}

	// 從element中解析並提取對應的元素
	protected void doParse(Element element, BeanDefinitionBuilder bean) {
		String name = element.getAttribute("name");
		String sex = element.getAttribute("sex");
		String age = element.getAttribute("age");
		// 將提取的數據放入到BeanDefinitionBuilder中,將全部beanbeanFactory中
		if (StringUtils.hasText(name)) {
			bean.addPropertyValue("name", name);
		}
		if (StringUtils.hasText(sex)) {
			bean.addPropertyValue("sex", sex);
		}
		if (StringUtils.hasText(age)) {
			bean.addPropertyValue("age", Integer.parseInt(age));
		}

	}

}

4.建立handler文件,註冊spring容器

package test.customtag;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

/******建立handler文件,組件註冊到spring容器***/
public class MyNamespaceHandler extends NamespaceHandlerSupport{

	@Override
	public void init() {
		// TODO Auto-generated method stub
		registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
	}

}

5.編寫spring.handlers和spring.schemas文件,默認在工程的/META-INF/文件下

spring.handlersspring

http\://www.springtest.com/schema/user=test.customtag.MyNamespaceHandler

spring.schemasapp

http\://www.springtest.com/schema/user.xsd=META-INF/Spring-test.xsd

此處注意dom

這裏由於我建立的是java項目,直接在項目下建造META-INF會提示找不到對應的文件,因此這裏是將文件打包成jar包導入到項目中。以下圖所示ide

 

 

 

6.測試

導入自定義標籤測試

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:myname="http://www.springtest.com/schema/user"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
        http://www.springtest.com/schema/user
       	http://www.springtest.com/schema/user.xsd
        ">
	<myname:user id="testBean" name="aaaaaa" sex="dsaf" age="12"></myname:user>
</beans>

 

測試代碼ui

public class Test {
	/****測試輸出*/
	@org.junit.Test
	public void test1(){
		System.out.println("--------");
		ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext-service.xml");
		User u=(User) act.getBean("testBean");
		System.out.println("--------------"+u.toString());
	}
}

輸出結果spa

3.整個項目結構

 

參考自:spring源碼深度解析code

相關文章
相關標籤/搜索