Spring自定義配置Schema可擴(kuò)展(一)
簡(jiǎn)述
本教程主要介紹如何擴(kuò)展Spring的xml配置,讓Spring能夠識(shí)別我們自定義的Schema和Annotation。
這里我們要實(shí)現(xiàn)的功能如下,首先讓Spring能夠識(shí)別下面的配置。
<std:annotation-endpoint />
這個(gè)配置的要實(shí)現(xiàn)的功能是,配置完后能夠讓Spring掃描我們自定義的@Endpoint注解。并且根據(jù)注解自動(dòng)發(fā)布WebService服務(wù)。功能未完全實(shí)現(xiàn),作為擴(kuò)展Spring的教程,起一個(gè)拋磚引玉的作用。
創(chuàng)建項(xiàng)目
首先需要?jiǎng)?chuàng)建一個(gè)Java項(xiàng)目,這里使用Maven創(chuàng)建一個(gè)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)目可以通過(guò)注解的方式發(fā)布WebService,基于Spring+CXF封裝,無(wú)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的知識(shí)此處不再贅述,不會(huì)用的小伙伴們需要先去了解下。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ù),通過(guò)在類(lèi)上加入{@code @Endpoint}注解實(shí)現(xiàn)服務(wù)暴露的目的。 * <p>擴(kuò)展Spring的Bean掃描功能,在Bean上加入此注解后會(huì)自動(dòng)注冊(cè)到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ù)器地址及端口號(hào)和項(xiàng)目路徑 * @return */ String address(); }
在Spring中的配置
打開(kāi)“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>
在配置中定義了要掃描的包,不依賴(lài)與context的配置。
以上所述是小編給大家分享的Spring自定義配置Schema可擴(kuò)展(一),希望對(duì)大家有所幫助。
相關(guān)文章
springboot在idea下debug調(diào)試熱部署問(wèn)題
這篇文章主要介紹了springboot在idea下debug調(diào)試熱部署問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Java將CSV的數(shù)據(jù)發(fā)送到kafka的示例
這篇文章主要介紹了Java將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下2020-11-11MyBatis?if?test?判斷字符串相等不生效問(wèn)題
這篇文章主要介紹了MyBatis?if?test?判斷字符串相等不生效問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10開(kāi)發(fā)10年,全記在這本Java進(jìn)階寶典里了
這篇文章主要給大家分享介紹了這本Java進(jìn)階寶典里,是開(kāi)發(fā)10年總結(jié)出來(lái)的,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2019-04-04Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
這篇文章主要為大家介紹了如何利用Java語(yǔ)言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05java后端調(diào)用第三方接口返回圖片流給前端的具體代碼實(shí)現(xiàn)
在前后端分離的開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要從后端返回圖片流給前端的情況,下面這篇文章主要給大家介紹了關(guān)于java后端調(diào)用第三方接口返回圖片流給前端的具體代碼實(shí)現(xiàn),需要的朋友可以參考下2024-02-02