引言: 最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的。回来博客上发现,在自定义标签上,最后一步实现忘记加上了。其实,人生的路程中,我们总是实现着自我的价值,让自己的生活更有意义。

在标签的定义完,也只是自我实现的一半,对于按我们的要求所定义的配置信息,自然而然的需要为这些定义各个属性进行解析和进一步的操作处理了。

进一步问题: 对于前一篇(spring自定义标签之二 —— 规范定义XSD )定义下来的xml的标签定义,如何对其进行解析的问题了。

自定义的标签如下:

  1. <mysql:client id="sqlMapClient" datasouceip="localhsost"  characterEncoding="utf8"
  2. dbname="freebug"   username="root" password="root"
  3. configLocation="classpath:SqlMapCommonConfig.xml" />

     

具体实现:

对于在spring的配置文件中已经进行了声明标签,这些可以上(上一节的规范定义已经说明了)。在上一节中也提到了,需要在资源文件中加入几个文件。

其中springtag.xsd及spring.schemas是为标签定义使用的,而spring.handlers是为了进行声明解释实handler现使用的。

在解析自定义的标签时,对于基本简单的自定义标签可以使用如下方式。继承,两个基类,进行实现。

图1. 实现自定义标签的实现类图

被继承的基类,为spring中带有的基类:

1、NamespaceHandlerSupport

2、AbstractSimpleBeanDefinitionParser

实现类为:

1、TagsNamespaceHandler

  1. package config;
  2. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
  3. /**
  4. * 注册定自义标签对应的解析类
  5. *
  6. * @author sammor
  7. * @date 2011-6-27 上午10:52:44
  8. */
  9. public class TagsNamespaceHandler extends NamespaceHandlerSupport {
  10. @Override
  11. public void init() {
  12. //自定义标签中的element标签名为client解析注册使用MysqlMapClientPraser进行.
  13. registerBeanDefinitionParser("client", new MysqlMapClientPraser());
  14. }
  15. }

2、MysqlMapClientPraser

  1. package config;
  2. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  3. import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
  4. import org.springframework.beans.factory.xml.ParserContext;
  5. import org.springframework.core.io.ClassPathResource;
  6. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  7. import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
  8. import org.springframework.orm.ibatis.SqlMapClientTemplate;
  9. import org.w3c.dom.Element;
  10. /**
  11. * 标签解析处理
  12. *
  13. * @author sammor
  14. * @date 2011-6-27
  15. */
  16. public class MysqlMapClientPraser extends AbstractSimpleBeanDefinitionParser {
  17. /**
  18. * element 相当于对应的element元素 parserContext 解析的上下文 builder 用于该标签的实现
  19. */
  20. @Override
  21. protected void doParse(Element element, ParserContext parserContext,
  22. BeanDefinitionBuilder builder) {
  23. // 从标签中取出对应的属性值
  24. String dbname = element.getAttribute("dbname");
  25. String datasouceip = element.getAttribute("datasouceip");
  26. String username = element.getAttribute("username");
  27. String password = element.getAttribute("password");
  28. String characterEncoding = element.getAttribute("characterEncoding");
  29. String configLocation = element.getAttribute("configLocation");
  30. final String driverClassName = "com.mysql.jdbc.Driver";
  31. // System.out.println("dbname" + dbname);
  32. // System.out.println("datasouceip" + datasouceip);
  33. // System.out.println("username" + username);
  34. // System.out.println("password" + password);
  35. // System.out.println("characterEncoding" + characterEncoding);
  36. // System.out.println("configLocation" + configLocation);
  37. final StringBuffer url = new StringBuffer("jdbc:mysql://");
  38. url.append(datasouceip).append("/").append(dbname).append(
  39. "?useUnicode=true").append("&amp;").append(
  40. "characterEncoding=" + characterEncoding).append(
  41. "&amp;autoReconnect=true");
  42. // 创建 datasource实例
  43. DriverManagerDataSource datasource = new DriverManagerDataSource();
  44. datasource.setDriverClassName(driverClassName);
  45. // System.out.println(url.toString());
  46. datasource.setUrl(url.toString());
  47. datasource.setUsername(username);
  48. datasource.setPassword(password);
  49. // 创建SqlMapClientFactoryBean实例
  50. SqlMapClientFactoryBean sqlmapclient = new SqlMapClientFactoryBean();
  51. sqlmapclient.setDataSource(datasource);
  52. sqlmapclient.setConfigLocation(new ClassPathResource(configLocation));
  53. try {
  54. sqlmapclient.afterPropertiesSet();
  55. } catch (Exception e) {
  56. parserContext.getReaderContext().error(
  57. "sqlmapclient.afterPropertiesSet error", e);
  58. }
  59. // 把创建完的实例对应的传到该标签类实现的相应属性中
  60. builder.addPropertyValue("dataSource", datasource);
  61. builder.addPropertyValue("sqlMapClient", sqlmapclient.getObject());
  62. ;
  63. }
  64. @Override
  65. protected Class getBeanClass(Element element) {
  66. // 返回该标签所定义的类实现,在这里是为了创建出SqlMapClientTemplate对象
  67. return SqlMapClientTemplate.class;
  68. }
  69. }

