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; + } }