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;
}
}
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;
}
}
相关文章推荐
- Java自定义注解
- 深入理解Java:注解(Annotation)自定义注解入门
- 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句
- java自定义注解
- 深入理解Java:注解(Annotation)自定义注解入门
- Java千百问_03基础语法(021)_如何自定义注解
- java自定义注解
- (转载)深入理解Java:注解(Annotation)自定义注解入门
- 160621、Java注解教程及自定义注解
- 注解:java 自定义注解应用实例
- java自定义注解学习
- Java自定义注解
- 深入理解Java:注解(Annotation)自定义注解入门
- java自定义注解
- 深入理解Java:注解(Annotation)自定义注解入门
- 自定义注解、Java启动spring
- java_自定义注解_1
- JavaWeb【自定义注解】
- Java自定义注解Annotation的使用
- 分析Spring 源码前需要了解的java自定义注解