簡述
本教程主要介紹如何擴展Spring的xml配置,讓Spring能夠識別我們自定義的Schema和Annotation。
這里我們要實現的功能如下,首先讓Spring能夠識別下面的配置。
<std:annotation-endpoint />
這個配置的要實現的功能是,配置完后能夠讓Spring掃描我們自定義的@Endpoint注解。并且根據注解自動發布WebService服務。功能未完全實現,作為擴展Spring的教程,起一個拋磚引玉的作用。
創建項目
首先需要創建一個Java項目,這里使用Maven創建一個quickstart項目(普通Java項目)。
POM文件內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?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。
定義注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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) @Documented public @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的注解掃描功能。
1
2
3
4
5
6
7
8
9
10
|
<?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可擴展(一),希望對大家有所幫助。