Skip to content

Commit

Permalink
1. fix cannot get bean bug. 2. fix aspectj not load bug
Browse files Browse the repository at this point in the history
  • Loading branch information
knightliao committed Dec 1, 2015
1 parent 277b5d1 commit 323caaf
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 13 deletions.
2 changes: 1 addition & 1 deletion disconf-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-base</artifactId>
<version>2.6.29</version>
<version>2.6.30-SNAPSHOT</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;

import com.baidu.disconf.client.store.aspect.DisconfAspectJ;
import com.baidu.disconf.client.store.inner.DisconfCenterHostFilesStore;
import com.baidu.disconf.client.utils.StringUtil;

Expand Down Expand Up @@ -49,7 +51,6 @@ public int getOrder() {
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

}

/**
Expand All @@ -72,13 +73,33 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) t
// 进行扫描
DisconfMgr.getInstance().setApplicationContext(applicationContext);
DisconfMgr.getInstance().firstScan(scanPackList);

// register java bean
registerAspect(registry);
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

/**
* register aspectJ for disconf get request
*
* @param registry
*/
private void registerAspect(BeanDefinitionRegistry registry) {

GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(DisconfAspectJ.class);
beanDefinition.setLazyInit(false);
beanDefinition.setAbstract(false);
beanDefinition.setAutowireCandidate(true);
beanDefinition.setScope("singleton");

registry.registerBeanDefinition("disconfAspectJ", beanDefinition);
}

/*
* 已经废弃了,不推荐使用
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void inject2OneConf(String fileName, DisconfCenterFile disconfCenterFile

object = disconfCenterFile.getObject();
if (object == null) {
object = registry.getFirstByType(disconfCenterFile.getCls());
object = registry.getFirstByType(disconfCenterFile.getCls(), true);
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void inject2OneConf(String key, DisconfCenterItem disconfCenterItem) {
//
if (!Modifier.isStatic(field.getModifiers())) {

object = registry.getFirstByType(field.getDeclaringClass());
object = registry.getFirstByType(field.getDeclaringClass(), true);
}

disconfStoreProcessor.inject2Instance(object, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public interface Registry {
* @return 可找到的Bean的实例列表
*/
<T> T getFirstByType(Class<T> type);

/**
* 查找Bean, 是否找proxy
*
* @param type 类型
*
* @return 可找到的Bean的实例列表
*/
<T> T getFirstByType(Class<T> type, boolean withProxy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public <T> T getFirstByType(Class<T> type) {
return list.get(0);
}
}

@Override
public <T> T getFirstByType(Class<T> type, boolean withProxy) {
return getFirstByType(type, withProxy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

Expand Down Expand Up @@ -44,10 +45,11 @@ public <T> List<T> findByType(Class<T> type) {

Map<String, T> map = findByTypeWithName(type);
if (map == null || map.isEmpty()) {
LOGGER.warn("Not found from Spring IoC container for " + type.getSimpleName() + ", and try to init by "
LOGGER.debug("Not found from Spring IoC container for " + type.getSimpleName() + ", and try to init by "
+ "calling newInstance.");
return simpleRegistry.findByType(type);
}

return new ArrayList<T>(map.values());
}

Expand All @@ -60,6 +62,23 @@ public <T> T getFirstByType(Class<T> type) {
return list.get(0);
}

@Override
public <T> T getFirstByType(Class<T> type, boolean withProxy) {

T object = getFirstByType(type);

if (!withProxy) {
return object;
}

try {
return getTargetObject(object, type);
} catch (Exception e) {
LOGGER.warn(e.toString());
return object;
}
}

/**
* 调用Spring工具类获取bean
*
Expand All @@ -68,6 +87,16 @@ public <T> T getFirstByType(Class<T> type) {
* @return 容器托管的bean字典
*/
public <T> Map<String, T> findByTypeWithName(Class<T> type) {
return BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, type);
return applicationContext.getBeansOfType(type);
}

protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
if (AopUtils.isJdkDynamicProxy(proxy)) {
return (T) ((Advised) proxy).getTargetSource().getTarget();
} else if (AopUtils.isCglibProxy(proxy)) {
return (T) ((Advised) proxy).getTargetSource().getTarget();
} else {
return (T) proxy;
}
}
}
2 changes: 1 addition & 1 deletion disconf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-base</artifactId>
<version>2.6.29</version>
<version>2.6.30-SNAPSHOT</version>
</parent>

<licenses>
Expand Down
2 changes: 1 addition & 1 deletion disconf-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-base</artifactId>
<version>2.6.29</version>
<version>2.6.30-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-base</artifactId>
<version>2.6.29</version>
<version>2.6.30-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
Expand All @@ -25,9 +25,9 @@

<properties>
<!-- 模块版本号 -->
<disconf-core.version>2.6.29</disconf-core.version>
<disconf-client.version>2.6.29</disconf-client.version>
<disconf-client-spring.version>2.6.29</disconf-client-spring.version>
<disconf-core.version>2.6.30-SNAPSHOT</disconf-core.version>
<disconf-client.version>2.6.30-SNAPSHOT</disconf-client.version>
<disconf-client-spring.version>2.6.30-SNAPSHOT</disconf-client-spring.version>

<!-- Spring項目配置 -->
<encoding>UTF-8</encoding>
Expand Down

0 comments on commit 323caaf

Please sign in to comment.