引言:
最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的。回来博客上发现,在自定义标签上,最后一步实现忘记加上了。其实,人生的路程中,我们总是实现着自我的价值,让自己的生活更有意义。
在标签的定义完,也只是自我实现的一半,对于按我们的要求所定义的配置信息,自然而然的需要为这些定义各个属性进行解析和进一步的操作处理了。
进一步问题:
对于前一篇(spring自定义标签之二 —— 规范定义XSD
)定义下来的xml的标签定义,如何对其进行解析的问题了。
自定义的标签如下:
<mysql:client id="sqlMapClient" datasouceip="localhsost" characterEncoding="utf8"
dbname="freebug" username="root" password="root"
configLocation="classpath:SqlMapCommonConfig.xml" />
具体实现:
对于在spring的配置文件中已经进行了声明标签,这些可以上(上一节的规范定义已经说明了)。在上一节中也提到了,需要在资源文件中加入几个文件。
其中springtag.xsd及spring.schemas是为标签定义使用的,而spring.handlers是为了进行声明解释实handler现使用的。
在解析自定义的标签时,对于基本简单的自定义标签可以使用如下方式。继承,两个基类,进行实现。
图1. 实现自定义标签的实现类图
被继承的基类,为spring中带有的基类:
1、NamespaceHandlerSupport
2、AbstractSimpleBeanDefinitionParser
实现类为:
1、TagsNamespaceHandler
package config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* 注册定自义标签对应的解析类
*
* @author sammor
* @date 2011-6-27 上午10:52:44
*/
public class TagsNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
//自定义标签中的element标签名为client解析注册使用MysqlMapClientPraser进行.
registerBeanDefinitionParser("client", new MysqlMapClientPraser());
}
}
2、MysqlMapClientPraser
package config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.w3c.dom.Element;
/**
* 标签解析处理
*
* @author sammor
* @date 2011-6-27
*/
public class MysqlMapClientPraser extends AbstractSimpleBeanDefinitionParser {
/**
* element 相当于对应的element元素 parserContext 解析的上下文 builder 用于该标签的实现
*/
@Override
protected void doParse(Element element, ParserContext parserContext,
BeanDefinitionBuilder builder) {
// 从标签中取出对应的属性值
String dbname = element.getAttribute("dbname");
String datasouceip = element.getAttribute("datasouceip");
String username = element.getAttribute("username");
String password = element.getAttribute("password");
String characterEncoding = element.getAttribute("characterEncoding");
String configLocation = element.getAttribute("configLocation");
final String driverClassName = "com.mysql.jdbc.Driver";
// System.out.println("dbname" + dbname);
// System.out.println("datasouceip" + datasouceip);
// System.out.println("username" + username);
// System.out.println("password" + password);
// System.out.println("characterEncoding" + characterEncoding);
// System.out.println("configLocation" + configLocation);
final StringBuffer url = new StringBuffer("jdbc:mysql://");
url.append(datasouceip).append("/").append(dbname).append(
"?useUnicode=true").append("&").append(
"characterEncoding=" + characterEncoding).append(
"&autoReconnect=true");
// 创建 datasource实例
DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setDriverClassName(driverClassName);
// System.out.println(url.toString());
datasource.setUrl(url.toString());
datasource.setUsername(username);
datasource.setPassword(password);
// 创建SqlMapClientFactoryBean实例
SqlMapClientFactoryBean sqlmapclient = new SqlMapClientFactoryBean();
sqlmapclient.setDataSource(datasource);
sqlmapclient.setConfigLocation(new ClassPathResource(configLocation));
try {
sqlmapclient.afterPropertiesSet();
} catch (Exception e) {
parserContext.getReaderContext().error(
"sqlmapclient.afterPropertiesSet error", e);
}
// 把创建完的实例对应的传到该标签类实现的相应属性中
builder.addPropertyValue("dataSource", datasource);
builder.addPropertyValue("sqlMapClient", sqlmapclient.getObject());
;
}
@Override
protected Class getBeanClass(Element element) {
// 返回该标签所定义的类实现,在这里是为了创建出SqlMapClientTemplate对象
return SqlMapClientTemplate.class;
}
}
对标签的实现类写完之后,需要声明该handler。通过spring.handlers
文件进行声明:
http\://sammor.javaeye.com/schema/tags=config.TagsNamespaceHandler
测试环节:
配置完成,进行测试。
1、spring配置文件填写配置信息
<mysql:client id="sqlMapClientTemplate" datasouceip="localhost"
dbname="freebug" characterEncoding="utf8" username="root" password="root"
configLocation="SqlMapCommonConfig.xml" />
<bean id="usersinfoDAO" class="com.dbms.dao.UsersinfoDAOImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
2、单元测试
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
UsersinfoDAO user = (UsersinfoDAO) ac.getBean("usersinfoDAO");
System.out.println("记录数:" + user.selectByExample(null).size());
3、测试结果:
记录数:6
结论
个人觉得自定义标签的应用可以很广,但如何去利用好这个便利才是一个问题,并不是把什么都自定义化才是最好的。自定义标签的目的是为了更好的方便我们的开发,对一些繁琐而又固定的东西,进行一次的封装配置化以减少问题等实现其价值的自我实现。
相关文档:
spring自定义标签之一 —— 意义思考
spring自定义标签之二 —— 规范定义XSD
- 大小: 98.2 KB
分享到:
相关推荐
spring的自定义标签代码工程样例,Chapter2工程
上文我们利用Spring rmi实现了Spring的远程访问(Spring 实现远程访问详解——rmi),本文主要讲解利用HttpInvoke实现远程访问。 Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和...
Spring 5.2.9 06 源码分析-spring自定义标签解析过程
Spring 5.2.9 06 源码分析-spring自定义标签解析过程
用于自定义spring xml标签, 例如 <dubbo:service /> 标签
spring自定义注解样例
主要介绍了这一次搞懂Spring自定义标签以及注解解析原理说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
spring mvc freemarker 自定义标签demo spring mvc 通过自定义的标签在jsp页面调用,初学,记录下做个备忘
实现spring自定义扩展标签的实现步骤
spring自定义切面实例.txt
这一次搞懂Spring自定义标签以及注解解析原理说明.docx
扩展Spring, 自定义标签的实现, 包括 schema配置和自定义标签以及handler的加载过程
1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,Spring支持两个传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的...
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
在spring环境中通过component-scan配置处理自定义java注解
用springMVC+velocity时,当使用自定义标签,发现自定义标签无法注入spring的其它服务类,归根源码发现,velocity的源码中有一段,有一段代码永远获取的是新实例。所以,把这个地方的源码改了,所以使用这个的时候,...
Spring.NET学习笔记22——整合WCF(应用篇) http://www.cnblogs.com/GoodHelper/archive/2010/05/15/SpringNet_Wcf.html
Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice. 1. webservice远程...
刘冬编写的Spring.NET学习笔记21——整合WebService的例子。 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_WebService.html
刘冬编写Spring.NET学习笔记25——整合Quartz.NET例子。 原文: http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_QuartzNet.html