spring源码剖析(三)自定义标签实现及使用

创建pojo类

package net.itaem.vo;

public class User {

	private String name;
	private String sex;
	private String email;
	private String id;
	
	//set get method...
	
}




定义一个xsd文件描述组件类容

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
			targetNamespace="http://www.lexueba.com/schema/user"
			xmlns:tns="http://www.lexueba.com/schema/user"
			elementFormDefault="qualified">


<element name="user">
	<complexType>
		<attribute name="id" type="string" />
		<attribute name="name" type="string" />
		<attribute name="sex" type="string" />
		<attribute name="email" type="string" />
	</complexType>
</element>

</schema>


定义xsd文件的话,比较简单,如果没有学过的同学可以先看看w3cschool的schema教程


实现AbstractSingleBeanDefinitionParser接口

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.itaem.parser;

import net.itaem.vo.User;

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


/**
 * 
 * @author Administrator
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#getBeanClass(org.w3c.dom.Element)
	 */
	@Override
	protected Class<?> getBeanClass(Element element) {
		return  User.class;
	}
	
	//从elelment中解析并提取对应的元素
	@Override
	protected void doParse(Element element, BeanDefinitionBuilder builder) {
	 
		String name=element.getAttribute("name");
		String email=element.getAttribute("sex");
		String sex=element.getAttribute("sex");
		//将提取到的数据放入beanDefinitionBuilder 中,待完成所有的bean解析后统一放到beanfactory
		if(StringUtils.hasText(name)){
			builder.addPropertyValue("name", name);
		}
		if(StringUtils.hasText(email)){
			builder.addPropertyValue("email", email);
		}
		if(StringUtils.hasText(sex)){
			builder.addPropertyValue("sex", sex);
		}
		
	}
	 
}


继承抽象类NamespaceHandlerSupport

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.itaem.handler;

import net.itaem.parser.UserBeanDefinitionParser;

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


/**
 * 
 * @author Administrator
 */
public class MyNamespaceHandler extends NamespaceHandlerSupport {

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.xml.NamespaceHandler#init()
	 */
	@Override
	public void init() {
		registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
	}

}


修改spring.handlers文件和spring.schemas文件

这两个文件默认路径是在META-INF这个路径下面

修改spring.handlers 文件,添加以下内容

http\://www.lexueba.com/schema/user=net.itaem.handler.MyNamespaceHandler

修改spring.schemas文件的内容,添加以下内容

http\://www.lexueba.com/schema/user.xsd=org/springframework/beans/factory/xml/use.xsd



创建测试配置文件

<?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:myname="http://www.lexueba.com/schema/user"

	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.lexueba.com/schema/user http://www.lexueba.com/schema/user.xsd">

	 <myname:user id="beantest" name="whx" email="[email protected]" sex="男"/>	
</beans>


编写测试代码

package net.itaem.test;

import net.itaem.vo.User;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("net/itaem/source/custom_user.xml");
		User bt=(User) context.getBean("beantest");
		System.out.println(bt);
	}
	 
}

运行结果可以看到,程序正常输出bean的信息





你可能感兴趣的:(spring,源代码,it)