大家应该知道在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; |
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=rollenholtuser.password=123 |
运行上面的main方法,就会输出属性文件中的内容了。
不知道大家有没有注意到,我们在测试代码中使用的UserService是一个接口,我们并没有创建他的实现类,但是我们在main函数中依旧可以钓鱼他的方法。那是因为在运行时自动生成了一个实现。是不是觉的这个功能可以用在很多的地方呀。
原文地址:http://www.cnblogs.com/rollenholt/p/3699952.html