对标签的实现类写完之后,需要声明该handler。通过spring.handlers 文件进行声明:

  1. http\://sammor.javaeye.com/schema/tags=config.TagsNamespaceHandler

测试环节:

配置完成,进行测试。

1、spring配置文件填写配置信息

  1. <mysql:client id="sqlMapClientTemplate" datasouceip="localhost"
  2. dbname="freebug" characterEncoding="utf8" username="root" password="root"
  3. configLocation="SqlMapCommonConfig.xml" />
  4. <bean id="usersinfoDAO" class="com.dbms.dao.UsersinfoDAOImpl">
  5. <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
  6. </bean>

2、单元测试

  1. ApplicationContext ac = new ClassPathXmlApplicationContext(
  2. "classpath:applicationContext.xml");
  3. UsersinfoDAO user = (UsersinfoDAO) ac.getBean("usersinfoDAO");
  4. System.out.println("记录数:" + user.selectByExample(null).size());

3、测试结果:

  1. 记录数:6

结论

个人觉得自定义标签的应用可以很广,但如何去利用好这个便利才是一个问题,并不是把什么都自定义化才是最好的。自定义标签的目的是为了更好的方便我们的开发,对一些繁琐而又固定的东西,进行一次的封装配置化以减少问题等实现其价值的自我实现。

spring自定义标签之 自我实现的更多相关文章

  1. spring自定义标签之 规范定义XSD

    引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...

  2. spring基础---->spring自定义标签(一)

    Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...

  3. Spring 自定义标签配置

    前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...

  4. Spring自定义标签

    一.原理: 1.Spring通过XML解析程序将其解析为DOM树, 2.通过NamespaceHandler指定对应的Namespace的BeanDefinitionParser将其转换成BeanDe ...

  5. 自己构建一个Spring自定义标签以及原理讲解

    平时不论是在Spring配置文件中引入其他中间件(比如dubbo),还是使用切面时,都会用到自定义标签.那么配置文件中的自定义标签是如何发挥作用的,或者说程序是如何通过你添加的自定义标签实现相应的功能 ...

  6. spring 自定义标签的实现

    在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...

  7. Spring自定义标签解析与实现

           在Spring Bean注册解析(一)和Spring Bean注册解析(二)中我们讲到,Spring在解析xml文件中的标签的时候会区分当前的标签是四种基本标签(import.alias ...

  8. spring自定义标签学习

    看到几篇很全的自定义标签,从定义到使用,写的很好. 这里我也是在那里学习的,对学习spring源码也很有帮助. 贴出来与大家共享. http://sammor.iteye.com/blog/11009 ...

  9. Spring自定义标签的实现

    首先 简单写下 spring xml解析的过程 通过一段简单的 调用spring代码开始 public static void main(String[] args) { ApplicationCon ...

随机推荐

  1. [Erlang16]为什么要用MFA代替fun()–>end?

    MFA:Module Function Arguments. 首先你要知道Module:Func(Args)和Func(Args)的区别在哪里? 如果对细节感兴趣,可以通过这里了解:http://ww ...

  2. Windows store app[Part 1]:读取U盘数据

    Windows 8系统下开发App程序,对于.NET程序员来说,需要重新熟悉下类库. 关于WinRT,引用一张网上传的很多的结构图: 图1 针对App的开发,App工作在系统划定的安全沙箱内,所以通过 ...

  3. win7 64 VC++ ado方式连接access 连接字符串

    运行环境:win7 64       vc++6.0       office 2007  32位(access 2007) 我用的是ado方式连接access数据库,(现在的Win7系统中安装的一般 ...

  4. webapi发布常见错误及解决方案

    webapi发布常见错误及解决方案 错误一: 错误:404 (Not Found) 解决方案: 在  <system.webServer>节点中添加如下模块: <modules ru ...

  5. 洛谷P3643 [APIO2016]划艇(组合数学)

    题面 传送门 题解 首先区间个数很少,我们考虑把所有区间离散化(这里要把所有的右端点变为\(B_i+1\)代表的开区间) 设\(f_{i,j}\)表示考虑到第\(i\)个学校且第\(i\)个学校必选, ...

  6. 【FAQ】maven包引入版本引发的问题

    pom.xml文件中的 dependency顺序可能会引起jar包版本不一致的问题,越上面越先引入进来

  7. ArchLinux下shadow服务报错

    用着Linux蓦然开机就报错了.我是个对报错很敏感的,而是是开机报错. 这个的严重性,听一位前辈说过:如果开机报错你都不理它,慢慢的它就会宕机. 报错内容: shadow服务是Linux下用于校队pa ...

  8. jqury属性操作,特殊效果

    一. 常用属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').html ...

  9. P3952 时间复杂度

    P3952 时间复杂度 题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机 ...

  10. jquery中获取单选标签redio的val

    $('input:radio:checked').val();