Spring-based scalable Schema-based custom configuration (1)
This chapter describes preparations before expansion.
Brief Introduction
This tutorial describes how to extend Spring's xml configuration so that Spring can recognize our custom Schema and Annotation.
Here we want to implement the following functions. First, let Spring recognize the following configurations.
The function of this configuration is to enable Spring to scan our custom@EndpointAnnotation. And automatically publish the WebService Service according to the annotation. The function is not fully implemented. As a Spring extension tutorial, it plays an interesting role.
Create a project
First, create a Java project. Here, use Maven to create a quickstart Project (common Java project ).
The content of the POM file is as follows:
<Code class = "hljs xml"> <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. ar TifactId} </name> <description> allows your project to publish WebService through annotation, which is encapsulated based on Spring + CXF 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> <strong connection> https://github.com/CodeSTD/spring-cxf-annotation-support.git </strong connection> </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> <groupid> org. springframework </groupid> <artifactid> spring-context </artifactid> <version >$ {spring. version }</version> </dependency> <groupid> org. apache. cxf </groupid> <artifactid> cxf-rt-frontend-jaxws </artifactid> <version >$ {cxf. version }</version> </dependency> <groupid> org. apache. cxf </groupid> <artifactid> cxf-rt-transports-http-jetty </artifactid> <version >$ {cxf. version }</version> </dependency> <groupid> org. springframework </groupid> <artifactid> spring-test </artifactid> <version >$ {spring. version }</version> </dependency> <groupid> log4j </groupid> <artifactid> log4j </artifactid> <version> 1.2.14 </version> <scope> test </scope> </dependency> <groupid> org. slf4j </groupid> <artifactid> slf4j-log4j12 </artifactid> <version> 1.7.7 </version> <scope> test </scope> </dependency> </dependencies> </project> </code>Define Schema
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%2D%2D%3E--><xsd:schema attributeformdefault="unqualified" elementformdefault="qualified" targetnamespace="http://www.codestd.com/schema/std/ws" xmlns="http://www.codestd.com/schema/std/ws" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:import namespace="http://www.springframework.org/schema/beans"> <xsd:annotation> <xsd:documentation><!--{cke_protected}{C}%3C!%2D%2D%5BCDATA%5B%20Namespace%20support%20for%20the%20annotation%20provided%20by%20cxf%20framework.%20%5D%5D%2D%2D%3E--></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><!--{cke_protected}{C}%3C!%2D%2D%5BCDATA%5B%20Name%20of%20bean.%20Insted%20of%20id%20%5D%5D%2D%2D%3E--></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="package" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><!--{cke_protected}{C}%3C!%2D%2D%5BCDATA%5B%20Pakeage%20to%20scan.%20%5D%5D%2D%2D%3E--></xsd:documentation> </xsd:annotation> </xsd:attribute> </xsd:extension> </xsd:complexcontent> </xsd:complextype> </xsd:element></xsd:import></xsd:schema></code>
The knowledge about Sechma will not be described here. Friends who will not use it need to learn about it first. Sechma is located in src/main/resources/META-INF/schema/stdws-1.0.xsd.
Define 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 the WebService. The service is exposed by adding the {@ code @ Endpoint} annotation to the class. *
Extends the Bean scanning function of Spring. After this annotation is added to the Bean, it is automatically registered to the Spring container. * @ Author jaune (WangChengwei) * @ since 1.0.0 */@ Target (ElementType. TYPE) @ Retention (RetentionPolicy. RUNTIME) @ brief ented public @ interface Endpoint {/*** ID of this Endpoint in the Spring container * @ return */String id ();/*** address of service release, the server address, port number, and project path * @ return */String address ();}
Configuration in Spring
Open "Window"-"Preferences"-"XML"-"XML Catalog ". Click "Add" and select the xsd created above in Location. Select Namespace Name for "Key type" and enter http://www.codestd.com/schema/std/ws/stdws-1.0.xsd. That is, the targetNamespace + file name defined in Sechma.
Add a namespace in Spring and use labels as follows. Spring annotation scanning is used here.
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%2D%2D%3E--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:std="http://www.codestd.com/schema/std/ws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"></std:annotation-endpoint></beans></code>
The package to be scanned is defined in the configuration and does not depend on the configuration of context.
The preparation is almost complete.