在此之前我都是写个PropertyUtil类来加载配置文件,然后通过get方法,把key对应的值取出来.
Spring提供一个PropertyPlaceholderConfigurer类,可以读取配置文件,然后在Spring配置文件通过${hibernate.dialect}这种方式注入到JavaBean中,有个不好的地方就是,要在代码中取的时候不是很方便.
然后在接触到Java注解特注解技术以后,感觉这个东东很好,hibernate映射,WebService都可以通过注解来完成,方便的很多,然后就在想能不能通过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 这个两个接口那么刚刚提到的问题就可以解决了,下面是实现代码
- packagecom.test.annotation;
- importjava.lang.reflect.Method;
- importorg.springframework.beans.BeansException;
- importorg.springframework.beans.factory.InitializingBean;
- importorg.springframework.beans.factory.config.BeanPostProcessor;
- importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
- importorg.springframework.util.ReflectionUtils;
- publicclassAnnotationBeanPostProcessorextendsPropertyPlaceholderConfigurerimplementsBeanPostProcessor,InitializingBean{
- privatejava.util.Propertiespros;
- @Override
- publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)
- throwsBeansException{
- //TODOAuto-generatedmethodstub
- returnbean;
- }
- @Override
- publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)
- throwsBeansException{
- if(bean.getClass().getAnnotation(Property.class)!=null){
- Method[]methods=bean.getClass().getDeclaredMethods();
- for(Methodmethod:methods){
- Propertyp=method.getAnnotation(Property.class);
- if(p!=null){
- //这里进行参数类型转换
- Objectpara=pros.getProperty(p.name());
- if((method.getParameterTypes()[0]).getName().equals("java.lang.Integer")){
- para=newInteger(para.toString());
- }
- ReflectionUtils.invokeMethod(method,bean,newObject[]{para});
- }
- }
- }
- returnbean;
- }
- @Override
- publicvoidafterPropertiesSet()throwsException{
- pros=mergeProperties();
- }
- }
- packagecom.test.annotation;
- importjava.lang.annotation.Retention;
- importjava.lang.annotation.RetentionPolicy;
- importjava.lang.annotation.Target;
- importjava.lang.annotation.ElementType;
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.TYPE,ElementType.METHOD})
- public@interfaceProperty{
- Stringname()default"";
- }
- packagecom.test;
- importcom.test.annotation.Property;
- @Property
- publicclassBean{
- privateStringname;
- privateIntegerage;
- privateStringaddress;
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetAddress(){
- returnaddress;
- }
- @Property(name="com.test.Bean.address")
- publicvoidsetAddress(Stringaddress){
- this.address=address;
- }
- publicIntegergetAge(){
- returnage;
- }
- @Property(name="com.test.Bean.age")
- publicvoidsetAge(Integerage){
- this.age=age;
- }
- }
- packagecom.test;
- importcom.test.annotation.Property;
- @Property
- publicclassJavaBean{
- privateStringname;
- privateStringaddress;
- publicStringgetName(){
- returnname;
- }
- @Property(name="com.test.JavaBean.name")
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetAddress(){
- returnaddress;
- }
- publicvoidsetAddress(Stringaddress){
- this.address=address;
- }
- }
- packagecom.test;
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
- publicclassTest{
- /**
- *@paramargs
- */
- publicstaticvoidmain(String[]args){
- ApplicationContextcontext=
- newClassPathXmlApplicationContext("spring.xml");
- System.out.println("加载配置文件结束");
- System.out.println("--------------------------------------------");
- JavaBeanjavaBean=(JavaBean)context.getBean("javaBean");
- System.out.println(javaBean.getName());
- System.out.println(javaBean.getAddress());
- System.out.println("--------------------------------------------");
- Beanbean=(Bean)context.getBean("bean");
- System.out.println(bean.getName());
- System.out.println(bean.getAddress());
- System.out.println(bean.getAge());
- System.out.println("--------------------------------------------");
- }
- }
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <beanid="propertyConfigurer"class="com.test.annotation.AnnotationBeanPostProcessor">
- <propertyname="locations">
- <list>
- <value>classpath*:system.properties</value>
- </list>
- </property>
- </bean>
- <beanid="javaBean"class="com.test.JavaBean">
- <propertyname="address"value="${com.test.JavaBean.address}"></property>
- </bean>
- <beanid="bean"class="com.test.Bean">
- <propertyname="name"value="${com.test.Bean.name}"></property>
- </bean>
- </beans>
ps:之所以要继承PropertyPlaceholderConfigurer类,还有一个原因就是,原来通过${}注入值的方式还可以用
BeanPostProcessor有两个方法,为什么要写在postProcessBeforeInitialization里面,而不是postProcessAfterInitialization里面,原因在于postProcessBeforeInitialization方法是在Bean的init方法之前执行,在init方法里面可能会用到类的属性,所以必须在init方法执行之前先赋值好.