新增factoryBean相关测试.

This commit is contained in:
aoshiguchen
2022-07-04 23:23:02 +08:00
parent 3a02782c66
commit e0efebba2c
7 changed files with 175 additions and 24 deletions
@@ -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> T doGetBeanByName(Class<T> type, String name, Object... args) throws BeanException {
List<BeanWrapper> beanList = findBeanList(type, name);
if (CollectionUtil.isEmpty(beanList)) {
return null;
// TODO check
List<BeanWrapper> 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> T doGetBeanByTypeAndName(Class<T> 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<BeanWrapper> findFactoryBeanListByName(Class<?> type, String name) {
Assert.notNull(type, "bean的类型不能为空!");
Assert.notNull(name, "bean的名称不能为空!");
List<BeanWrapper> 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<BeanWrapper> findFactoryBeanListByType(Class<?> type) {
Assert.notNull(type, "bean的类型不能为空!");
List<BeanWrapper> 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);
}
}
@@ -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()));
@@ -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();
}
});
}
}
@@ -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 <T>
* @return
*/
public static <T> T ignoreException(AopCallback<T> callback) {
try {
return callback.callback();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@@ -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("汪汪汪");
@@ -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<Dog> {
@Override
public Dog getInstance() throws BeanException {
return new Dog().setColor("黄色");
}
@Override
public BeanIdentity getBeanIdentity() {
return new BeanIdentity("dog10", Dog.class);
}
}
@@ -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<Animal> list;
@Autowired
private SimpleBeanFactory applicationBeanFactory;
@Init
public void init() {
System.out.println("初始化---");
cat.call();
dog.call();
// cat.call();
// dog.call();
}
@Destroy