Skip to content

Commit

Permalink
添加 AppContext::newInstance 接口
Browse files Browse the repository at this point in the history
  • Loading branch information
noear committed Sep 23, 2023
1 parent d7fdb77 commit f24b8c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions UPDATE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
### 2.5.7

* 调整 solon.config.add 与 solon.config.load 合并,规范格式(同时支持内部与外部) ???
* 添加 `AppContext::newInstance` 接口
* 使用 `solon.config.import` 替代 `solon.config.add``solon.config.load`,后者标为弃用
* 使用 `@Import` 替代 `@PropertySource``@TestPropertySource`,后者标为弃用
* 使用 `@Rollback` 替代 `@TestRollback`,后者标为弃用
Expand Down
21 changes: 20 additions & 1 deletion solon/src/main/java/org/noear/solon/core/AppContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.noear.solon.core.event.EventBus;
import org.noear.solon.core.event.EventListener;

import java.lang.reflect.Constructor;

/**
* 应用上下文( 为全局对象;热插拨的插件,会产生独立的上下文)
*
Expand All @@ -15,7 +17,7 @@
* @author noear
* @since 2.5
* */
public class AppContext extends AopContext{ //(继承,为兼容性过度)
public class AppContext extends AopContext { //(继承,为兼容性过度)
public AppContext(ClassLoader classLoader, Props props) {
super(classLoader, props);
}
Expand All @@ -37,4 +39,21 @@ public <T> AppContext onEvent(Class<T> type, int index, EventListener<T> handler
return this;
}

/**
* 构造实例
* */
public <T> T newInstance(Class<?> clz) throws Exception {
Constructor c = clz.getDeclaredConstructors()[0];
if (c.getParameterCount() > 0) {
Object[] args = new Object[c.getParameterCount()];

for (int i = 0; i < args.length; i++) {
args[i] = getBean(c.getParameterTypes()[i]);
}

return (T) c.newInstance(args);
} else {
return (T) c.newInstance();
}
}
}
10 changes: 0 additions & 10 deletions solon/src/main/java/org/noear/solon/core/BeanContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ public ClassLoader getClassLoader() {
* for Aot
*/


/**
* clz mapping
*/
private final Map<Class<?>, Class<?>> clzMapping = new HashMap<>();


//启动时写入
/**
* bean 构建器
Expand Down Expand Up @@ -131,7 +124,6 @@ public void clear() {
beanWrapsOfName.clear();
beanWrapSet.clear();

clzMapping.clear();
attrs.clear();
aot.clear();

Expand Down Expand Up @@ -596,8 +588,6 @@ protected void beanRegisterSup0(BeanWrap bw) {
Class<?>[] list = bw.clz().getInterfaces();
for (Class<?> c : list) {
if (c.getName().contains("java.") == false) {
//建立关系映射
clzMapping.putIfAbsent(c, bw.clz());
putWrap(c, bw);
}
}
Expand Down

0 comments on commit f24b8c2

Please sign in to comment.