简述
本教程主要介绍如何扩展Spring的xml配置,让Spring能够识别我们自定义的Schema和Annotation。
这里我们要实现的功能如下,首先让Spring能够识别下面的配置。
<std:annotation-endpoint />
这个配置的要实现的功能是,配置完后能够让Spring扫描我们自定义的@Endpoint注解。并且根据注解自动发布WebService服务。功能未完全实现,作为扩展Spring的教程,起一个抛砖引玉的作用。
创建项目
首先需要创建一个Java项目,这里使用Maven创建一个quickstart项目(普通Java项目)。
POM文件内容如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.codestd</groupId><artifactId>spring-cxf-annotation-support</artifactId><version>1.0.0-SNAPSHOT</version><name>${project.artifactId}</name><description>使您的项目可以通过注解的方式发布WebService,基于Spring+CXF封装,无API侵入。</description><url>https://github.com/CodeSTD/spring-cxf-annotation-support</url><licenses><license><name>The Apache License, Version 2.0</name><url>http://www.apache.org/licenses/LICENSE-2.0.txt</url></license></licenses><developers><developer><name>jaune(WangChengwei)</name><email>jaune162@126.com</email><roles><role>developer</role></roles><timezone>GMT+8</timezone></developer></developers><scm><connection>https://github.com/CodeSTD/spring-cxf-annotation-support.git</connection><developerConnection>https://github.com/CodeSTD/spring-cxf-annotation-support.git</developerConnection></scm><properties><junit.version>4.12</junit.version><spring.version>4.2.4.RELEASE</spring.version><cxf.version>3.1.3</cxf.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.14</version><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version><scope>test</scope></dependency></dependencies></project>
定义Schema
<?xml version="1.0" encoding="UTF-8" standalone="no"?><xsd:schema xmlns="http://www.codestd.com/schema/std/ws" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:beans="http://www.springframework.org/schema/beans"targetNamespace="http://www.codestd.com/schema/std/ws"elementFormDefault="qualified"attributeFormDefault="unqualified"><xsd:import namespace="http://www.springframework.org/schema/beans"/><xsd:annotation><xsd:documentation><![CDATA[ Namespace support for the annotation provided by cxf framework. ]]></xsd:documentation></xsd:annotation><xsd:element name="annotation-endpoint"><xsd:complexType><xsd:complexContent><xsd:extension base="beans:identifiedType"><xsd:attribute name="name" type="xsd:string" use="optional"><xsd:annotation><xsd:documentation><![CDATA[ Name of bean. Insted of id ]]></xsd:documentation></xsd:annotation></xsd:attribute><xsd:attribute name="package" type="xsd:string" use="optional"><xsd:annotation><xsd:documentation><![CDATA[ Pakeage to scan. ]]></xsd:documentation></xsd:annotation></xsd:attribute></xsd:extension></xsd:complexContent></xsd:complexType></xsd:element></xsd:schema>
关于Sechma的知识此处不再赘述,不会用的小伙伴们需要先去了解下。sechma位置在src/main/resources/META-INF/schema/stdws-1.0.xsd。
定义注解
package com.codestd.spring.cxf.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/*** 用于暴露WebService服务,通过在类上加入{@code @Endpoint}注解实现服务暴露的目的。* <p>扩展Spring的Bean扫描功能,在Bean上加入此注解后会自动注册到Spring容器中。* @author jaune(WangChengwei)* @since 1.0.0*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Endpoint {/*** 此Endpoint在Spring容器中的ID* @return*/String id();/*** 服务发布的地址,应神略服务器地址及端口号和项目路径* @return*/String address();}
在Spring中的配置
打开“Window” “Preferences” “XML” “XML Catalog”。点击“Add”,然后在Location中选择我们上面创建的xsd。“Key type”选择Namespace Name,key输入http://www.codestd.com/schema/std/ws/stdws-1.0.xsd。即Sechma中定义的targetNamespace+文件名。
在Spring中加入命名空间,并使用标签,如下。这里要用到Spring的注解扫描功能。
<?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:std="http://www.codestd.com/schema/std/ws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.codestd.com/schema/std/wshttp://www.codestd.com/schema/std/ws/stdws-1.0.xsd"><std:annotation-endpoint package="com.codestd.spring.cxf.ws"/></beans>
在配置中定义了要扫描的包,不依赖与context的配置。
以上所述是小编给大家分享的Spring自定义配置Schema可扩展(一),希望对大家有所帮助。
新闻热点
疑难解答