您的位置:首页 > 编程语言 > Java开发

java自定义注解

2015-05-20 14:34 141 查看
package com.**.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BizServiceProxy {
    public String address() default "";
}

package com.**.common;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.**.action.BaseAction;
import com.**.base.biz.IBiz;
import com.**.util.ConfigUtils;

/**
 * BizServiceProxy注解的自动注入类。
 * 
 * 1、在beans.xml文件中进行以下配置:
 * <bean class="com.**.BizServiceAutowiring" />
 * <context:component-scan base-package="com.**.*.action" />
 * 
 * 2、在action包下的类中,如果某个变量对应通过调用WebService创建的biz实例,
 * 则针对该变量加上@BizServiceProxy注解。
 * 
 * @author sanfy

 * @version 1.0
 * CreatedTime    2013-12-20
 */
public class BizServiceAutowiring implements BeanPostProcessor {
    private Logger log = LoggerFactory.getLogger(BizServiceAutowiring.class);
    private static String prefix = ConfigUtils.getConfig("kbs.ws.url.prefix");
    
    /**
     * 存放通过WebService创建的实例,其中key对应WebService的address
     */
    private static Map<String, IBiz> bizMap = new HashMap<String, IBiz>();
    
    @SuppressWarnings("unchecked")
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        if (bean instanceof BaseAction){
            Field[] fields = bean.getClass().getDeclaredFields();
            for(Field field : fields){
                if(field != null && field.isAnnotationPresent(BizServiceProxy.class)){
                    BizServiceProxy anno = field.getAnnotation(BizServiceProxy.class);
                    String address = anno.address();// 注解指定的WS服务名
                    if(StringUtils.isBlank(address)){// 注解未指定时采用变量名做WS服务名
                        address = field.getName();
                    } 
                    try {
                        IBiz biz;
                        if(StringUtils.isNotBlank(prefix)){
                            if(bizMap.containsKey(address)){
                                biz = bizMap.get(address);
                            } else {// map中不存在该实例,通过WebService创建一个,然后存放到map中
                                Class fieldType = field.getType();
                                JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
                                factoryBean.setServiceClass(fieldType);
                                factoryBean.setAddress(prefix + address);
                                biz = (IBiz)factoryBean.create();
                                
                                bizMap.put(address, biz);
                            }
                        } else {// WebService前缀没有值,即部署在同一台机器,可以直接获取bean实例
                            biz = (IBiz)BeansContext.getDynamicBean(address);
                        }
                        // 对变量进行初始化,在此之前需要设置变量可访问
                        field.setAccessible(true);
                        field.set(bean, biz);
                    } catch (IllegalArgumentException e) {
                        log.info("ws url = " + address);
                        log.error("IllegalArgumentException happened: " + e);
                        throw new BeanInitializationException("The arg is not type of String", e);
                    } catch (IllegalAccessException e) {
                        log.info("ws url = " + address);
                        log.error("IllegalAccessException happened: " + e);
                        throw new BeanInitializationException("The field is not accessiable", e);
                    } catch (ClassCastException e){
                        log.info("ws url = " + address);
                        log.error("ClassCastException happened: " + e);
                        throw new BeanInitializationException("The bean is not type of IBiz", e);
                    }
                }
            }
        }
        
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        return bean;
    }
    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: