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 73d977bf..db4f8fdd 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 @@ -206,7 +206,20 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, private T doGetBean(String name, Object... args) throws BeanException { List beanList = findBeanList(name); if (CollectionUtil.isEmpty(beanList)) { - return null; + // 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(); } else if (beanList.size() > 1) { throw new BeanException(String.format("Bean[name:%s] 存在多个实例!", name)); } @@ -239,7 +252,20 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, private T doGetBean(Class type, Object... args) throws BeanException { List beanList = findBeanList(type); if (CollectionUtil.isEmpty(beanList)) { - return null; + // 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(); } else if (beanList.size() > 1) { throw new BeanException(String.format("Bean[type:%s] 存在多个实例!", type)); } @@ -382,6 +408,22 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry, 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 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 a024aff8..f82a99e9 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 @@ -79,5 +79,4 @@ public class Launcher { public static void main(String[] args) { NeutrinoLauncher.run(Launcher.class, args).sync(); } - } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java new file mode 100644 index 00000000..985879c0 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java @@ -0,0 +1,64 @@ +/** + * 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.test2; + +import lombok.Data; +import org.junit.Test; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/5 + */ +public class Test1 { + + @Test + public void test1() throws IntrospectionException, InvocationTargetException, IllegalAccessException { + PropertyDescriptor descriptor = new PropertyDescriptor("age", Student.class); + Student student = new Student(); + Method method = descriptor.getWriteMethod(); + method.invoke(student, 30); + System.out.println(student); + } + + public static class Student { + private String name; + private int age; + private int score; + + public int getAge() { + return age; + } + +// public void setAge(int age) { +// this.age = age; +// } + public void setAge(int age) { + System.out.println("111"); + } + } +}