程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring自界說設置裝備擺設Schema可擴大(二)

Spring自界說設置裝備擺設Schema可擴大(二)

編輯:關於JAVA

Spring自界說設置裝備擺設Schema可擴大(二)。本站提示廣大學習愛好者:(Spring自界說設置裝備擺設Schema可擴大(二))文章只能為提供參考,不一定能成為您想要的結果。以下是Spring自界說設置裝備擺設Schema可擴大(二)正文


定名空間支撐

要完成定名空間支撐,須要繼續自NamespaceHandlerSupport。

package com.codestd.spring.cxf.config.schema;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import com.codestd.spring.cxf.config.EndpointBeanProcessor;
/**
* 處置定名空間
* @author jaune(Wang Chengwei)
* @since 1.0.0
*/
public class WebServiceAnnotationNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
// TODO Auto-generated method stub
this.registerBeanDefinitionParser("annotation-endpoint", new AnnotationBeanDefinitionParser(EndpointBeanProcessor.class));
}
}

經由過程registerBeanDefinitionParser辦法講設置裝備擺設支撐添加到Spring中。annotation-endpoint是設置裝備擺設支撐的元素。AnnotationBeanDefinitionParser是處置設置裝備擺設的類。EndpointBeanProcessor是處置@Endpoint注解的Bean的類,前面會有具體的講述。

處置設置裝備擺設

須要完成BeanDefinitionParser

package com.codestd.spring.cxf.config.schema;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author jaune(Wang Chengwei)
* @since 1.0.0
*/
public class AnnotationBeanDefinitionParser implements BeanDefinitionParser {
private final Class<?> beanClass;
public AnnotationBeanDefinitionParser(Class<?> beanClass) {
this.beanClass = beanClass;
}
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
beanDefinition.setLazyInit(false);
String id = element.getAttribute("id");
if(id == null || id.length() == 0 ){
String name = element.getAttribute("name");
if(!StringUtils.isEmpty(name)) id = name;
else id = beanClass.getName();
}
if (parserContext.getRegistry().containsBeanDefinition(id)) {
throw new IllegalStateException("Duplicate spring bean id " + id);
}
parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
String annotationPackage = element.getAttribute("package");
if(!StringUtils.isEmpty(annotationPackage))
beanDefinition.getPropertyValues().add("annotationPackage", annotationPackage);
return beanDefinition;
}
}

BeanDefinitionParser的運用拜見Spring官方文檔。

Bean注冊對象類

package com.codestd.spring.cxf.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Registry Bean. Must inject the spring ApplicationContext.
* @author jaune(Wang Chengwei)
* @since 1.0.0
*/
public class BeanRegistry implements ApplicationContextAware{
private ApplicationContext applicationContext;
private ConfigurableApplicationContext configurableApplicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
if(applicationContext instanceof ConfigurableApplicationContext){
this.configurableApplicationContext = (ConfigurableApplicationContext)this.applicationContext;
}
}
public BeanRegistry(){
}
public BeanRegistry(ApplicationContext applicationContext){
this.setApplicationContext(applicationContext);
}
public BeanDefinition register(Class<?> clazz){
if(configurableApplicationContext == null)return null;
BeanDefinitionRegistry beanDefinitonRegistry = 
(BeanDefinitionRegistry)configurableApplicationContext.getBeanFactory();
BeanDefinitionBuilder beanDefinitionBuilder = this.createBuilder(clazz);
BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
beanDefinitonRegistry.registerBeanDefinition(clazz.getName(),beanDefinition);
return beanDefinition;
}
private BeanDefinitionBuilder createBuilder(Class<?> clazz){
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
return beanDefinitionBuilder;
}
}

處置@Endpoint

package com.codestd.spring.cxf.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.StringUtils;
import com.codestd.spring.cxf.annotation.Endpoint;
/**
* @author jaune(WangChengwei)
* @since 1.0.0
*/
public class EndpointBeanProcessor implements 
BeanFactoryPostProcessor, DisposableBean, BeanPostProcessor, ApplicationContextAware{
private final String COMMA_SPLIT_PATTERN = ",";
private ApplicationContext applicationContext;
private String annotationPackage;
private String[] annotationPackages;
private BeanRegistry beanRegistry;
public void setAnnotationPackage(String annotationPackage) {
this.annotationPackage = annotationPackage;
if(!StringUtils.isEmpty(this.annotationPackage))
this.annotationPackages = this.annotationPackage.split(this.COMMA_SPLIT_PATTERN);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
this.beanRegistry = new BeanRegistry(this.applicationContext);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if(!this.isMatchPackage(bean))return bean;
Endpoint endpoint = bean.getClass().getAnnotation(Endpoint.class);
if(endpoint != null){
System.out.println(bean.getClass());
}
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public void destroy() throws Exception {
}
/**
* 包能否婚配
* @param bean
* @return
*/
private boolean isMatchPackage(Object bean){
if (annotationPackages == null || annotationPackages.length == 0) {
return true;
}
String beanClassName = bean.getClass().getName();
for (String pkg : annotationPackages) {
if (beanClassName.startsWith(pkg)) {
return true;
}
}
return false;
}
/**
* 掃描{@link com.codestd.spring.cxf.annotation.Endpoint}注解
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (annotationPackage == null || annotationPackage.length() == 0) {
return;
}
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry)beanFactory;
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry,true);
AnnotationTypeFilter filter = new AnnotationTypeFilter(Endpoint.class);
scanner.addIncludeFilter(filter);
scanner.scan(annotationPackages);
}
}
}

這裡曾經完成了注解的掃描。然後須要在postProcessAfterInitialization辦法中寫營業處置代碼。AfterInitialization表現Bean曾經創立而且注入屬性。

postProcessBeforeInitialization重要是為了在Bean實例化時注入屬性。

讓Spring辨認擴大

起首在classpath的META-INF下創立spring.handlers,內容以下

http\://www.codestd.com/schema/std/ws=com.codestd.spring.cxf.config.schema.WebServiceAnnotationNamespaceHandler

在這個文件中指清楚明了哪一個定名空間須要哪一個類來處置。
然後再創立spring.schemas

http\://www.codestd.com/schema/std/ws/stdws-1.0.xsd=META-INF/schema/stdws-1.0.xsd

指清楚明了Sechma文件的地位,Spring會應用這裡制訂的xsd文件來驗證設置裝備擺設能否准確。

測試

創立接口

package com.codestd.spring.cxf.ws;
import javax.jws.WebService;
/**
* @author jaune(Wang Chengwei)
* @since 1.0.0
*/
@WebService
public interface HelloService {
public String syHi(String name);
}

完成類

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")
public class HelloServiceImpl implements HelloService{
@Override
public String syHi(String name) {
return "Hello "+name;
}
}

測試用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class InitializationTest {
@Test
public void test(){
}
}

在處置類中有一段代碼是將有@Endpoint注解的類都打印出來,所以假如類名被打印出來就表現設置裝備擺設正常了。

運轉測試用例

掌握台可以或許看到

class com.codestd.spring.cxf.ws.HelloServiceImpl

經由過程以上內容的引見本次擴大根本上就完成了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved