使用注解属性绑定

大家应该知道在Spring中有一个注解@Value,他可以帮助我们来讲Spring加载的配置文件(*.perperties)文件中的信息自动的注入到我们的非静态属性中的。

一般情况下我们会这样使用:

1.  首先在Spring的配置文件中加载属性文件:

1
<context:property-placeholder location= "classpath:component.properties"   ignore-unresolvable= "true" />

然后在Java代码中使用@Value注解就可以注入值了,比如:

 
1
2
@Value ( "${open_office_install_home}" )
private  String openOfficeInstallHome;
 
当然属性如果是static的话是不能注入的。
 
其实这个自动注入的过程实现起来比较简单,我们下面通过一个例子来大致描述一下这个原理吧,这个例子是我写的,并不代表Spring的源码就是这么实现的。但是原理是一样的。
 
1.  我们先自定义一个注解:
1
2
3
4
5
6
7
8
9
10
11
12
import  java.lang.annotation.ElementType;
import  java.lang.annotation.Retention;
import  java.lang.annotation.RetentionPolicy;
import  java.lang.annotation.Target;
 
@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
public  @interface  Value {
 
     public  String value();
 
}

2. 然后新增一个处理类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import  java.lang.reflect.InvocationHandler;
import  java.lang.reflect.Method;
import  java.util.Properties;
 
 
public  class  PropertyInvokationHandler  implements  InvocationHandler {
 
     private  Properties properties;
 
     public  PropertyInvokationHandler(Properties properties) {
         this .properties = properties;
     }
 
     @Override
     public  Object invoke(Object proxy, Method method, Object[] args)  throws  Throwable {
         Value annotation = method.getAnnotation(Value. class );
         if (annotation ==  null ){
             throw  new  RuntimeException(String.format( "Method:{} is not bound to a property." , method.getName()));
         }
         return  properties.getProperty(annotation.value());
     }
}

3.  创建一个公共方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import  java.lang.reflect.Proxy;
import  java.util.Properties;
 
public  class  PropertyTool {
 
     private  PropertyTool() {
     }
 
     public  static  <T> T bindProperties(Class<T> clazz, Properties properties) {
         return  (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                 new  Class[]{clazz},
                 new  PropertyInvokationHandler(properties));
     }
}

这样我们就完成了这个功能了。

 

下面我们通过测试代码来验证一下我们的功能是否起作用:

我们创建一个接口:

1
2
3
4
5
6
7
8
9
public  interface  UserService {
 
     @Value ( "user.name" )
     public  String getUserName();
 
     @Value ( "user.password" )
     public  String getPassword();
 
}

然后编写测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import  java.io.FileInputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.util.Properties;
 
public  class  UserServiceTester {
 
     public  static  void  main(String[] args) {
         Properties properties =  new  Properties();
 
         try  {
             String path = UserServiceTester. class .getResource( "/user.properties" ).getPath();
             InputStream in =  new  FileInputStream(path);
             properties.load(in);
             in.close();
         catch (IOException ex) {
             ex.printStackTrace();
         }
 
         UserService config = PropertyTool.bindProperties(UserService. class , properties);
         System.out.println( "User Name: "  + config.getUserName());
         System.out.println( "Password: "  + config.getPassword());
     }
}

而我们的user.properties属性文件中的内容为:

1
2
user.name=rollenholt
user.password= 123

运行上面的main方法,就会输出属性文件中的内容了。

 

不知道大家有没有注意到,我们在测试代码中使用的UserService是一个接口,我们并没有创建他的实现类,但是我们在main函数中依旧可以钓鱼他的方法。那是因为在运行时自动生成了一个实现。是不是觉的这个功能可以用在很多的地方呀。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值