Spring自定義配置Schema可擴(kuò)展(一)
簡述
本教程主要介紹如何擴(kuò)展Spring的xml配置,讓Spring能夠識別我們自定義的Schema和Annotation。
這里我們要實(shí)現(xiàn)的功能如下,首先讓Spring能夠識別下面的配置。
<std:annotation-endpoint />
這個配置的要實(shí)現(xiàn)的功能是,配置完后能夠讓Spring掃描我們自定義的@Endpoint注解。并且根據(jù)注解自動發(fā)布WebService服務(wù)。功能未完全實(shí)現(xiàn),作為擴(kuò)展Spring的教程,起一個拋磚引玉的作用。
創(chuàng)建項(xiàng)目
首先需要創(chuàng)建一個Java項(xiàng)目,這里使用Maven創(chuàng)建一個quickstart項(xiàng)目(普通Java項(xiàng)目)。
POM文件內(nèi)容如下
<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>使您的項(xiàng)目可以通過注解的方式發(fā)布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>
關(guān)于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服務(wù),通過在類上加入{@code @Endpoint}注解實(shí)現(xiàn)服務(wù)暴露的目的。
* <p>擴(kuò)展Spring的Bean掃描功能,在Bean上加入此注解后會自動注冊到Spring容器中。
* @author jaune(WangChengwei)
* @since 1.0.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Endpoint {
/**
* 此Endpoint在Spring容器中的ID
* @return
*/
String id();
/**
* 服務(wù)發(fā)布的地址,應(yīng)神略服務(wù)器地址及端口號和項(xiàng)目路徑
* @return
*/
String address();
}
在Spring中的配置
打開“Window”–“Preferences”–“XML”–“XML Catalog”。點(diǎn)擊“Add”,然后在Location中選擇我們上面創(chuàng)建的xsd?!癒ey type”選擇Namespace Name,key輸入http://www.codestd.com/schema/std/ws/stdws-1.0.xsd。即Sechma中定義的targetNamespace+文件名。
在Spring中加入命名空間,并使用標(biāo)簽,如下。這里要用到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/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.codestd.com/schema/std/ws http://www.codestd.com/schema/std/ws/stdws-1.0.xsd"> <std:annotation-endpoint package="com.codestd.spring.cxf.ws"/> </beans>
在配置中定義了要掃描的包,不依賴與context的配置。
以上所述是小編給大家分享的Spring自定義配置Schema可擴(kuò)展(一),希望對大家有所幫助。
相關(guān)文章
Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別
這篇文章主要介紹了Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別,文章為榮啊主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
java數(shù)據(jù)結(jié)構(gòu)與算法之插入排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之插入排序,結(jié)合實(shí)例形式分析了java插入排序的概念、分類、原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
Java日期轉(zhuǎn)換注解配置date?format時間失效
這篇文章主要為大家介紹了Java日期轉(zhuǎn)換注解配置date?format時間失效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
RabbitMq消息防丟失功能實(shí)現(xiàn)方式講解
這篇文章主要介紹了RabbitMq消息防丟失功能實(shí)現(xiàn),RabbitMQ中,消息丟失可以簡單的分為兩種:客戶端丟失和服務(wù)端丟失。針對這兩種消息丟失,RabbitMQ都給出了相應(yīng)的解決方案2023-01-01
POI導(dǎo)出Excel報(bào)錯No such file or directory的解決方法
這篇文章主要為大家詳細(xì)介紹了POI導(dǎo)出Excel報(bào)錯No such file or directory的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Java實(shí)現(xiàn)從jar包中讀取指定文件的方法
這篇文章主要介紹了Java實(shí)現(xiàn)從jar包中讀取指定文件的方法,涉及java針對jar文件的讀取及查找相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Java應(yīng)用服務(wù)器之tomcat部署的詳細(xì)教程
這篇文章主要介紹了Java應(yīng)用服務(wù)器之tomcat部署,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Spring實(shí)戰(zhàn)之屬性占位符配置器用法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之屬性占位符配置器用法,結(jié)合實(shí)例形式分析了spring屬性占位符配置器的具體配置及使用技巧,需要的朋友可以參考下2019-12-12

