diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Lazy.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Lazy.java index 0cb6ea9e..a48b6ed4 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Lazy.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Lazy.java @@ -33,7 +33,7 @@ import java.lang.annotation.Target; * @date: 2022/6/16 */ @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) +@Target({ElementType.TYPE,ElementType.METHOD}) public @interface Lazy { boolean value() default true; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java index db4f8fdd..86b4b787 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/AbstractBeanFactory.java @@ -21,14 +21,12 @@ */ package fun.asgc.neutrino.core.bean; +import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.context.LifeCycle; import fun.asgc.neutrino.core.context.LifeCycleManager; import fun.asgc.neutrino.core.context.LifeCycleStatus; import fun.asgc.neutrino.core.exception.BeanException; -import fun.asgc.neutrino.core.util.Assert; -import fun.asgc.neutrino.core.util.CollectionUtil; -import fun.asgc.neutrino.core.util.LockUtil; -import fun.asgc.neutrino.core.util.TypeUtil; +import fun.asgc.neutrino.core.util.*; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; @@ -53,10 +51,6 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, * bean缓存 */ protected Map beanCache; - /** - * 工厂bean缓存 - */ - protected Map factoryBeanCache; /** * 互斥锁 */ @@ -78,7 +72,6 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, Assert.notNull(name, "工厂名称不能为空"); this.parent = parent; this.beanCache = new ConcurrentHashMap<>(256); - this.factoryBeanCache = new ConcurrentHashMap<>(256); this.name = name; if (null == parent) { this.mutex = new Object(); @@ -146,6 +139,11 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, return res || doHasBean(type, name); } + @Override + public boolean hasBean(Class type) { + return hasBean(type, ClassUtil.getBeanName(type)); + } + @Override public int countBean(String name) { int count = (null != parent) ? parent.countBean(name) : 0; @@ -190,7 +188,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, @Override public void registerBean(Class type) throws BeanException { - registerBean(type, TypeUtil.getDefaultVariableName(type)); + registerBean(type, ClassUtil.getBeanName(type)); } @Override @@ -206,20 +204,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, private T doGetBean(String name, Object... args) throws BeanException { List beanList = findBeanList(name); if (CollectionUtil.isEmpty(beanList)) { - // TODO check - List factoryBeanWrapperList = findFactoryBeanListByName(name); - if (CollectionUtil.isEmpty(factoryBeanWrapperList)) { - return null; - } else if (factoryBeanWrapperList.size() > 1) { - throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); - } - BeanWrapper factoryBeanWrapper = factoryBeanWrapperList.get(0); - dependencyCheck(factoryBeanWrapperList); - newInstance(factoryBeanWrapperList); - inject(factoryBeanWrapperList); - factoryBeanWrapper.init(); - - return (T)((FactoryBean)factoryBeanWrapper.getInstance()).getInstance(); + return null; } else if (beanList.size() > 1) { throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); } @@ -229,20 +214,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, private T doGetBeanByName(Class type, String name, Object... args) throws BeanException { List beanList = findBeanList(type, name); if (CollectionUtil.isEmpty(beanList)) { - // TODO check - List factoryBeanWrapperList = findFactoryBeanListByName(type, name); - if (CollectionUtil.isEmpty(factoryBeanWrapperList)) { - return null; - } else if (factoryBeanWrapperList.size() > 1) { - throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); - } - BeanWrapper factoryBeanWrapper = factoryBeanWrapperList.get(0); - dependencyCheck(factoryBeanWrapperList); - newInstance(factoryBeanWrapperList); - inject(factoryBeanWrapperList); - factoryBeanWrapper.init(); - - return (T)((FactoryBean)factoryBeanWrapper.getInstance()).getInstance(); + return null; } else if (beanList.size() > 1) { throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); } @@ -252,20 +224,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, private T doGetBean(Class type, Object... args) throws BeanException { List beanList = findBeanList(type); if (CollectionUtil.isEmpty(beanList)) { - // TODO check - List factoryBeanWrapperList = findFactoryBeanListByType(type); - if (CollectionUtil.isEmpty(factoryBeanWrapperList)) { - return null; - } else if (factoryBeanWrapperList.size() > 1) { - throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); - } - BeanWrapper factoryBeanWrapper = factoryBeanWrapperList.get(0); - dependencyCheck(factoryBeanWrapperList); - newInstance(factoryBeanWrapperList); - inject(factoryBeanWrapperList); - factoryBeanWrapper.init(); - - return (T)((FactoryBean)factoryBeanWrapper.getInstance()).getInstance(); + return null; } else if (beanList.size() > 1) { throw new BeanException(String.format("Bean[type:%s] 存在多个实例!", type)); } @@ -275,10 +234,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, private T doGetBeanByTypeAndName(Class type, String name, Object... args) throws BeanException { BeanWrapper bean = findBean(type, name); if (null == bean) { - BeanWrapper factoryBeanWrapper = findFactoryBean(type, name); - if (null != factoryBeanWrapper) { - return (T)((FactoryBean)factoryBeanWrapper.getInstance()).getInstance(); - } + return null; } if (null == bean) { return null; @@ -378,68 +334,6 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, return beanCache.get(new BeanIdentity(name, type)); } - /** - * 查找工厂bean - * @param type - * @param name - * @return - */ - private BeanWrapper findFactoryBean(Class type, String name) { - Assert.notNull(type, "bean的类型不能为空!"); - Assert.notNull(name, "bean的名称不能为空!"); - return factoryBeanCache.get(new BeanIdentity(name, type)); - } - - /** - * 查找工厂bean列表 - * @param type - * @param name - * @return - */ - private List findFactoryBeanListByName(Class type, String name) { - Assert.notNull(type, "bean的类型不能为空!"); - Assert.notNull(name, "bean的名称不能为空!"); - List list = new ArrayList<>(); - for (BeanIdentity identity : factoryBeanCache.keySet()) { - if (type.isAssignableFrom(identity.getType()) && identity.getName().equals(name)) { - list.add(factoryBeanCache.get(identity)); - } - } - return list; - } - - /** - * 查找工厂bean列表 - * @param name - * @return - */ - private List findFactoryBeanListByName(String name) { - Assert.notNull(name, "bean的名称不能为空!"); - List list = new ArrayList<>(); - for (BeanIdentity identity : factoryBeanCache.keySet()) { - if (identity.getName().equals(name)) { - list.add(factoryBeanCache.get(identity)); - } - } - return list; - } - - /** - * 查找工厂bean列表 - * @param type - * @return - */ - private List findFactoryBeanListByType(Class type) { - Assert.notNull(type, "bean的类型不能为空!"); - List list = new ArrayList<>(); - for (BeanIdentity identity : factoryBeanCache.keySet()) { - if (type.isAssignableFrom(identity.getType())) { - list.add(factoryBeanCache.get(identity)); - } - } - return list; - } - /** * 新增一个bean * @param bean @@ -451,37 +345,15 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, newInstance(bean); // 工厂bean BeanIdentity identity = ((FactoryBean)bean.getInstance()).getBeanIdentity(); - if (factoryBeanCache.containsKey(identity)) { - throw new BeanException(String.format("Bean[type:%s name:%s] 存在多个factoryBean!", bean.getType().getName(), bean.getName())); - } if (beanCache.containsKey(identity)) { throw new BeanException(String.format("Bean[type:%s name:%s] 定义与BeanFactory[type:%s name:%s]冲突!", identity.getType().getName(), identity.getName(), bean.getType().getName(), bean.getName())); } - factoryBeanCache.put(identity, bean); + BeanWrapper targetBeanWrapper = addBean(identity.getType(), identity.getName()); + targetBeanWrapper.setFactoryBean(bean); + targetBeanWrapper.setInstantiationMode(BeanInstantiationMode.FACTORY); } } - /** - * 获取bean的工厂 - * @param bean - * @return - */ - protected FactoryBean getFactory(BeanWrapper bean) throws BeanException { - BeanWrapper factoryBeanWrapper = factoryBeanCache.get(bean.getIdentity()); - if (null == factoryBeanWrapper) { - return null; - } - dependencyCheck(factoryBeanWrapper); - newInstance(factoryBeanWrapper); - inject(factoryBeanWrapper); - factoryBeanWrapper.init(); - - if (BeanStatus.INIT == factoryBeanWrapper.getStatus() || BeanStatus.RUNNING == factoryBeanWrapper.getStatus()) { - return (FactoryBean)factoryBeanWrapper.getInstance(); - } - throw new BeanException(String.format("Bean[type:%s name%s] 获取工厂 Beanfactory[type:%s name:%s]异常!", bean.getType().getName(), bean.getName(), factoryBeanWrapper.getType().getName(), factoryBeanWrapper.getName())); - } - @Override public void init() { lifeCycleManager.init(() -> { @@ -562,7 +434,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, * @param type * @param name */ - protected abstract void addBean(Class type, String name); + protected abstract BeanWrapper addBean(Class type, String name); /** * 获取或创建实例 diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/ApplicationContainer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanFactoryAware.java similarity index 83% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/container/ApplicationContainer.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanFactoryAware.java index f9ade5c6..7388daa9 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/ApplicationContainer.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanFactoryAware.java @@ -19,14 +19,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - -package fun.asgc.neutrino.core.container; +package fun.asgc.neutrino.core.bean; /** - * 应用容器 + * 用于标识需要beanFactory的组件 * @author: aoshiguchen - * @date: 2022/6/16 + * @date: 2022/7/6 */ -public interface ApplicationContainer extends Container { +public interface BeanFactoryAware { + /** + * 设置bean工厂 + * @param beanFactory + */ + void setBeanFactory(BeanFactory beanFactory); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanRegistry.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanRegistry.java index d8c2cdc7..30728821 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanRegistry.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanRegistry.java @@ -68,6 +68,13 @@ public interface BeanRegistry { */ boolean hasBean(Class type, String name); + /** + * 判断是否包含此bean + * @param type + * @return + */ + boolean hasBean(Class type); + /** * 查询bean容器中有多少个叫该名称的bean * @param name diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java index 6b12df22..bd9dff4b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/BeanWrapper.java @@ -23,12 +23,7 @@ package fun.asgc.neutrino.core.bean; import fun.asgc.neutrino.core.annotation.*; -import fun.asgc.neutrino.core.aop.Aop; import fun.asgc.neutrino.core.context.LifeCycle; -import fun.asgc.neutrino.core.container.BeanContainer; -import fun.asgc.neutrino.core.context.ApplicationConfig; -import fun.asgc.neutrino.core.context.ApplicationContext; -import fun.asgc.neutrino.core.context.Environment; import fun.asgc.neutrino.core.util.*; import lombok.Data; import lombok.experimental.Accessors; @@ -57,6 +52,9 @@ public class BeanWrapper implements LifeCycle { private BeanStatus status; private boolean isLazy; private BeanIdentity beanIdentity; + private BeanWrapper factoryBean; + private BeanInstantiationMode instantiationMode; + private Method instantiationMethod; public boolean hasInstance() { return null != instance; @@ -104,113 +102,6 @@ public class BeanWrapper implements LifeCycle { } } - public boolean newInstance(ApplicationContext context) { - boolean result = newInstance0(); - if (result) { - inject(context); - init(); - } - return result; - } - - private boolean newInstance0() { - try { - return LockUtil.doubleCheckProcess(() -> !hasInstance(), - this, - () -> { - try { - // 暂时先只支持yml配置 - Configuration configuration = type.getAnnotation(Configuration.class); - if (null != configuration) { - instance = ConfigUtil.getYmlConfig(type); - } else if (ClassUtil.isInterface(type)) { - instance = Aop.get(type); - } else { - // 由编码规避没有无参构造器的问题 - instance = type.newInstance(); - } - } catch (Exception e) { - // ignore - } - }, - () -> hasInstance() - ); - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - - private void inject(ApplicationContext context) { - Set methods = ReflectUtil.getMethods(type); - if (CollectionUtil.notEmpty(methods)) { - methods.stream().forEach(method -> { - fun.asgc.neutrino.core.annotation.Bean bean = method.getAnnotation(fun.asgc.neutrino.core.annotation.Bean.class); - if (null == bean) { - return; - } - String name = bean.value(); - if (StringUtil.isEmpty(name)) { - name = method.getName(); - } - // TODO 方法带参数的情况、class重复的问题,暂时由编码规避 - if (method.getParameters().length == 0) { - try { - Object obj = method.invoke(this.instance); - if (null == obj) { - log.error("bean 实例不能为空!"); - return; - } - context.getBeanContainer().addBean(new BeanWrapper() - .setType(obj.getClass()) - .setBoot(false) - .setComponent(null) - .setInstance(obj) - .setName(name) - .setOrder(Integer.MAX_VALUE) - ); - } catch (Exception e) { - log.error(String.format("bean [%s] 实例化失败!", name), e); - } - } - }); - } - Set fieldSet = ReflectUtil.getInheritChainDeclaredFieldSet(type); - if (CollectionUtil.notEmpty(fieldSet)) { - fieldSet.forEach(field -> { - Autowired autowired = field.getAnnotation(Autowired.class); - if (null == autowired) { - return; - } - if (field.getType() == Environment.class) { - ReflectUtil.setFieldValue(field, instance, context.getEnvironment()); - } else if(field.getType() == ApplicationConfig.class) { - ReflectUtil.setFieldValue(field, instance, context.getApplicationConfig()); - } else if(field.getType() == BeanContainer.class) { - ReflectUtil.setFieldValue(field, instance, context.getBeanContainer()); - } else if (field.getType() == ApplicationContext.class) { - ReflectUtil.setFieldValue(field, instance, context); - } else { - Class autowiredType = field.getType(); - BeanWrapper autowiredBean = null; - if (StringUtil.isEmpty(autowired.value())) { - autowiredBean = context.getBeanContainer().getBean(autowiredType); - } else { - autowiredBean = context.getBeanContainer().getBean(autowired.value()); - } - if (null == autowiredBean) { - throw new RuntimeException(String.format("类 %s 自动装配字段:%s 依赖bean不存在!", type.getName(), field.getName())); - } - if (autowiredBean.isDependOn(type)) { - throw new RuntimeException(String.format("类[%s]与类[%s]存在循环依赖!", type.getName(), field.getType().getName())); - } - autowiredBean.newInstance(context); - ReflectUtil.setFieldValue(field, instance, autowiredBean.getInstance()); - } - }); - } - } - public boolean isLazy() { Lazy lazy = ClassUtil.getAnnotation(type, Lazy.class); return null != lazy; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java index 4cdfdf64..bcba7591 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java @@ -52,14 +52,15 @@ public class SimpleBeanFactory extends AbstractBeanFactory { } @Override - protected void addBean(Class type, String name) { + protected BeanWrapper addBean(Class type, String name) { log.debug("addBean[type:{} name:{}]", type.getName(), name); BeanWrapper beanWrapper = new BeanWrapper() .setType(type) .setName(name) .setLazy(Boolean.FALSE) .setOrder(Integer.MAX_VALUE) - .setStatus(BeanStatus.REGISTER); + .setStatus(BeanStatus.REGISTER) + .setInstantiationMode(BeanInstantiationMode.DIRECT); Lazy lazy = ClassUtil.getAnnotation(type, Lazy.class); if (null != lazy) { beanWrapper.setLazy(lazy.value()); @@ -69,14 +70,16 @@ public class SimpleBeanFactory extends AbstractBeanFactory { beanWrapper.setOrder(order.value()); } addBean(beanWrapper); - registerBeanForMethod(type); + registerBeanForMethod(beanWrapper, type); + return beanWrapper; } /** * 根据method注册bean + * @param factoryBean * @param type */ - private void registerBeanForMethod(Class type) { + private void registerBeanForMethod(BeanWrapper factoryBean, Class type) { Set methods = ReflectUtil.getMethods(type); if (CollectionUtil.isEmpty(methods)) { return; @@ -94,7 +97,19 @@ public class SimpleBeanFactory extends AbstractBeanFactory { if (method.getParameters().length > 0) { throw new BeanException(String.format("Bean[type:%s name:%s] 注册失败,Class:%s method:%s 方法不能带参数!", beanType.getName(), beanName, type.getName(), method.getName())); } - addBean(beanType, beanName); + BeanWrapper beanWrapper = addBean(beanType, beanName); + beanWrapper.setFactoryBean(factoryBean); + beanWrapper.setInstantiationMode(BeanInstantiationMode.METHOD); + beanWrapper.setInstantiationMethod(method); + + Lazy lazy = method.getAnnotation(Lazy.class); + if (null != lazy) { + beanWrapper.setLazy(lazy.value()); + } + Order order = method.getAnnotation(Order.class); + if (null != order) { + beanWrapper.setOrder(order.value()); + } } } @@ -124,22 +139,40 @@ public class SimpleBeanFactory extends AbstractBeanFactory { NonIntercept nonIntercept = ClassUtil.getAnnotation(bean.getType(), NonIntercept.class); boolean isNonIntercept = (null != nonIntercept && nonIntercept.value()) ? true : false; - if (ClassUtil.isInterface(bean.getType())) { - bean.setInstance(Aop.get(bean.getType())); - } else if(bean.getType().isAnnotationPresent(Configuration.class)) { - bean.setInstance(ConfigUtil.getYmlConfig(bean.getType())); - } else { - if (ClassUtil.hasNoArgsConstructor(bean.getType())) { - if (isNonIntercept) { - bean.setInstance(bean.getType().newInstance()); - } else { - bean.setInstance(Aop.get(bean.getType())); - } + if (BeanInstantiationMode.DIRECT == bean.getInstantiationMode()) { + // 直接实例化 + if (ClassUtil.isInterface(bean.getType())) { + bean.setInstance(Aop.get(bean.getType())); + } else if(bean.getType().isAnnotationPresent(Configuration.class)) { + bean.setInstance(ConfigUtil.getYmlConfig(bean.getType())); } else { - // TODO 暂不支持有参构造器 - throw new BeanException(String.format("Bean[type:%s name:%s] 没有无参构造器,实例化失败!", bean.getType().getName(), bean.getName())); + if (ClassUtil.hasNoArgsConstructor(bean.getType())) { + if (isNonIntercept) { + bean.setInstance(bean.getType().newInstance()); + } else { + bean.setInstance(Aop.get(bean.getType())); + } + } else { + // TODO 暂不支持有参构造器 + throw new BeanException(String.format("Bean[type:%s name:%s] 没有无参构造器,实例化失败!", bean.getType().getName(), bean.getName())); + } } + } else if (BeanInstantiationMode.METHOD == bean.getInstantiationMode()) { + // 通过bean方法实例化 + BeanWrapper factoryBean = bean.getFactoryBean(); + if (!factoryBean.hasInstance()) { + newInstance(factoryBean); + } + bean.setInstance(bean.getInstantiationMethod().invoke(factoryBean.getInstance())); + } else if (BeanInstantiationMode.FACTORY == bean.getInstantiationMode()) { + // 通过FactoryBean实例化 + BeanWrapper factoryBean = bean.getFactoryBean(); + if (!factoryBean.hasInstance()) { + newInstance(factoryBean); + } + bean.setInstance(((FactoryBean)factoryBean.getInstance()).getInstance()); } + if (null != bean.getInstance()) { bean.setStatus(BeanStatus.INSTANCE); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/BeanContainer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/BeanContainer.java deleted file mode 100644 index bafa1719..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/BeanContainer.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.container; - -import fun.asgc.neutrino.core.bean.BeanWrapper; - -import java.util.List; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -public interface BeanContainer extends Container { - - /** - * 是否存在bean实例 - * @param clazz - * @return - */ - boolean hasBean(Class clazz); - - /** - * 是否存在bean实例 - * @param name - * @return - */ - boolean hasBean(String name); - - /** - * 获取bean实例 - * @param clazz - * @return - */ - BeanWrapper getBean(Class clazz); - - /** - * 获取bean实例 - * @param name - * @return - */ - BeanWrapper getBean(String name); - - /** - * 获取bean集合 - * @return - */ - List beanList(); - - /** - * 添加bean - * @param bean - */ - void addBean(BeanWrapper bean); -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/ClassContainer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/ClassContainer.java deleted file mode 100644 index 478411c7..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/ClassContainer.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.container; - -import java.util.Set; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -public interface ClassContainer extends Container { - - /** - * 判断指定类是否在容器中 - * @param clazz - * @return - */ - boolean hasClass(Class clazz); - - /** - * 获取容器中所有的类 - * @return - */ - Set> getClasses(); - - /** - * 获取容器中所有的组件 - * @return - */ - Set> getComponentClasses(); - -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/Container.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/Container.java deleted file mode 100644 index 2b39e723..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/Container.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.container; - -import fun.asgc.neutrino.core.context.LifeCycle; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -public interface Container extends LifeCycle { - - -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultApplicationContainer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultApplicationContainer.java deleted file mode 100644 index 04e31fc7..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultApplicationContainer.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.container; - -import fun.asgc.neutrino.core.context.ApplicationContext; -import fun.asgc.neutrino.core.context.Environment; -import fun.asgc.neutrino.core.exception.ContainerInitException; -import fun.asgc.neutrino.core.runner.ApplicationRunner; -import fun.asgc.neutrino.core.util.BeanManager; -import lombok.extern.slf4j.Slf4j; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -@Slf4j -public class DefaultApplicationContainer implements ApplicationContainer { - private Environment environment; - private BeanContainer beanContainer; - private ApplicationContext context; - - public DefaultApplicationContainer(Environment environment) { - this.environment = environment; - this.init(); - } - - @Override - public void init() throws ContainerInitException { - this.beanContainer = new DefaultBeanContainer(environment); - this.context = new ApplicationContext() - .setEnvironment(environment) - .setBeanContainer(beanContainer) - .setApplicationConfig(environment.getConfig()); - BeanManager.setContext(this.context); - this.beanContainer.beanList().forEach(bean -> { - if (bean.isBoot() || !bean.isLazy()) { - bean.newInstance(context); - } - }); - log.info("应用容器初始化完成"); - - this.run(); - } - - private void run() { - this.beanContainer.beanList().forEach(bean -> { - if (bean.isBoot()) { - if (bean.hasInstance()) { - if (ApplicationRunner.class.isAssignableFrom(bean.getType())) { - ApplicationRunner runner = (ApplicationRunner)bean.getInstance(); - runner.run(environment.getMainArgs()); - } - } - } - }); - } - - @Override - public void destroy() { - this.beanContainer.destroy(); - this.beanContainer.beanList().forEach(bean -> bean.destroy()); - log.info("应用容器销毁"); - } -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultBeanContainer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultBeanContainer.java deleted file mode 100644 index 3865e9ee..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultBeanContainer.java +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.container; - -import com.google.common.collect.Lists; -import fun.asgc.neutrino.core.annotation.Component; -import fun.asgc.neutrino.core.annotation.Order; -import fun.asgc.neutrino.core.bean.BeanWrapper; -import fun.asgc.neutrino.core.cache.Cache; -import fun.asgc.neutrino.core.cache.MemoryCache; -import fun.asgc.neutrino.core.context.Environment; -import fun.asgc.neutrino.core.exception.ContainerInitException; -import fun.asgc.neutrino.core.runner.ApplicationRunner; -import fun.asgc.neutrino.core.util.ClassUtil; -import fun.asgc.neutrino.core.util.StringUtil; -import lombok.extern.slf4j.Slf4j; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -@Slf4j -public class DefaultBeanContainer implements BeanContainer { - private Environment environment; - private ClassContainer classContainer; - private Cache nameBeanCache; - private Cache, BeanWrapper> classBeanCache; - private List beans; - - public DefaultBeanContainer(Environment environment) { - this.environment = environment; - this.nameBeanCache = new MemoryCache<>(); - this.classBeanCache = new MemoryCache<>(); - this.beans = new ArrayList<>(); - this.init(); - } - - @Override - public boolean hasBean(Class clazz) { - return classBeanCache.containsKey(clazz); - } - - @Override - public boolean hasBean(String name) { - return nameBeanCache.containsKey(name); - } - - @Override - public BeanWrapper getBean(Class clazz) { - return classBeanCache.get(clazz); - } - - @Override - public BeanWrapper getBean(String name) { - return nameBeanCache.get(name); - } - - @Override - public void init() throws ContainerInitException { - this.classContainer = new DefaultClassContainer(environment); - this.classContainer.getComponentClasses().forEach(clazz -> { - String name = clazz.getName(); - Component component = ClassUtil.getAnnotation(clazz, Component.class); - if (null != component && StringUtil.notEmpty(component.value())) { - name = component.value(); - } - Order order = ClassUtil.getAnnotation(clazz, Order.class); - int orderValue = Integer.MAX_VALUE; - if (null != order) { - orderValue = order.value(); - } - - addBean(new BeanWrapper() - .setType(clazz) - .setName(name) - .setComponent(component) - .setOrder(orderValue) - ); - - }); - - if (!classBeanCache.isEmpty()) { - beans = Lists.newArrayList(classBeanCache.values()); - Collections.sort(beans, Comparator.comparingInt(BeanWrapper::getOrder)); - } - - log.info("bean容器初始化完成"); - } - - @Override - public void destroy() { - this.classContainer.destroy(); - log.info("bean容器销毁"); - } - - @Override - public void addBean(BeanWrapper bean) { - if (null == bean) { - return; - } - // TODO 暂时由编码时规避名字冲突 - if (ApplicationRunner.class.isAssignableFrom(bean.getType())) { - bean.setOrder(Integer.MIN_VALUE); - bean.setBoot(true); - } - classBeanCache.set(bean.getType(), bean); - nameBeanCache.set(bean.getName(), bean); - } - - @Override - public List beanList() { - return beans; - } -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultClassContainer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultClassContainer.java deleted file mode 100644 index ccb783b5..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/container/DefaultClassContainer.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.container; - -import fun.asgc.neutrino.core.annotation.Component; -import fun.asgc.neutrino.core.context.Environment; -import fun.asgc.neutrino.core.exception.ContainerInitException; -import fun.asgc.neutrino.core.util.ClassUtil; -import fun.asgc.neutrino.core.util.CollectionUtil; -import lombok.extern.slf4j.Slf4j; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -@Slf4j -public class DefaultClassContainer implements ClassContainer { - private Environment environment; - private Set> classes; - private Set> componentClasses; - - public DefaultClassContainer(Environment environment) { - this.environment = environment; - this.classes = Collections.synchronizedSet(new HashSet<>()); - this.componentClasses = Collections.synchronizedSet(new HashSet<>()); - this.init(); - } - - @Override - public void init() throws ContainerInitException { - try { - if (CollectionUtil.isEmpty(environment.getScanBasePackages())) { - return; - } - this.classes = ClassUtil.scan(environment.getScanBasePackages()); - this.componentClasses = this.classes.stream().filter(item -> ClassUtil.isAnnotateWith(item, Component.class)).collect(Collectors.toSet()); - log.info("class容器初始化完成"); - } catch (Exception e) { - log.error("class容器初始化异常!"); - throw new ContainerInitException(this); - } - } - - @Override - public boolean hasClass(Class clazz) { - return classes.contains(clazz); - } - - @Override - public Set> getClasses() { - return classes; - } - - @Override - public Set> getComponentClasses() { - return componentClasses; - } - - @Override - public void destroy() { - log.info("class容器销毁"); - } -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java index 2a226b09..de55e915 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java @@ -27,8 +27,8 @@ import fun.asgc.neutrino.core.aop.interceptor.ExceptionHandler; import fun.asgc.neutrino.core.aop.interceptor.Filter; import fun.asgc.neutrino.core.aop.interceptor.Interceptor; import fun.asgc.neutrino.core.aop.interceptor.ResultAdvice; +import fun.asgc.neutrino.core.bean.BeanFactoryAware; import fun.asgc.neutrino.core.bean.SimpleBeanFactory; -import fun.asgc.neutrino.core.container.BeanContainer; import fun.asgc.neutrino.core.runner.ApplicationRunner; import fun.asgc.neutrino.core.util.*; import lombok.Data; @@ -57,10 +57,6 @@ public class ApplicationContext implements LifeCycle { * 应用配置 */ private ApplicationConfig applicationConfig; - /** - * bean容器 - */ - private BeanContainer beanContainer; /** * 根Bean工厂 */ @@ -74,9 +70,6 @@ public class ApplicationContext implements LifeCycle { */ private LifeCycleManager lifeCycleManager = LifeCycleManager.create(); - public ApplicationContext() { - - } public ApplicationContext(Environment environment) { this.environment = environment; @@ -93,8 +86,6 @@ public class ApplicationContext implements LifeCycle { this.rootBeanFactory.registerBean(environment.getConfig(), "rootApplicationConfig"); register(); this.applicationBeanFactory.init(); - // TODO 后面优化掉这种不安全的BeanManager - BeanManager.setContext(this); } catch (Exception e) { log.error("应用上下文初始化异常!", e); System.exit(-1); @@ -137,6 +128,9 @@ public class ApplicationContext implements LifeCycle { } this.applicationBeanFactory.registerBean(clazz, beanName); }); + if (!applicationBeanFactory.hasBean(BeanManager.class)) { + applicationBeanFactory.registerBean(BeanManager.class); + } } /** @@ -144,12 +138,13 @@ public class ApplicationContext implements LifeCycle { */ public void run() { init(); + List beanFactoryAwareList = this.applicationBeanFactory.getBeanList(BeanFactoryAware.class); + if (CollectionUtil.notEmpty(beanFactoryAwareList)) { + beanFactoryAwareList.forEach(beanFactoryAware -> beanFactoryAware.setBeanFactory(applicationBeanFactory)); + } List applicationRunnerList = this.applicationBeanFactory.getBeanList(ApplicationRunner.class); if (CollectionUtil.notEmpty(applicationRunnerList)) { applicationRunnerList.forEach(applicationRunner -> applicationRunner.run(this.environment.getMainArgs())); -// for (ApplicationRunner runner : applicationRunnerList) { -// runner.run(this.environment.getMainArgs()); -// } } } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java index 280fb9d9..ba03488b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java @@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.db.mapper; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.aop.Invocation; import fun.asgc.neutrino.core.aop.interceptor.Interceptor; import fun.asgc.neutrino.core.cache.Cache; @@ -43,6 +44,7 @@ import java.util.Map; * @author: aoshiguchen * @date: 2022/6/28 */ +@NonIntercept @Component public class SqlMapperInterceptor implements Interceptor { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/exception/ContainerInitException.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/exception/ContainerInitException.java deleted file mode 100644 index 0ddfe17f..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/exception/ContainerInitException.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package fun.asgc.neutrino.core.exception; - -import fun.asgc.neutrino.core.container.Container; - -/** - * - * @author: aoshiguchen - * @date: 2022/6/16 - */ -public class ContainerInitException extends RuntimeException { - - public ContainerInitException(Container container) { - super(String.format("容器:[%s] 初始化异常", container.getClass().getName())); - } - -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/launcher/NeutrinoLauncher.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/launcher/NeutrinoLauncher.java index f5c09df9..f61a0a6d 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/launcher/NeutrinoLauncher.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/launcher/NeutrinoLauncher.java @@ -25,8 +25,6 @@ package fun.asgc.neutrino.core.launcher; import com.google.common.collect.Lists; import fun.asgc.neutrino.core.annotation.NeutrinoApplication; import fun.asgc.neutrino.core.constant.MetaDataConstant; -import fun.asgc.neutrino.core.container.ApplicationContainer; -import fun.asgc.neutrino.core.container.DefaultApplicationContainer; import fun.asgc.neutrino.core.context.ApplicationConfig; import fun.asgc.neutrino.core.context.ApplicationContext; import fun.asgc.neutrino.core.context.Environment; @@ -43,7 +41,6 @@ import java.lang.management.ManagementFactory; @Slf4j public class NeutrinoLauncher { private Environment environment; - private ApplicationContainer applicationContainer; public static SystemUtil.RunContext run(final Class clazz, final String[] args) { Assert.notNull(clazz, "启动类不能为空!"); @@ -65,11 +62,6 @@ public class NeutrinoLauncher { stopWatch.start(); environmentInit(); -// this.applicationContainer = new DefaultApplicationContainer(environment); -// SystemUtil.RunContext runContext = SystemUtil.waitProcessDestroy(() -> { -// this.applicationContainer.destroy(); -// log.info("Application already stop."); -// }); ApplicationContext context = new ApplicationContext(environment); context.run(); SystemUtil.RunContext runContext = SystemUtil.waitProcessDestroy(() -> { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/BeanManager.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/BeanManager.java index 4f045a3e..75036eb7 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/BeanManager.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/BeanManager.java @@ -22,16 +22,13 @@ package fun.asgc.neutrino.core.util; -import fun.asgc.neutrino.core.container.BeanContainer; -import fun.asgc.neutrino.core.context.ApplicationConfig; -import fun.asgc.neutrino.core.context.ApplicationContext; -import fun.asgc.neutrino.core.bean.BeanWrapper; -import fun.asgc.neutrino.core.context.Environment; +import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.core.bean.BeanFactory; +import fun.asgc.neutrino.core.bean.BeanFactoryAware; import lombok.extern.slf4j.Slf4j; import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; /** * @@ -39,65 +36,28 @@ import java.util.stream.Collectors; * @date: 2022/6/16 */ @Slf4j -public class BeanManager { - private static volatile ApplicationContext context; +@NonIntercept +@Component +public class BeanManager implements BeanFactoryAware { + private static volatile BeanFactory beanFactory; - public static synchronized void setContext(ApplicationContext ctx) { - Assert.notNull(ctx, "ctx不能为空!"); - context = ctx; - log.info("BeanManager初始化完成"); + @Override + public void setBeanFactory(BeanFactory beanFactory) { + BeanManager.beanFactory = beanFactory; } public static Object getBean(String name) { - if (StringUtil.isEmpty(name)) { - return null; + if (null == beanFactory) { + throw new RuntimeException("Beanfactory尚未初始化"); } - if ("environment".equals(name)) { - return context.getEnvironment(); - } else if("applicationConfig".equals(name)) { - return context.getApplicationConfig(); - } else if("beanContainer".equals(name)) { - return context.getBeanContainer(); - } else if ("applicationContext".equals(name)) { - return context; - } else { - if (null != context.getBeanContainer()) { - BeanWrapper bean = context.getBeanContainer().getBean(name); - if (null != bean) { - if (!bean.hasInstance()) { - bean.newInstance(context); - } - return bean.getInstance(); - } - } - } - return null; + return beanFactory.getBean(name); } public static T getBean(Class clazz) { - if (null == context) { - return null; + if (null == beanFactory) { + throw new RuntimeException("Beanfactory尚未初始化"); } - if (clazz == Environment.class) { - return (T)context.getEnvironment(); - } else if(clazz == ApplicationConfig.class) { - return (T)context.getApplicationConfig(); - } else if(clazz == BeanContainer.class) { - return (T)context.getBeanContainer(); - } else if (clazz == ApplicationContext.class) { - return (T)context; - } else { - if (null != context.getBeanContainer()) { - BeanWrapper bean = context.getBeanContainer().getBean(clazz); - if (null != bean) { - if (!bean.hasInstance()) { - bean.newInstance(context); - } - return (T)bean.getInstance(); - } - } - } - return null; + return beanFactory.getBean(clazz); } /** @@ -107,15 +67,10 @@ public class BeanManager { * @return */ public static List getBeanListBySuperClass(Class superClass) { - if (null == context || null == context.getBeanContainer()) { - return null; + if (null == beanFactory) { + throw new RuntimeException("Beanfactory尚未初始化"); } - return context.getBeanContainer().beanList().stream() - .map(BeanWrapper::getType) - .filter(item -> superClass.isAssignableFrom(item)) - .map(item -> (T)getBean(item)) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + return beanFactory.getBeanList(superClass); } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java index da8cca07..c469e0bf 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java @@ -244,4 +244,18 @@ public class ClassUtil { public static boolean hasNoArgsConstructor(Class clazz) { return Stream.of(clazz.getConstructors()).filter(c -> c.getParameterCount() == 0).count() > 0; } + + /** + * 获取bean名称 + * @param type + * @return + */ + public static String getBeanName(Class type) { + String name = TypeUtil.getDefaultVariableName(type); + Component component = ClassUtil.getAnnotation(type, Component.class); + if (null != component && StringUtil.notEmpty(component.value())) { + name = component.value(); + } + return name; + } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Cat.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Cat.java index 25a2144d..2cef5f5d 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Cat.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Cat.java @@ -23,12 +23,14 @@ package fun.asgc.neutrino.core.bean.test1; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Init; +import fun.asgc.neutrino.core.annotation.NonIntercept; /** * * @author: aoshiguchen * @date: 2022/7/4 */ +@NonIntercept @Component public class Cat extends Animal { diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Dog.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Dog.java index b5446b47..6057a90c 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Dog.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Dog.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.bean.test1; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; import lombok.Data; import lombok.experimental.Accessors; @@ -33,6 +34,7 @@ import lombok.experimental.Accessors; @Accessors(chain = true) @Data @Component +@NonIntercept public class Dog extends Animal { private String color; @Override diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Launcher.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Launcher.java index f82a99e9..845a4950 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Launcher.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/Launcher.java @@ -54,6 +54,8 @@ public class Launcher { private Dog dog2; @Autowired private Dog dog10; + @Autowired + private Dog dog11; @Autowired(value = "dog", matchMode = BeanMatchMode.ByName, parameterTypes = Dog.class) private Object[] dogs1; @Autowired(matchMode = BeanMatchMode.ByType, parameterTypes = Dog.class) @@ -79,4 +81,9 @@ public class Launcher { public static void main(String[] args) { NeutrinoLauncher.run(Launcher.class, args).sync(); } + + @Bean + public Dog dog11() { + return new Dog().setColor("red"); + } } diff --git a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ProxyClientRunner.java b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ProxyClientRunner.java index 27383ff5..17128075 100644 --- a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ProxyClientRunner.java +++ b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ProxyClientRunner.java @@ -26,6 +26,7 @@ import com.alibaba.fastjson.JSONObject; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Bean; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.runner.ApplicationRunner; import fun.asgc.neutrino.core.util.CollectionUtil; import fun.asgc.neutrino.core.util.FileUtil; @@ -57,6 +58,7 @@ import java.security.KeyStore; * @date: 2022/6/16 */ @Slf4j +@NonIntercept @Component public class ProxyClientRunner implements ApplicationRunner { @Autowired diff --git a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageConnectHandler.java b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageConnectHandler.java index b94c8e1d..f72dbec5 100644 --- a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageConnectHandler.java +++ b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageConnectHandler.java @@ -25,6 +25,7 @@ package fun.asgc.neutrino.proxy.client.handler; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.client.config.ProxyConfig; import fun.asgc.neutrino.proxy.client.core.ProxyChannelBorrowListener; import fun.asgc.neutrino.proxy.client.util.ClientChannelMannager; @@ -37,6 +38,7 @@ import io.netty.channel.*; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.CONNECT) @Component public class ProxyMessageConnectHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageDisconnectHandler.java b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageDisconnectHandler.java index 0b083ce6..121c6040 100644 --- a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageDisconnectHandler.java +++ b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageDisconnectHandler.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.client.handler; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.client.util.ClientChannelMannager; import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; @@ -39,6 +40,7 @@ import io.netty.channel.ChannelHandlerContext; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.DISCONNECT) @Component public class ProxyMessageDisconnectHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageErrorHandler.java b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageErrorHandler.java index 8d7a901c..492f658c 100644 --- a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageErrorHandler.java +++ b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageErrorHandler.java @@ -25,6 +25,7 @@ package fun.asgc.neutrino.proxy.client.handler; import com.alibaba.fastjson.JSONObject; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.core.*; import io.netty.channel.ChannelHandlerContext; import lombok.extern.slf4j.Slf4j; @@ -35,6 +36,7 @@ import lombok.extern.slf4j.Slf4j; * @date: 2022/6/16 */ @Slf4j +@NonIntercept @Match(type = Constants.ProxyDataTypeName.ERROR) @Component public class ProxyMessageErrorHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageTransferHandler.java b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageTransferHandler.java index df463a31..93fd7ae5 100644 --- a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageTransferHandler.java +++ b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/handler/ProxyMessageTransferHandler.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.client.handler; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; import fun.asgc.neutrino.proxy.core.ProxyMessage; @@ -37,6 +38,7 @@ import io.netty.channel.ChannelHandlerContext; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.TRANSFER) @Component public class ProxyMessageTransferHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/core/ProxyServerRunner.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/core/ProxyServerRunner.java index d8ec155e..31099237 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/core/ProxyServerRunner.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/core/ProxyServerRunner.java @@ -25,6 +25,7 @@ package fun.asgc.neutrino.proxy.server.core; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Bean; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.runner.ApplicationRunner; import fun.asgc.neutrino.core.util.FileUtil; import fun.asgc.neutrino.proxy.core.IdleCheckHandler; @@ -50,6 +51,7 @@ import java.security.KeyStore; * @date: 2022/6/16 */ @Slf4j +@NonIntercept @Component public class ProxyServerRunner implements ApplicationRunner { @Autowired diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageAuthHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageAuthHandler.java index a437c5de..c40ef0fc 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageAuthHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageAuthHandler.java @@ -26,6 +26,7 @@ import com.alibaba.fastjson.JSONObject; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.util.BeanManager; import fun.asgc.neutrino.proxy.core.*; import fun.asgc.neutrino.proxy.server.config.ProxyConfig; @@ -51,6 +52,7 @@ import java.util.List; * @date: 2022/6/16 */ @Slf4j +@NonIntercept @Match(type = Constants.ProxyDataTypeName.AUTH) @Component public class ProxyMessageAuthHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageConnectHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageConnectHandler.java index c9d201d4..f9179e43 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageConnectHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageConnectHandler.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.server.handler; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; import fun.asgc.neutrino.proxy.core.ProxyMessage; @@ -38,6 +39,7 @@ import io.netty.channel.ChannelOption; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.CONNECT) @Component public class ProxyMessageConnectHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageDisconnectHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageDisconnectHandler.java index 1baf8d03..34f5ebb9 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageDisconnectHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageDisconnectHandler.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.server.handler; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; import fun.asgc.neutrino.proxy.core.ProxyMessage; @@ -39,6 +40,7 @@ import io.netty.channel.ChannelHandlerContext; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.DISCONNECT) @Component public class ProxyMessageDisconnectHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageHeartbeatHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageHeartbeatHandler.java index 4a92ce90..5eddf811 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageHeartbeatHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageHeartbeatHandler.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.server.handler; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; import fun.asgc.neutrino.proxy.core.ProxyMessage; @@ -35,6 +36,7 @@ import io.netty.channel.ChannelHandlerContext; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.HEARTBEAT) @Component public class ProxyMessageHeartbeatHandler implements ProxyMessageHandler { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageTransferHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageTransferHandler.java index f11ea08d..daabcacf 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageTransferHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/handler/ProxyMessageTransferHandler.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.proxy.server.handler; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.Match; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; import fun.asgc.neutrino.proxy.core.ProxyMessage; @@ -37,6 +38,7 @@ import io.netty.channel.ChannelHandlerContext; * @author: aoshiguchen * @date: 2022/6/16 */ +@NonIntercept @Match(type = Constants.ProxyDataTypeName.TRANSFER) @Component public class ProxyMessageTransferHandler implements ProxyMessageHandler {