上一篇是在main方法下测试的,这一篇我给各位博友服务到底,封装成了一个工具类,直接调用即可!不多说上代码:
public class DataSourceSwitch {
@DataSource(value = "slave1")
public static void change(String data)throws Exception{
Method method = DataSourceSwitch.class.getMethod("change", String.class);
DataSource dataSource = method.getAnnotation(DataSource.class);
if (dataSource ==null){
throw new RuntimeException("请添加数据源");
}
InvocationHandler invocationHandler = Proxy.getInvocationHandler(dataSource);
Field value = invocationHandler.getClass().getDeclaredField("memberValues");
value.setAccessible(true);
Map<String,Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
String val = (String) memberValues.get("value");
System.out.println("改变前"+val);
val = data;
m