一、问题描述:
通过spring托管的service业务类,这个类是通过反射拿到的,经过实验发现这个类只能反射取得service实现了接口的方法,而extends类的方法一律不出现,debug后发现这个servie实例被spring替换成jdkDynmicProxy类,而不是原始对象了,它里面只有service的接口方法,而没有extends 过的super class方法。
结论:当使用策略模式动态切换service实现类时,最好使用原始目标对象。
原因:spring会给我们生成代理对象,有两种方式:JDK动态代理(实现目标类的接口,与目标类是兄弟关系),Cglib动态代理(继承目标类,与目标类是父子关系);当我们的目标类,既实现接口,并且又继承某个类时,就会导致上面的问题出现;
1、获取代理类的目标对象,解决上面遇到的问题:
import java.lang.reflect.Field;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
/**
* Description: 能获取JDK动态代理/CGLIB代理对象代理的目标对象。
* All Rights Reserved.
* @version 1.0 2015-6-28 上午9:04:32 by [email protected]创建
*/
public class AopTargetUtils {
/**
* 获取 目标对象
* @param proxy 代理对象
* @return
* @throws Exception
*/
public static Object getTarget(Object proxy) throws Exception {
if(!AopUtils.isAopProxy(proxy)) {
return proxy;//不是代理对象
}
if(AopUtils.isJdkDynamicProxy(proxy)) {
return getJdkDynamicProxyTargetObject(proxy);
} else { //cglib
return getCglibProxyTargetObject(proxy);
}
}
private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
h.setAccessible(true);
Object dynamicAdvisedInterceptor = h.get(proxy);
Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
advised.setAccessible(true);
Object target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
return target;
}
private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
h.setAccessible(true);
AopProxy aopProxy = (AopProxy) h.get(proxy);
Field advised = aopProxy.getClass().getDeclaredField("advised");
advised.setAccessible(true);
Object target = ((AdvisedSupport)advised.get(aopProxy)).getTargetSource().getTarget();
return target;
}
}
二、策略模式动态切换业务实现
/**
*
* @Date: 2020/3/2 13:49
* @Author: zhenliang.song
* @Description:
*/
public interface UserService {
/**
* 保存用户
*/
public void saveUser();
}
@Service("userService1")
class UserService1 implements UserService{
@Override
public void saveUser() {
System.out.println("UserService1:业务实现类");
}
}
@Service("userService2")
class UserService2 implements UserService{
@Override
public void saveUser() {
System.out.println("UserService2:业务实现类");
}
}
/**
* 适配策略枚举
*/
enum StrategyCrossEnums{
STRATEGY_ONE("1", "userService1"),
STRATEGY_TWO("2", "userService2"),
;
private String code;
private String className;
StrategyCrossEnums(String code, String className) {
this.code = code;
this.className = className;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
/**
* 管理策略业务:spring启动时加载afterProperTiesSet方法
*/
@Component
class BeanLoaderFactory implements InitializingBean {
private Map<String, Object> contextMap = new HashMap<>();
@Override
public void afterPropertiesSet() throws Exception {
//获取spring创建的bean
ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
for(StrategyCrossEnums crossEnums : StrategyCrossEnums.values()){
//获取原始目标对对象
Object target = AopUtils.getTarget(applicationContext.getBean(crossEnums.getClassName()));
contextMap.put(crossEnums.getCode(), target);
}
}
/**
* 根据code获取业务原始目标对象
* @param code
* @return
*/
public Object getStrategyMap(String code){
Object target = contextMap.get(code);
return target;
}
}
@Controller
@RequestMapping("/strategyController")
class StrategyController{
@Autowired
private BeanLoaderFactory beanLoaderFactory;
/**
* 调用方法
* @param code
*/
@RequestMapping("/method")
public void method(String code){
//实现多个业务策略动态切换
UserService userService = (UserService) beanLoaderFactory.getStrategyMap(code);
userService.saveUser();
}
}