com.alibaba.boot.dubbo.AnnotationBeanConfiguration Maven / Gradle / Ivy
package com.alibaba.boot.dubbo;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Protocol;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
/**
* Created by wuyu on 2017/4/19.
*/
public class AnnotationBeanConfiguration extends AnnotationBean implements ApplicationListener {
private ApplicationContext applicationContext;
private String annotationPackage;
/**
* 去除Dubbo扫描com.alibaba.dubbo.config.annotation.Service,仅把Service做为描述接口注解使用
*
* @param beanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
super.setApplicationContext(applicationContext);
this.applicationContext = applicationContext;
}
@Override
public void setPackage(String annotationPackage) {
super.setPackage(annotationPackage);
this.annotationPackage = annotationPackage;
}
/**
* 修复 @Service 注解bug
*
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (!isMatchPackage(bean)) {
return bean;
}
Class> targetClass = AopUtils.getTargetClass(bean);
Service service = targetClass.getAnnotation(Service.class);
//全局超时时间
String timeout = applicationContext.getEnvironment().resolvePlaceholders("${spring.dubbo.timeout}");
if (service != null) {
ServiceBean