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

来源:互联网 发布:汽车管家软件 编辑:程序博客网 时间:2024/06/05 16:25
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;@Configurationpublic 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)@Documentedpublic @interface HessianExporter {/** * hessian服务名称 * @return */String beanName() default "" ;/** * 服务端serviceBeanName * @return */String serviceBeanName() default "" ;/** * 暴露的接口 * @return */Class<?> interfaceClass();}