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 fe0e7edf..73d977bf 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 @@ -31,6 +31,7 @@ import fun.asgc.neutrino.core.util.LockUtil; import fun.asgc.neutrino.core.util.TypeUtil; import lombok.extern.slf4j.Slf4j; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -215,7 +216,20 @@ 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)) { - return null; + // 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(); } else if (beanList.size() > 1) { throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); } @@ -234,6 +248,12 @@ 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(); + } + } if (null == bean) { return null; } @@ -332,6 +352,52 @@ 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 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 @@ -339,11 +405,17 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, protected void addBean(BeanWrapper bean) throws BeanException { beanCache.put(new BeanIdentity(bean.getName(), bean.getType()), bean); if (FactoryBean.class.isAssignableFrom(bean.getType())) { + dependencyCheck(bean); + newInstance(bean); // 工厂bean - if (factoryBeanCache.containsKey(bean.getBeanIdentity())) { + 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())); } - factoryBeanCache.put(bean.getBeanIdentity(), bean); + 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); } } @@ -478,7 +550,7 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, () -> bean.getInstance() ); } catch (Exception e) { - throw new BeanException(String.format("Bean[type:%s name:%s] getOrNew bean实例异常!"), e); + throw new BeanException(String.format("Bean[type:%s name:%s] getOrNew bean实例异常!", bean.getType().getName(), bean.getName()), e); } } 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 c0481798..4cdfdf64 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 @@ -124,11 +124,7 @@ public class SimpleBeanFactory extends AbstractBeanFactory { NonIntercept nonIntercept = ClassUtil.getAnnotation(bean.getType(), NonIntercept.class); boolean isNonIntercept = (null != nonIntercept && nonIntercept.value()) ? true : false; - // TODO 此处factoryBean可能还未注入、初始化,需要思考优化 - FactoryBean factoryBean = getFactory(bean); - if (null != factoryBean) { - bean.setInstance(factoryBean.getInstance()); - } else if (ClassUtil.isInterface(bean.getType())) { + if (ClassUtil.isInterface(bean.getType())) { bean.setInstance(Aop.get(bean.getType())); } else if(bean.getType().isAnnotationPresent(Configuration.class)) { bean.setInstance(ConfigUtil.getYmlConfig(bean.getType())); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/LifeCycleManager.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/LifeCycleManager.java index 1c5dc806..e04b55b5 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/LifeCycleManager.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/LifeCycleManager.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.context; import fun.asgc.neutrino.core.base.CodeBlock; +import fun.asgc.neutrino.core.util.FunctionUtil; /** * @@ -40,34 +41,28 @@ public class LifeCycleManager { public synchronized void init(CodeBlock codeBlock) { if (this.status == LifeCycleStatus.CREATE) { - try { + FunctionUtil.ignoreException(() -> { codeBlock.execute(); this.status = LifeCycleStatus.INIT; - } catch (Exception e) { - e.printStackTrace(); - } + }); } } public synchronized void run(CodeBlock codeBlock) { if (this.status == LifeCycleStatus.INIT) { - try { + FunctionUtil.ignoreException(() -> { codeBlock.execute(); this.status = LifeCycleStatus.RUN; - } catch (Exception e) { - e.printStackTrace(); - } + }); } } public synchronized void destroy(CodeBlock codeBlock) { if (this.status != LifeCycleStatus.DESTROY) { - try { + FunctionUtil.ignoreException(() -> { codeBlock.execute(); this.status = LifeCycleStatus.DESTROY; - } catch (Exception e) { - e.printStackTrace(); - } + }); } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/FunctionUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/FunctionUtil.java index 629f4df6..d5da171b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/FunctionUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/FunctionUtil.java @@ -21,6 +21,9 @@ */ package fun.asgc.neutrino.core.util; +import fun.asgc.neutrino.core.aop.AopCallback; +import fun.asgc.neutrino.core.base.CodeBlock; + import java.util.function.Supplier; /** @@ -51,4 +54,31 @@ public class FunctionUtil { return null; } + /** + * 忽略异常 + * @param codeBlock + */ + public static void ignoreException(CodeBlock codeBlock) { + try { + codeBlock.execute(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 忽略异常 + * @param callback + * @param + * @return + */ + public static T ignoreException(AopCallback callback) { + try { + return callback.callback(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + } 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 782b4159..b5446b47 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,14 +22,19 @@ package fun.asgc.neutrino.core.bean.test1; import fun.asgc.neutrino.core.annotation.Component; +import lombok.Data; +import lombok.experimental.Accessors; /** * * @author: aoshiguchen * @date: 2022/7/4 */ +@Accessors(chain = true) +@Data @Component public class Dog extends Animal { + private String color; @Override public void call() { System.out.println("汪汪汪"); diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/FactoryBean1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/FactoryBean1.java new file mode 100644 index 00000000..14f96c45 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test1/FactoryBean1.java @@ -0,0 +1,48 @@ +/** + * 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.bean.test1; + +import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.core.bean.BeanIdentity; +import fun.asgc.neutrino.core.bean.FactoryBean; +import fun.asgc.neutrino.core.exception.BeanException; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/4 + */ +@NonIntercept +@Component +public class FactoryBean1 implements FactoryBean { + + @Override + public Dog getInstance() throws BeanException { + return new Dog().setColor("黄色"); + } + + @Override + public BeanIdentity getBeanIdentity() { + return new BeanIdentity("dog10", Dog.class); + } +} 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 0f775e18..a024aff8 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 @@ -23,7 +23,7 @@ package fun.asgc.neutrino.core.bean.test1; import fun.asgc.neutrino.core.annotation.*; import fun.asgc.neutrino.core.bean.BeanMatchMode; -import fun.asgc.neutrino.core.bean.BeanStatus; +import fun.asgc.neutrino.core.bean.SimpleBeanFactory; import fun.asgc.neutrino.core.launcher.NeutrinoLauncher; import java.util.List; @@ -52,6 +52,8 @@ public class Launcher { private Dog dog; @Autowired("dog") private Dog dog2; + @Autowired + private Dog dog10; @Autowired(value = "dog", matchMode = BeanMatchMode.ByName, parameterTypes = Dog.class) private Object[] dogs1; @Autowired(matchMode = BeanMatchMode.ByType, parameterTypes = Dog.class) @@ -59,11 +61,14 @@ public class Launcher { @Autowired(matchMode = BeanMatchMode.ByType, parameterTypes = Animal.class) private List list; + @Autowired + private SimpleBeanFactory applicationBeanFactory; + @Init public void init() { System.out.println("初始化---"); - cat.call(); - dog.call(); +// cat.call(); +// dog.call(); } @Destroy