新增注解,减少不必要的Aop代理。

This commit is contained in:
aoshiguchen
2022-07-04 18:07:52 +08:00
parent 2e726b6513
commit 3a02782c66
4 changed files with 50 additions and 5 deletions
@@ -33,6 +33,7 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@NonIntercept
public @interface NeutrinoApplication {
String[] scanBasePackages() default {};
}
@@ -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;
}
@@ -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()));
@@ -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;