spring自定义bean(包含引用bean)--自定义注解解析

import java.lang.annotation.Annotation;
import java.util.Set;

import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.remoting.caucho.HessianServiceExporter;

import com.mmhlive.eshop.annotation.HessianExporter;
@Configuration
public class CustomBeanDefinitionRegistryPostProcessor   implements ApplicationContextAware   , BeanDefinitionRegistryPostProcessor {
    protected Logger log = LoggerFactory.getLogger(getClass());
 
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        Reflections reflections = new Reflections("com.xxxx","com.ejavashop");

        Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(HessianExporter.class);
        for (Class<?> serviceClass : annotated) {
            log.info("命中++"+serviceClass.getName());
            for(Annotation annotation : serviceClass.getAnnotations()){
                log.info("命中%%++"+annotation.annotationType().getName());
                if (! (annotation instanceof HessianExporter)){
                    continue;
                }
                log.info("加载--"+annotation.annotationType().getName());
                
                RootBeanDefinition beanDefinition = new RootBeanDefinition();
                beanDefinition.setBeanClass(HessianServiceExporter.class);
                String serviceBeanName =  (String) AnnotationUtils.getAnnotationAttributes(annotation).get("serviceBeanName");
                String beanName =  (String) AnnotationUtils.getAnnotationAttributes(annotation).get("beanName");
                beanDefinition.setLazyInit(true);
                beanDefinition.getPropertyValues().addPropertyValue("serviceInterface", AnnotationUtils.getAnnotationAttributes(annotation).get("interfaceClass"));
                 
                PropertyValue pv=new PropertyValue("ref",null);
                pv.setAttribute("bean",serviceBeanName);
                beanDefinition.setLazyInit(true);
                beanDefinition.getPropertyValues().addPropertyValue("service", applicationContext.getBean(serviceBeanName));
                registry.registerBeanDefinition(beanName, beanDefinition);
            }
        }
    }
    ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         this.applicationContext=applicationContext;
        
    }

}

注解:

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;

/**
 * hessian服务暴露注解
 * 
 * @author zsj
 *
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HessianExporter {
	/**
	 * hessian服务名称
	 * @return
	 */
	String beanName() default "" ;
	/**
	 * 服务端serviceBeanName
	 * @return
	 */
	String serviceBeanName() default "" ;
	/**
	 * 暴露的接口
	 * @return
	 */
	Class<?> interfaceClass();
}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值