From 860678c2a5ad7ad879cd8ff3ec149b92c9c9bd07 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Fri, 8 Jul 2022 17:57:29 +0800 Subject: [PATCH] =?UTF-8?q?bean=E5=AE=9E=E4=BE=8B=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3bean=E6=96=B9=E6=B3=95=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1=E8=B0=83=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/bean/SimpleBeanFactory.java | 2 + .../core/bean/SingletonInterceptor.java | 53 +++++++++++++++++++ .../fun/asgc/neutrino/core/bean/test3/A.java | 33 ++++++++++++ .../neutrino/core/bean/test3/Launcher.java | 15 ++++++ 4 files changed, 103 insertions(+) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SingletonInterceptor.java create mode 100644 neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/A.java 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 48605f5f..41c92f69 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 @@ -117,6 +117,8 @@ public class SimpleBeanFactory extends AbstractBeanFactory { if (null == bean) { continue; } + Aop.intercept(method, SingletonInterceptor.class); + String beanName = bean.value(); if (StringUtil.isEmpty(beanName)) { beanName = method.getName(); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SingletonInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SingletonInterceptor.java new file mode 100644 index 00000000..7469535f --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SingletonInterceptor.java @@ -0,0 +1,53 @@ +/** + * 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; + +import fun.asgc.neutrino.core.aop.Invocation; +import fun.asgc.neutrino.core.aop.interceptor.Interceptor; +import fun.asgc.neutrino.core.util.LockUtil; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 单例拦截器 + * @author: aoshiguchen + * @date: 2022/7/8 + */ +public class SingletonInterceptor implements Interceptor { + private static final Map cache = new ConcurrentHashMap<>(); + + @Override + public void intercept(Invocation inv) throws Exception { + LockUtil.doubleCheckProcess( + () -> !cache.containsKey(inv.getTargetMethod()), + inv.getTargetMethod(), + () -> { + inv.invoke(); + cache.put(inv.getTargetMethod(), inv.getReturnValue()); + } + ); + inv.setReturnValue(cache.get(inv.getTargetMethod())); + } + +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/A.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/A.java new file mode 100644 index 00000000..d69baa86 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/A.java @@ -0,0 +1,33 @@ +/** + * 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.test3; + +/** + * @author: aoshiguchen + * @date: 2022/7/8 + */ +public interface A { + + Dog dog(); + + Person person(Dog dog); +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/Launcher.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/Launcher.java index fc38d499..2e43951a 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/Launcher.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test3/Launcher.java @@ -24,6 +24,7 @@ package fun.asgc.neutrino.core.bean.test3; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Bean; import fun.asgc.neutrino.core.annotation.NeutrinoApplication; +import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.context.ApplicationRunner; import fun.asgc.neutrino.core.context.NeutrinoLauncher; @@ -32,14 +33,19 @@ import fun.asgc.neutrino.core.context.NeutrinoLauncher; * @author: aoshiguchen * @date: 2022/7/8 */ +@NonIntercept(false) @NeutrinoApplication public class Launcher implements ApplicationRunner { @Autowired private Person person; + @Autowired + private Person person2; @Override public void run(String[] args) { person.walkTheDog(); + System.out.println("---------------------"); + person.walkTheDog(); } public static void main(String[] args) { @@ -48,6 +54,7 @@ public class Launcher implements ApplicationRunner { @Bean public Dog dog() { + System.out.println("hhahahahahahahahaah"); return new Dog("大黄"); } @@ -57,4 +64,12 @@ public class Launcher implements ApplicationRunner { person.setPet(dog); return person; } + + @Bean + public Person person2() { + Person person = new Person("李四"); + person.setPet(dog()); + System.out.println("-----------------" + (dog() == dog())); + return person; + } }