【SSM基础知识3】spring3

目录

Spring整合其他组件

Spring xml方式整合第三方框架


Spring整合其他组件

   例如Dubbo框架在于Spring进行整合时,要使用Dubbo提供的命名空间的扩展方式,自定义了一些Dubbo的标签

<?xml version="1.0" encoding="UrF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!--配置应用名称-->

<dubbo:application name="dubbo1-consumer"/>

<!--配置注册中心地址-->

<dubbo:registry address="zookeeper://localhost:2181"/>

<!--扫描dubbo的注解-->

<dubbo:annotation package="com.itheima.controller"/>

<!--消费者配置-->

<dubbo:consumer check="false"timeout="1000"retries="0"/>

</beans>

Spring的context 命名空间

需求:加载外部properties文件,将键值对存储在Spring容器中

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=你数据库密码
jdbc.url=jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=utf8

applicationcontext.xml

 <!--加载properties文件-->

<context:property-placeholderlocation="classpath:jdbc.properties"/>

<!--配置数据源信息-->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

<property name="driverClassName" value="${jdbc.driver}"></property>

<property name="url" value="${jdbc.url}"></property>

<property name="username" value="${jdbc.username}"></property>

<property name="password" value="${jdbc.password}"></property >

</bean>

 案例

进行某一个框架与Spring的集成开发,效果是通过一个指示标签,向Spring容器中自动注入一个BeanPostProcessor

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/xm1Schema-instance"

xmlns:haohao="http://www.ting.com/haohao"

xsi:schemaLocation="http://www.springframework.org/schema/beans

                    http://www.springframework.org/schema/beans/spring-beans.xsd

                    http://www.ting.com/haohao

                    http://www.ting.com/haohao/haohao-annotation.xsd"> 

        <haohao:annotation-driven/>

</beans>

Spring xml方式整合第三方框架

步骤分析:

1.确定命名空间名称、schema虚拟路径、标签名称;

2.编写schema约束文件haohao-annotation.xsd

3.在类加载路径下创建META-INF目录,编写约束映射文件spring.schemas和处理器映射文件spring.handlers

4.编写命名空间处理器HaohaoNamespaceHandler,在init方法中注册HaohaoBeanDefinitionParser

5.编写标签的解析器 HaohaoBeanDefinitionParser,在parse方法中注册HaohaoBeanPostProcessor

6.编写HaohaoBeanPostProcessor

==========以上五步是框架开发者写的,以下是框架使用者写的===========

1.在applicationContext.xml配置文件中引入命名空间

2. 在applicationContext.xml配置文件中使用自定义的标签

1.资源下com/ting/haohao/config

haohao-annotation.xsd

 <?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns="http://www.ting.com/haohao"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.ting.com/haohao">

   <xsd:element name="annotation-driven"></xsd:element>

</xsd:schema>

2.META-INF目录

spring.handles文件

http\://www.ting.com/haohao/haohao-annotation.xsd=com/ting/haohao/config/haohao-annotation.xsd

http\://www.ting.com/haohao=com.ting.handler.HaohaoNamespaceHandler

3.java包下handlers包HaohaoNamespaceHandles

public class HaohaoNamespaceHandles extends NamespaceHandlerSupport{
  //初始化
    @Override
    public void init(){
       this.registerBeanDefinitionParser("annotation-driven", new HaohaoBeanDefinitionParser() );

    }

}

4.handlers包HaohaoBeanDefinitionParser

public class HaohaoBeanDefinitionParser implements BeanDefinitionParser {
   public BeanDefinition parse(Element element,ParserContext parserContext) {
   //创建HaohaoBeanPostProcessor的BeanDefinition
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(HaohaoBeanPostProcessor.class);
    //注册HaohaoBeanPostProcessor
          parserContext.getRegistry().reqisterBeanDefinition("haohaoBeanPostProcessor",beanDefinition); 
    return beanDefinition;
    }

}

5.processor包HaohaoBeanPostProcessor

 public class HaohaoBeanPostProcessor implements BeanPostProcessor {

  @Override 

     public Object postProcessAfterInitialization(Object bean, String beanName){
                System.out.println("HaohaoBeanPostProcessor执行....");
    }

    return bean;

    }

} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值