大家应该知道在Spring中有一个注解@Value,他可以帮助我们来讲Spring加载的配置文件(*.perperties)文件中的信息自动的注入到我们的非静态属性中的。
一般情况下我们会这样使用:
1. 首先在Spring的配置文件中加载属性文件:
<context:property-placeholder location="classpath:component.properties" ignore-unresolvable="true"/>
然后在Java代码中使用@Value注解就可以注入值了,比如:
@Value("${open_office_install_home}") private String openOfficeInstallHome;
当然属性如果是static的话是不能注入的。
其实这个自动注入的过程实现起来比较简单,我们下面通过一个例子来大致描述一下这个原理吧,这个例子是我写的,并不代表Spring的源码就是这么实现的。但是原理是一样的。
1. 我们先自定义一个注解:
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. 然后新增一个处理类:
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. 创建一个公共方法:
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)); } }
这样我们就完成了这个功能了。
下面我们通过测试代码来验证一下我们的功能是否起作用:
我们创建一个接口:
public interface UserService { @Value("user.name") public String getUserName(); @Value("user.password") public String getPassword(); }
然后编写测试类:
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属性文件中的内容为:
user.name=rollenholt user.password=123
运行上面的main方法,就会输出属性文件中的内容了。
不知道大家有没有注意到,我们在测试代码中使用的UserService是一个接口,我们并没有创建他的实现类,但是我们在main函数中依旧可以钓鱼他的方法。那是因为在运行时自动生成了一个实现。是不是觉的这个功能可以用在很多的地方呀。