Dukungan namespace
Untuk mengimplementasikan dukungan namespace, itu perlu diwarisi dari NamespaceHandlersupport.
Paket com.codestd.spring.cxf.config.schema; impor org.springframework.beans.factory.xml.namespaceDerSupport; import com.codestd.spring.cxf.config.endpointbeanprocessor;/*** menangani namespace* @aute.aute.aUnkePoCessor;/*** menangani namespace* @aute @aUnbor; class WebServiceAnnotationNamespaceHandler extends NamespaceHandlerSupport {@Overridepublic void init() {// TODO Auto-generated method stubthis.registerBeanDefinitionParser("annotation-endpoint", new AnnotationBeanDefinitionParser(EndpointBeanProcessor.class));}}Gunakan metode RegisterBeandFinitionParser untuk menjelaskan dukungan konfigurasi yang ditambahkan ke Spring. Annotation-endpoint adalah elemen yang didukung oleh konfigurasi. AnnotationBeandefinitionParser adalah kelas yang menangani konfigurasi. EndpointBeanProcessor adalah kelas yang memproses @endpoint yang dijelaskan oleh Bean, dan akan dijelaskan secara rinci nanti.
Konfigurasi Pemrosesan
Perlu menerapkan beandefinitionparser
Paket com.codestd.spring.cxf.config.schema; impor org.springframework.bean.factory.config.beandefinition; impor org.springframework.bean.factory.support.rootbeandefinition; import org.spramework. org.springframework.beans.factory.xml.parsercontext; impor org.springframework.util.stringutils; import org.w3c.dom.element;/*** @author JauneFiondeFiondiondiondiondiondiondionseDions BEANPEINGIE*@since 1.0.0.0*/Public Class AnnotationBeingBeidBeingBeiingBeFiEndiEndiEing)*@since 1.0.0.0; Class <?> Beansclass; AnnotationBeAndefinitionParser (class <?> Beandass) {this.beanClass = beanClass;}@overridepublic beandefinition parse (elemen elemen, parsercontext parsercontex Rootbeandefinition (); beandefinition.setBeAnclass (beanClass); beandefinition.setLazyInit (false); string id = element.getattribute ("id"); if (id == null || id.length () == 0) {string name = element.getattribute ("name"); beansclass.getName ();} if (parsercontext.getregistry (). containsbeandefinition (id)) {lempar baru ilegalstateException ("duplikat ID kacang musim semi" + id);} Parsercontext.getregistry (). RegisterBeAdefinition (id, beanDefinition) .getregistry (). RegisterBeanDefinition (ID, beanDefinition) (). element.getAttribute ("package"); if (! stringutils.isempty (annotationpackage)) beandefinition.getPropertyValues (). add ("annotationpackage", annotationpackage); kembalikan lefinisi beandfinition;}}}Untuk penerapan BeandefinitionParser, silakan merujuk ke dokumentasi musim semi resmi.
Kelas alat pendaftaran kacang
Paket com.codestd.spring.cxf.config; impor org.springframework.beans.beansException; impor org.springframework.beans.factory.config.beandefinition; impor org.spramewework.beans.factory.support.beandeFindionDering; org.springframework.beans.factory.support.beandefinitionregistry; impor org.springframework.context.applicationContext; Impor org.springframework.context.applicationContextAware; org.springframpewework.context.configramp. Harus menyuntikkan Spring ApplicationContext.* @Author Jaune (Wang Chengwei)* @since 1.0.0*/Kelas Publik BeanRegistry mengimplementasikan ApplicationContextAware {private applicationContextContext @overrideplicclicationContextContextcontcontContextContext; {this.applicationContext = applicationContext; if (applicationContext instanceof configurableApplicationContext) {this.configurableApplicationContext = (configurableApplicationContext) this.applicationContext;}} beanregistry publik () {} Public Beanregistry (PublicContex ApplicationContext) {this.setApplicationContext (ApplicationContext);} Public Beandefinition Register (class <?> clazz) {if (configurableApplicationContext == null) return; beandefinitionregistry beandefinitonregistry = (Beandefinitionregistry) configurableApplicationContext.getBeanFactory (); beandefinitionBuilder beandefinitionBuilder = this.createBuilder (clazz); beandefinition beandefinition = beandefinitionBuilder.getrawbeandefinition (); beandefinitonregistry.registerbeandefinition (clazz.getname (), beandefinition); return beandefinition;} beandefinitionBuilder createBuilder (class <?> CLAZZ) {CLIARD) {CLIZZ) {CLIARD) {CLASST <?> CLAZZBUZZE {class <?> CLAZZBUZZEDERDERDERDER (CLASS <?> CLAZZBUZZE (CLASS? BeandefinitionBuilder.genericbeandefinition (clazz); return beandefinitionBuilder;}}Memproses @endpoint
Paket com.codestd.spring.cxf.config; impor org.springframework.beans.beansException; impor org.springframework.beans.factory.disposablean; impor org.springframework. org.springframework.beans.factory.config.configurableListableBeanFactory; impor org.springframework.beans.factory.support.plicationcontext; org.springframework.context.applicationcontext; impor org.springframework.context.applicationcontext; impor org org.springframework.context.annotation.classpathbeandefinitionScanner; impor org.springframework.core.type.filter.annotationTypeFilter; impor org.springframework.util.stringutils; impor com.codestd.spring.cxpox.util.string.stringuTiLs; impor com.codestd.spring.cannor.util.string.string.string; Jaune (Wangchengwei)* @since 1.0.0*/Kelas Publik EndpointbeanProcessor mengimplementasikan BeanFactoryPostProcessor, DisposableBean, BeanPostProcessor, ApplicationContextAware {private string comma_split = ","; beanregistry; public void setannotationPackage (string annotationpackage) {this.annoTationPackage = annotationpackage; if (! stringutils.isempty (this.annotationpackage)) this.annotationpackage = this.annotationpackage.split (this.comma_spliter = this.annotationpackage.split (this.commma_split setappLicationContext (ApplicationContext ApplicationContext) melempar BeansException {this.applicationContext = ApplicationContext; this.beanregistry = beanregistry baru (this.applicationContext);}@overridepublic objek postprocessafterinitialization (bean objek, string beanname) melempar beansexception {if (! this.ismatchpackage (bean) mengembalikan bean; endpointpoint = bean.get -get (). null) {System.out.println (bean.getClass ());} return bean;} @overridepublic objek postprocessBeforeInitialization (Object Bean, String Beanname) melempar BeansException @ @ @ @ @overidepublic void hancur () lempar pengecualian @ @ @ @ @ @overidepublic @ @ @ @ @ @ @ @ @ @ @ @ IsMatchPackage (Object Bean) {if (annotationPackages == null || annotationpackages.length == 0) {return true;} string beanClassName = bean.getClass (). getName (); for (string pkg: annotationpackages) {if (beanclClClassname. false;}/*** scan {@link com.codestd.spring.cxf.annotation.endpoint} annotasi*/@overridepublic void postprocessBeanFactory (configurableListableBeanFactory = nulluppackpackypackpackpackpack (non -lipkaprage (annotationpack = | non -non | non -liptexception {if (ife (ifcrage = non -nonFactory {if (nnoTationPack) {return;}if (beanFactory instance of BeanDefinitionRegistry) {BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry)beanFactory;ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry,true);AnnotationTypeFilter filter = new AnnotationTypeFilter (Endpoint.class); Scanner.Addincludefilter (Filter); Scanner.Scan (AnnotationPackages);}}} Pemindaian anotasi telah diimplementasikan di sini. Maka Anda perlu menulis kode pemrosesan bisnis dalam metode postprocessafterinitialisasi. Afterinitialisasi berarti bahwa kacang telah dibuat dan disuntikkan dengan atribut.
PostprocessBeforeInitialisasi terutama digunakan untuk menyuntikkan sifat ketika instantiasi kacang.
Biarkan Spring mengenali ekstensi
Pertama-tama buat spring.handlers di bawah meta-inf di classpath, kontennya adalah sebagai berikut
http/: //www.codestd.com/schema/std/ws=com.codestd.spring.cxf.config.schema.webserviceannotationnotationnotespaceAndler
Dalam file ini, tentukan namespace mana yang membutuhkan kelas mana yang akan ditangani.
Kemudian buat Spring.schemas
http/: //www.codestd.com/schema/std/ws/stdws-1.0.xsd=meta-inf/schema/stdws-1.0.xsd=meta-inf/schema/stdws-1.0.xsd
Tentukan lokasi file SecHMA, Spring akan menggunakan file XSD yang diformulasikan di sini untuk memverifikasi bahwa konfigurasinya benar.
tes
Buat antarmuka
Paket com.codestd.spring.cxf.ws; import javax.jws.webservice;/*** @author jaune (wang chengwei)*@since 1.0.0*/@webservicepublic HelloService {public string syhi (string name);}Kelas Implementasi
package com.codestd.spring.cxf.ws;import javax.jws.WebService;import com.codestd.spring.cxf.annotation.Endpoint;/*** @author jaune(Wang Chengwei)* @since 1.0.0*/@Endpoint(address="HelloService", id = "HelloServiceEndPoint")@WebService (endpointInterface = "com.codestd.spring.cxf.ws.helloService") kelas publik HelloServiceImpl mengimplementasikan HelloService {@Overridepublic string syhi (string name) {return "hello"+name;}}Kasus uji
@Runwith (springjunit4classrunner.class) @contextConfiguration (lokasi = {"classpath: applicationContext.xml"}) Inisialisasi kelas publik {@testpublic void test () {}} Ada sepotong kode di kelas pemrosesan yang mencetak semua kelas dengan anotasi @endpoint, jadi jika nama kelas dicetak, itu berarti konfigurasinya normal.
Jalankan kasus uji
Konsol bisa melihat
kelas com.codestd.spring.cxf.ws.helloServiceImpl
Melalui pengenalan konten di atas, ekstensi ini pada dasarnya direalisasikan.