新增@Singleton注解,使得普通方法也能方便的增强单例功能,@Bean注解继承@@Singleton注解

This commit is contained in:
aoshiguchen
2022-07-08 20:11:39 +08:00
parent a6c1dfe190
commit 70db0fad5d
8 changed files with 93 additions and 4 deletions
@@ -34,6 +34,7 @@ import java.lang.annotation.Target;
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Singleton
public @interface Bean {
String value() default "";
}
@@ -0,0 +1,37 @@
/**
* 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;
/**
* @author: aoshiguchen
* @date: 2022/7/8
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Singleton {
boolean value() default true;
}
@@ -19,9 +19,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.bean;
package fun.asgc.neutrino.core.aop;
import fun.asgc.neutrino.core.aop.Invocation;
import fun.asgc.neutrino.core.aop.interceptor.Interceptor;
import fun.asgc.neutrino.core.util.LockUtil;
@@ -21,7 +21,9 @@
*/
package fun.asgc.neutrino.core.aop.interceptor;
import fun.asgc.neutrino.core.annotation.Singleton;
import fun.asgc.neutrino.core.aop.Intercept;
import fun.asgc.neutrino.core.aop.SingletonInterceptor;
import fun.asgc.neutrino.core.base.GlobalConfig;
import fun.asgc.neutrino.core.cache.Cache;
import fun.asgc.neutrino.core.cache.MemoryCache;
@@ -262,6 +264,11 @@ public class InterceptorFactory {
}
}
}
Singleton singleton = ClassUtil.getAnnotation(targetMethod, Singleton.class);
if (null != singleton && singleton.value()) {
SingletonInterceptor interceptor = getOrNew(SingletonInterceptor.class);
interceptors.add(interceptor);
}
return interceptors;
}
@@ -24,6 +24,7 @@ package fun.asgc.neutrino.core.bean;
import com.google.common.collect.Lists;
import fun.asgc.neutrino.core.annotation.*;
import fun.asgc.neutrino.core.aop.Aop;
import fun.asgc.neutrino.core.aop.SingletonInterceptor;
import fun.asgc.neutrino.core.aop.interceptor.ExceptionHandler;
import fun.asgc.neutrino.core.aop.interceptor.Filter;
import fun.asgc.neutrino.core.aop.interceptor.Interceptor;
@@ -117,8 +118,6 @@ 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();
@@ -31,6 +31,7 @@ import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.JarURLConnection;
import java.net.URL;
@@ -213,6 +214,37 @@ public class ClassUtil {
return annotation;
}
public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationType) {
T annotation = method.getAnnotation(annotationType);
if (null == annotation) {
Annotation[] annotations = method.getAnnotations();
for (Annotation item : annotations) {
Class<? extends Annotation> aClass = item.annotationType();
if (aClass.isAnnotationPresent(annotationType)) {
return aClass.getAnnotation(annotationType);
}
}
}
return annotation;
}
public static boolean isAnnotationPresent(Method method, Class annotationType) {
Annotation annotation = method.getAnnotation(annotationType);
if (null != annotation) {
return true;
}
if (null == annotation) {
Annotation[] annotations = method.getAnnotations();
for (Annotation item : annotations) {
Class<? extends Annotation> aClass = item.annotationType();
if (aClass.isAnnotationPresent(annotationType)) {
return true;
}
}
}
return false;
}
public static boolean isPublic(Class<?> clazz) {
return Modifier.isPublic(clazz.getModifiers());
}
@@ -22,6 +22,8 @@
package fun.asgc.neutrino.core.aop;
import fun.asgc.neutrino.core.annotation.Singleton;
import java.util.Arrays;
/**
@@ -34,7 +36,9 @@ public class Giraffe {
System.out.println("运行:" + Arrays.toString(args));
}
@Singleton
public Dog getDog() {
System.out.println("new Dog");
return new Dog();
}
}
@@ -52,4 +52,14 @@ public class Test5 {
panda.say("hello");
}
/**
* 单例测试
* @throws Exception
*/
@Test
public void test3() throws Exception{
Giraffe giraffe = Aop.get(Giraffe.class);
System.out.println(giraffe.getDog() == giraffe.getDog());
}
}