利用Java注解特性加载属性文件(properties)的值到Java类

在Spring中JavaBean的初始化顺序就是为现在写这个做准备的,要实现现在说的这个功能,大体方案有如下:

  1.定义一个注解类

  2.在需要加载属性的JavaBean的属性set方法上写注解,注解的参数就是key

  3.在Spring启动的时候,去读取属性文件,然后把值赋给JavaBean

  我们在上一篇写在Spring中JavaBean的初始化顺序提到了,如果一个JavaBean实现了BeanPostProcessor接口,那么其他Bean初始化以后都会交给这个Bean来处理.这样我们就可以写一个JavaBean实现BeanPostProcessor接口,这个Bean有个属性,它指向属性文件路径,在这个Bean初始化的时候读取属性文件内容,然后在postProcessBeforeInitialization方法里面对写了注解的Bean进行赋值.这样一个实现思路似乎很顺其自然,都是自己觉得不是很好,Spring通过PropertyPlaceholderConfigurer这样类来加载属性文件,而我们有写了另外一个类,这样一是不够统一,二是不够可能会造成多个属性文件.解决办法就是扩展PropertyPlaceholderConfigurer类.写一个类继承PropertyPlaceholderConfigurer类.然后在PropertyPlaceholderConfigurer初始化完成以后,获取加载的属性文件内容,在postProcessBeforeInitialization里面把写过注解的类属性进行赋值.那么怎么确定什么时候PropertyPlaceholderConfigurer加载完成呢,根据Spring中JavaBean的初始化顺序,我们知道一个JavaBean如果实现了InitializingBean接口,那么Spring容器会在这个Bean初始化以后调用afterPropertiesSet方法,现在我写个类继承PropertyPlaceholderConfigurer类,实现BeanPostProcessor, InitializingBean 这个两个接口那么刚刚提到的问题就可以解决了,下面是实现代码

package com.test.annotation;
import java.lang.reflect.Method;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.ReflectionUtils;
public class AnnotationBeanPostProcessor extends PropertyPlaceholderConfigurer implements BeanPostProcessor, InitializingBean {
   private java.util.Properties pros;  
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName)
      throws BeansException {
    // TODO Auto-generated method stub
    return bean;
  }
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {
    if(bean.getClass().getAnnotation(Property.class)!=null){
      Method[] methods = bean.getClass().getDeclaredMethods(); 
      for (Method method : methods) {  
        Property p = method.getAnnotation(Property.class);
        if(p!=null){
//         这里进行参数类型转换
          Object para=pros.getProperty(p.name());
          if((method.getParameterTypes()[0]).getName().equals("java.lang.Integer")){
            para= new Integer(para.toString());
          }
          ReflectionUtils.invokeMethod(method, bean, new Object[]{para});
        }
      }  
    }
    return bean;
  }
  @Override
  public void afterPropertiesSet() throws Exception {
    pros = mergeProperties();  
  }
}
package com.test.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Property {
  String name() default "";
}
package com.test;
import com.test.annotation.Property;
@Property
public class Bean {
  private String name;
  private Integer age;
private String address;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getAddress() {
    return address;
  }
  @Property(name="com.test.Bean.address")
  public void setAddress(String address) {
    this.address = address;
  }
  public Integer getAge() {
    return age;
  }
  @Property(name="com.test.Bean.age")
  public void setAge(Integer age) {
    this.age = age;
  }
}
package com.test;
import com.test.annotation.Property;
@Property
public class JavaBean {
  private String name;
  private String address;
  public String getName() {
    return name;
  }
  @Property(name="com.test.JavaBean.name")
  public void setName(String name) {
    this.name = name;
  }
  public String getAddress() {
    return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
  /**
   * @param args
   */
  public static void main(String[] args) {
     ApplicationContext context = 
        new ClassPathXmlApplicationContext("spring.xml"); 
     System.out.println("加载配置文件结束");
     System.out.println("--------------------------------------------");
     JavaBean javaBean=(JavaBean)context.getBean("javaBean");
     System.out.println(javaBean.getName());
     System.out.println(javaBean.getAddress());
     System.out.println("--------------------------------------------");
     Bean bean=(Bean)context.getBean("bean");
     System.out.println(bean.getName());
     System.out.println(bean.getAddress());
     System.out.println(bean.getAge());
     System.out.println("--------------------------------------------");
  }
}
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id=
attribute-value>"propertyConfigurer" class="com.test.annotation.AnnotationBeanPostProcessor">
    <property name="locations">
      <list>
        <value>classpath*:system.properties</value>
      </list>
    </property>
  </bean>
  <bean id="javaBean" class="com.test.JavaBean">
    <property name="address" value="${com.test.JavaBean.address}"></property>
  </bean>
  <bean id="bean" class="com.test.Bean">
    <property name="name" value="${com.test.Bean.name}"></property>
  </bean>
</beans>

 

  ps:之所以要继承PropertyPlaceholderConfigurer类,还有一个原因就是,原来通过${}注入值的方式还可以用

  BeanPostProcessor有两个方法,为什么要写在postProcessBeforeInitialization里面,而不是postProcessAfterInitialization里面,原因在于postProcessBeforeInitialization方法是在Bean的init方法之前执行,在init方法里面可能会用到类的属性,所以必须在init方法执行之前先赋值好.

 

<!-- 分页 -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值