Brève description
Ce tutoriel présente principalement comment étendre la configuration XML de Spring afin que Spring puisse reconnaître notre schéma et notre annotation personnalisés.
Les fonctions que nous voulons implémenter ici sont comme suit: Tout d'abord, laissez Spring reconnaître la configuration suivante.
<std: annotation-endpoint />
La fonction à implémenter dans cette configuration est qu'après la configuration, Spring peut numériser notre annotation @endpoint personnalisée. Et publier automatiquement les services de service Web en fonction des annotations. La fonction n'est pas entièrement implémentée et, en tant que tutoriel pour étendre le printemps, il joue un rôle dans l'attraction de Jade.
Créer un projet
Tout d'abord, vous devez créer un projet Java, et ici vous utilisez Maven pour créer un projet QuickStart (projet Java normal).
Le contenu du fichier POM est le suivant
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <ModelVersion> 4.0.0 </ ModelVersion> <ProupID> com.codestd </prôdId> <ArtifActid> Spring-CXF-Annotation-Support </ ArfactId> <Dersion> 1.0.0-Snapshot </DERNIERS> <name> $ {project.artifactid} </name> <escription> Permet à votre projet de publier le service Web via Annotations, basé sur l'encapsulation Spring + CXF, sans intrusion API. </ Description> <url> https://github.com/codestd/spring-cxf-annotation-support </url> <licence> <licence> <name> La licence Apache, version 2.0 </name> <url> http://www.apache.org/licenses/license-2.0.txt </url> </ licence> </cliance> <fouleverers> <maild> <name> jaune (wangchengwei) </ name> <mail> [email protected] </emter /role></Roles><TimeZone> gmt+8</timezone></developer></develovers><scm><connection> https://github.com/codestd/spring-cxf-annotation-support.git </cockection><developperconnection> htuptupt ESTD / Spring-CXF-Annotation-Support.git </ DeveloperConnection> </SCM> <Properties> <Junit.version> 4.12 </junit.version> <printemps.version> 4.2.4.release </spring.version> <cxf.version> 3.1.3 </cxf.version> </prope> Prote RTIES> <Dependces> <Dependance> <ProupId> JUnit </proupId> <ArtifActid> JUnit </ ArtiFactId> <Dersion> $ {Junit.Version} </DERNIERD> <COPE> test </cope> </Dependance> <Dependency> <ProupId> org.springFramework </jol UPID> <Artifactid> Spring-Context </ Artifactid> <Sease> $ {Spring.Version} </DERNIFRIENT> </DENDENCENCE> <Dependance> <ProupID> org.apache.cxf </proupId> <ArtifActid> CXF-RT-FRONTEND-JAXWS </RIFACTID> <version> $ {CX F.Version} </ version> </ Dependency> <Dedency> <ProupId> org.apache.cxf </proupId> <ArtefactId> CXF-RT-Transports-Http-Jetty </lefactive> <Dendency> $ {cxf.version} </-version> </Dedency> <Dedency> <proupId> org.springframework </proupId> <ArtifactId> printemps-test </ artifactId> <Dersion> $ {printemps.version} </ version> </dependency> <dependency> <proupId> LOG4J </proupId> <aRtifactid> LOG4J ersion> <ccope> test </cope> </ dépendendency> <dependency> <proupId> org.slf4j </proupId> <ArtifActid> SLF4J-LOG4J12 </ Artifactid> <version> 1.7.7 </ version> <ccope> Test </ccope> </Dedency> </randing>Schéma de définition
<? 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" TargetNamespac e = "http://www.codestd.com/schema/std/ws" elementFormDefault = "qualifié" AttributeFormDefault = "Unqualified"> <xsd: importation namespace = "http://www.springframework.org/schema/beans" /> <xsd: annotation> <xsd: documentation> <! [CDATA [Prise en charge de l'espace de noms pour l'annotation fournie par CXF Framework. ]]> </ xsd: documentation> </ xsd: annotation> <xsd: élément name = "annotation-endpoint"> <xsd: complexType> <xsd: complexContent> <xsd: extension base = "beans: identifiedType"> <xsd: attribute name = "name" type = "xsd: string" use = "Facultatif"> <xsd: annotation> <xsd: documentation> <! [cdata [nom de bean. INDED OF ID]]> </ XSD: Documentation> </ XSD: Annotation> </ XSD: attribute> <xsd: attribut name = "package" type = "xsd: string" use = "optional"> <xsd: annotation> <xsd: documentation> <! [Cdata [Pakeage pour scanner. ]]> </ xsd: documentation> </ xsd: annotation> </ xsd: attribut> </ xsd: extension> </ xsd: complexContent> </xsd: complexType> </xsd: élément> </ xsd: schema>
Les connaissances sur SECHMA ne seront pas décrites ici. Des amis qui ne savent pas comment l'utiliser doivent d'abord en apprendre davantage. L'emplacement SECHMA est dans SRC / Main / Resources / Meta-Inf / Schema / STDWS-1.0.xsd.
Annotation de définition
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.retentionPlocy; Importer java.lang.annotation. Le but de l'exposition au service en ajoutant {@code @endpoint} annotation à la classe. * <p> Étendez la fonction de balayage des bean de Spring. Après avoir ajouté cette annotation au haricot, il sera automatiquement enregistré dans le conteneur à ressort. * @Author Jaune (Wangchengwei) * @Since 1.0.0 * / @ Target (elementType.Type) @retention (RetenderPolicy.runtime) @DocumentedPublic @Interface Endpoint {/ *** L'identification de ce string dans le conteneur Spring * @ return * / String id (); / *** l'adresse du service est publié, la adresse du serveur, le port de projet *; adresse();}Configuration au printemps
Ouvrez "Window", "Préférences", "XML", "Catalogue XML". Cliquez sur "Ajouter", puis sélectionnez le XSD que nous avons créé ci-dessus à l'emplacement. "Type de clé" Sélectionnez le nom de l'espace de noms et entrez la touche en http://www.codestd.com/schema/std/ws/stdws-1.0.xsd. Autrement dit, le nom de fichier TargetNamespace + défini dans SECHMA.
Ajoutez des espaces de noms à Spring et utilisez des balises comme suit. Ici, nous devons utiliser la fonction d'annotation de Spring.
<? xml version = "1.0" Encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-in Stance "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.cod estd.com/schema/std/wshttp://www.codestd.com/schema/std/ws/stdws-1.0.xsd"><std:annotation-endpoint package = "com.codestd.spring.cxf.ws" /> </ beans>
Le package à analyser est défini dans la configuration et ne dépend pas de la configuration avec le contexte.
Ce qui précède est le schéma de configuration personnalisé à ressort extensible (1) partagé par l'éditeur. J'espère que ce sera utile à tout le monde.