diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NeutrinoApplication.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NeutrinoApplication.java index 5821578c..91cf71ba 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NeutrinoApplication.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NeutrinoApplication.java @@ -33,6 +33,7 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Documented @Component +@NonIntercept public @interface NeutrinoApplication { String[] scanBasePackages() default {}; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NonIntercept.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NonIntercept.java new file mode 100644 index 00000000..fff1f552 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/NonIntercept.java @@ -0,0 +1,40 @@ +/** + * 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.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 用于指定一个bean不需要拦截.这个标注只是一个建议 + * 1、bean是一个接口,将强制采用aop代理 + * 2、bean采用factoryBean实例化,由factoryBean决定是否代理 + * @author: aoshiguchen + * @date: 2022/7/4 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface NonIntercept { + boolean value() default true; +} 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 e7f4d92a..c0481798 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 @@ -121,6 +121,9 @@ public class SimpleBeanFactory extends AbstractBeanFactory { () -> BeanStatus.DEPENDENCY_CHECKING == bean.getStatus(), bean, () -> { + 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) { @@ -131,7 +134,11 @@ public class SimpleBeanFactory extends AbstractBeanFactory { bean.setInstance(ConfigUtil.getYmlConfig(bean.getType())); } else { if (ClassUtil.hasNoArgsConstructor(bean.getType())) { - bean.setInstance(Aop.get(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())); 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 181607f8..0f775e18 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 @@ -21,10 +21,7 @@ */ package fun.asgc.neutrino.core.bean.test1; -import fun.asgc.neutrino.core.annotation.Autowired; -import fun.asgc.neutrino.core.annotation.Destroy; -import fun.asgc.neutrino.core.annotation.Init; -import fun.asgc.neutrino.core.annotation.NeutrinoApplication; +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.launcher.NeutrinoLauncher;