网页游戏开服表弹窗seo推广系统
目录
Spring整合第三方框架
加载外部properties文件
自定义命名空间解析原理
自定义命名空间总结和案例需求
总结
案例
Spring整合第三方框架
加载外部properties文件
- Spring整合第三方框架不像MyBatis那么简单了,例如Dubbo框架在与Spring框架整合时,要使用Dubbo提供的命名空间拓展方式,自定义一些Dubbo的标签,为了降低学习成本,不再引用第三方框架了,以Spring的context命名空间进行讲解,该方式也是命名空间拓展方式。文章传送门:基于Xml方式Bean的配置-命名空间种类_保持敬畏的博客-CSDN博客
- 需求:加载外部peoperties文件,将键值对存储到Spring容器中
- 首先将需要配置的信息存入到创建的properties配置文件中
-
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/db02 jdbc.username=root jdbc.password=123456
-
然后在配置文件中加载创建的配置文件,同时使用其中的配置信息
-
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- todo 加载properties配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><!-- todo 配置数据源信息 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/></bean><!-- 扫描指定的包,产生mapper对象存储到Spring容器中,不需要再配置Mapper对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.example.Mapper"/></bean><bean id="userService" class="com.example.Service.Impl.UserServiceImpl"><property name="empMapper" ref="empMapper"></property><property name="userDAO" ref="userDAO"/><property name="name" value="hhhh"/></bean><bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl"/> </beans>
自定义命名空间解析原理
- 自定义的命名空间中可能会配置很多标签,不同的标签对应不同的解析器,由此我们在创建第三方框架时,要创建META-INF/spring.handlers文件,在该文件中将不同的标签与不同的解析器(Namespacehandler)对应起来,解析器一般都实现对应的接口(NamespaceHandler),在解析器中会重写init()方法(--xxx初始化方法,针对于命名空间中不同的标签,注册属于当前标签的解析器类,该类也实现BeanDefinitionParsee接口,其中也有parse()方法,在Spring解析到对应的标签时就会调用该parse()方法,该方法逻辑自己根据业务需求进行定义,主要就是将某个BeanDefinition对象进行注册,最终存入单例池中,或者向Spring容器中注册一个BeanPostProcessor,对于Spring中配置的bean进行操作xxx--)以及parse()方法(解析方法)parse()方法则主要用于解析具体的标签,将其转换为对应的BeanDefinition对象,并注册到Spring容器中。我们在配置文件中使用自定义命名空间的标签时,会出现约束
这些约束源于我们在配置文件中设置的约束地址
同样的在MATE-INF/spring.schemas文件中进行设置约束地址与文件位置进行映射
自定义命名空间总结和案例需求
总结
- 通过上述分析,外部命名空间标签的执行流程如下:
- 将自定义表标签的约束与物理约束文件与网络约束名称的约束以键值对的形式存储到一个spring.schemas文件中,将文件存储在类加载路径的MATE-INF中,Spring框架会自动加载到
- 将自定义命名空间的名称与自定义命名空间处理器映射关系,以键值对的形式存储到spring.handlers文件中,该文件存储在类加载路径的MATE-INF中,Spring会自动加载到
- 准备好NaspaceHandler,如果命名空间只有一个标签,那么直接在parse()方法中进行解析即可,一般解析结果就是注册对应的BeanDefinition,如果命名空间中有多个标签,那么可以在init方法中为每个标签都注册一个BeanDefinitionParser,在执行NamespaceHandler的parser方法时再分流给不同的BeanDefinitionParser解析解析(重写doParse方法即可)
- ps:讲的有些抽象,具体是跟着课程翻源码看来三四遍才多多少少理解了一些,大家还需要自己去找对应的教程,或者有补充修改意见的欢迎在评论区留言。
案例
- 设想自己是一名架构师,进行某一个框架和Spring的集成开发,效果是通过一个指令标签,向Spring容器中自动注入一个BeanPostProcessor
- 步骤分析
- 确定命名空间名称、schema虚拟路径、标签名称
- 编写schema约束文件haohao-annotation.xsd
- 具体代码如下
-
<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns="http://www.example.com/haohao"xmlns:xsd="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.example.com/haohao"><xsd:element name="annotation-driven"></xsd:element></xsd:schema>
- 在类加载路径下创建META目录,编写约束映射文件spring.schema和处理器映射文件spring.handlers
- spring.schema
-
http\://www.example.com/haohao/haohao-annotation.xsd=com/example/haohao/configure/haohao-annotation.xsd
-
-
spring.handlers
-
http\://www.example.com/haohao=com.example.handlers.HaoHaoNamespaceHandler
-
- spring.schema
- 编写命名空间处理器HaohaoNamespaceHandler,在init()方法中注册HaohaoBeanDefintionparser
-
package com.example.handlers;import org.springframework.beans.factory.xml.NamespaceHandlerSupport;public class HaoHaoNamespaceHandler extends NamespaceHandlerSupport {@Overridepublic void init() {// 初始化,一般情况下,一个命名空间下有多个标签,会在当前init方法中为每一个标签注册一个标签解析器this.registerBeanDefinitionParser("annotation-driven", new HaoHaoBeanDefinitionParser());} }
-
- 编写标签的解析器HaohaoBeanDefinitionPaeser,在parser方法中注册HaohaoBeanPostProcessor
-
package com.example.handlers;import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element;public class HaoHaoBeanDefinitionParser implements org.springframework.beans.factory.xml.BeanDefinitionParser {@Overridepublic BeanDefinition parse(Element element, ParserContext parserContext) {// 注入一个BeanPostProcessorBeanDefinition beanDefinition = new RootBeanDefinition();beanDefinition.setBeanClassName("com.example.PostProcessor.HaoHaoBeanPostProcessor");parserContext.getRegistry().registerBeanDefinition("HaoHaoBeanPostProcessor", beanDefinition);return beanDefinition;} }
-
- 编写HaohaoBeanPostProcessor
-
package com.example.PostProcessor;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor;public class HaoHaoBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("HaoHaoBeanPostProcessor执行......");return bean;} }
-
- ========上述步骤由框架开发者完成,下列步骤由框架使用者完成=======
- 在applicationContext.xml配置文件中引入命名空间
- 在applicationContext.xml配置文件中使用自定义标签