大家應該知道在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函數中依舊可以釣魚他的方法。那是因為在運行時自動生成了一個實現。是不是覺的這個功能可以用在很多的地方呀。