新增factoryBean相关测试

This commit is contained in:
aoshiguchen
2022-07-05 18:43:00 +08:00
parent e0efebba2c
commit d7c3551b5c
3 changed files with 108 additions and 3 deletions
@@ -206,7 +206,20 @@ public abstract class AbstractBeanFactory implements BeanFactory, BeanRegistry,
private <T> T doGetBean(String name, Object... args) throws BeanException {
List<BeanWrapper> beanList = findBeanList(name);
if (CollectionUtil.isEmpty(beanList)) {
return null;
// TODO check
List<BeanWrapper> 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> T doGetBean(Class<T> type, Object... args) throws BeanException {
List<BeanWrapper> beanList = findBeanList(type);
if (CollectionUtil.isEmpty(beanList)) {
return null;
// TODO check
List<BeanWrapper> 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<BeanWrapper> findFactoryBeanListByName(String name) {
Assert.notNull(name, "bean的名称不能为空!");
List<BeanWrapper> list = new ArrayList<>();
for (BeanIdentity identity : factoryBeanCache.keySet()) {
if (identity.getName().equals(name)) {
list.add(factoryBeanCache.get(identity));
}
}
return list;
}
/**
* 查找工厂bean列表
* @param type
@@ -79,5 +79,4 @@ public class Launcher {
public static void main(String[] args) {
NeutrinoLauncher.run(Launcher.class, args).sync();
}
}
@@ -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");
}
}
}