别装了,难道你们不想把properties直接注入到object中去(spring-plugin)?
[size=small]/**
*作者:张荣华(ahuaxuan)
*日期:2008-4-9
**/
1背景
Spring2.5支持使用annotation来配置我们的service,比如如下代码:
@Service("userService")public class UserServiceImpl extends BaseServiceSupport implements UserService {public void xxx() {}}<context:component-scan base-package="xxxxxxx"/>
<bean id="propertyConfigurer" name="code">@Servicepublic class ImageFileUpload implements Serializable { @Properties(name="pic.address" ) private String picAddress; @Properties(name="pic.url" ) private String picUrl; private String picServerUrl;}/** * @author ahuaxuan(aaron zhang) * @since 2008-4-7 * @version $Id: Properties.java 261 2008-04-07 07:03:41Z aaron $ */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME) public @interface Properties {//String bundle();String name();}/** * @author ahuaxuan(aaron zhang) * @since 2008-4-7 * @version $Id: AnnotationBeanPostProcessor.java 260 2008-04-07 07:03:35Z aaron $ */public class AnnotationBeanPostProcessor extends PropertyPlaceholderConfigurer implements BeanPostProcessor, InitializingBean {private static transient Log logger = LogFactory.getLog(AnnotationBeanPostProcessor.class);private java.util.Properties pros;@SuppressWarnings("unchecked")private Class[] enableClassList = {String.class};@SuppressWarnings("unchecked")public void setEnableClassList(Class[] enableClassList) {this.enableClassList = enableClassList;}public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {Field [] fields = bean.getClass().getDeclaredFields();for (Field field : fields) {if (logger.isDebugEnabled()) {StringBuilder sb = new StringBuilder();sb.append(" ========= ").append(field.getType()).append(" ============ ").append(field.getName()).append(" ============ ").append(field.isAnnotationPresent(Properties.class));logger.debug(sb.toString());}if (field.isAnnotationPresent(Properties.class)) {if (filterType(field.getType().toString())) {Properties p = field.getAnnotation(Properties.class);try {//StringBuilder sb = new StringBuilder();//sb.append("set").append(StringUtils.upperCase(field.getName().substring(0, 1)))//.append(field.getName().substring(1, field.getName().length()));////Method method = bean.getClass().getMethod(sb.toString(), String.class);//method.invoke(bean, pros.getProperty(p.name()));本来我是通过set方法来把properties文件中的值注入到对应的属性上去的,后来downpour提供了更好的方案,就是下面这两行代码,虽然这样做破坏了private的功能,同时破坏了封装,但是确实节省了很多代码,建议大家在业务代码中不要这样做,如果做框架代码可以考虑一下。ReflectionUtils.makeAccessible(field);field.set(bean, pros.getProperty(p.name()));} catch (Exception e) {logger.error(" --- ", e);} }}}return bean;}@SuppressWarnings("unchecked")private boolean filterType(String type) {if (type != null) {for (Class c : enableClassList) {if (c.toString().equals(type)) {return true;}}return false;} else {return true;}}public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {return bean;}public void afterPropertiesSet() throws Exception {pros = mergeProperties();}}<bean id="propertyConfigurer"name="code">@Componentpublic class Config implements Serializable{/** */private static final long serialVersionUID = 8737228049639915113L;@Properties(name = " online.pay.accounts")private String accounts;@Properties(name = " online.pay.user")private String user;@Properties(name = " online.pay.password")private String password;@Properties(name = " online.transurl")private String transUrl;@Properties(name = " online.refundurl")private String refundUrl;@Properties(name = " online.query")private String queryUrl;```setter and getter method}@Service(“userService”)public class UserServiceImpl implements UserService {@autowiredprivate Config config;public void setConfig(Config config) {This.config = config;}}