spring容器启动就获得实现指定接口的beanMap
来源:互联网 发布:安卓模拟器优化 编辑:程序博客网 时间:2024/04/29 10:23
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import cn.baozun.crm.service.api.common.IMobile;
import cn.baozun.crm.util.ProxyToTarget;
// IMObile 换成指定接口就行了 在application。xml配置 listenner bean即可
public class ContextBeanListener implements ApplicationListener<ContextRefreshedEvent>{@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// 根容器为Spring容器
if(event.getApplicationContext().getParent()==null){
Map<String,Object> beans = event.getApplicationContext().getBeansWithAnnotation(org.springframework.stereotype.Component.class );
HashMap<String,Object> map= new HashMap<String,Object>();
for (Entry<String,Object> entry : beans.entrySet()) {
String key = entry.getKey();
Object obj = entry.getValue();
try {
Object target = ProxyToTarget.getTarget(obj);
if(target instanceof IMobile){
map.put(key, target);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//将spring 代理对象转换成 真是对象
package cn.baozun.crm.util;
import java.lang.reflect.Field;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
public class ProxyToTarget {
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);
AdvisedSupport advisedSupport = (AdvisedSupport) advised.get(dynamicAdvisedInterceptor);
Object target = advisedSupport.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;
}
}
- spring容器启动就获得实现指定接口的beanMap
- 获得spring容器中,实现某接口的所有实例;
- 获得spring 容器的实例
- Spring实现Aware接口,完成对IOC容器的感知
- Spring WebApplicationContext当web容器已经启动时获得当前线程的bean
- Spring 容器的启动方式
- spring容器 的启动过程
- Spring容器启动的第二阶段
- 获得指定接口的所有实现类(实现类需位于接口所在的包或其子包中)
- beanMap
- Spring获得初始化容器
- 获得spring容器上下文
- 【Spring源码--IOC容器的实现】(一)Web容器的启动
- Spring 实现容器销毁该bean的时候获得一次回调
- Spring进阶之路(3)-bean获得Spring的容器
- Spring进阶之路(3)-bean获得Spring的容器
- 实现了Runnable接口的实例,就一定会启动一个新的线程么?
- Spring IOC容器的实现
- An internal error occurred during:"Updating indexes".Java heap space
- cf 749 A. Bachgold Problem
- 前端js中的引号问题
- 20161226 VBA密码破解
- 初学java总结
- spring容器启动就获得实现指定接口的beanMap
- linux或sunOs下防火墙配置
- 2A-L5 gradient direction quiz code
- 详细解析BluetoothAdapter的详细api
- Centos 6下通过rpm安装redis3.0
- Junit基础用法
- 可调用对象
- 排序
- TCP建立连接和断开连接流程