Brief description
This tutorial mainly introduces how to extend Spring's XML configuration so that Spring can recognize our customized schema and Annotation.
The functions we want to implement here are as follows: First, let Spring recognize the following configuration.
<std:annotation-endpoint />
The function to implement in this configuration is that after configuration, Spring can scan our customized @Endpoint annotation. And automatically publish WebService services according to the annotations. The function is not fully implemented, and as a tutorial to extend Spring, it plays a role in attracting jade.
Create a project
First, you need to create a Java project, and here you use Maven to create a quickstart project (normal Java project).
The POM file content is as follows
<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> enables your project to publish WebService through annotations, based on Spring+CXF encapsulation, without API intrusion. </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>[email protected]</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/Cod eSTD/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></prope rties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.springframework</gro upId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cx f.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</v ersion><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version><scope>test</scope></dependency></project>Definition 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>
The knowledge about Sechma will not be described here. Friends who don’t know how to use it need to learn about it first. The sechma location is in src/main/resources/META-INF/schema/stdws-1.0.xsd.
Definition annotation
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;/*** Used to expose WebService services, and achieve the purpose of service exposure by adding {@code @Endpoint} annotation to the class. * <p>Extend Spring's bean scanning function. After adding this annotation to the bean, it will be automatically registered in the Spring container. * @author jaune(WangChengwei)* @since 1.0.0*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Endpoint {/*** The ID of this Endpoint in the Spring container* @return*/String id();/*** The address of the service is published, the server address, port number and project path* @return*/String address();}Configuration in Spring
Open "Window", "Preferences", "XML", "XML Catalog". Click "Add", and then select the xsd we created above in Location. "Key type" Select Namespace Name and enter the key into http://www.codestd.com/schema/std/ws/stdws-1.0.xsd. That is, the targetNamespace+ file name defined in Sechma.
Add namespaces in Spring and use tags as follows. Here we need to use Spring's annotation scanning function.
<?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.springframew ork.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>
The package to be scanned is defined in the configuration and does not depend on the configuration with context.
The above is the Spring custom configuration Schema extensible (1) shared by the editor. I hope it will be helpful to everyone.