Java 自定义注解简单使用 第一步:在 pom.xml 下引入反射框架 <dependency> <groupId>org.reflections</groupId> <artifactId >reflections</artifactId> <version>0.10.2</version> </dependency> 第二步:自定义注解 package com.zibo.api.annotation 第四步:写个工具类扫描被注解的类(核心) package com.zibo.api.utils; import com.zibo.api.annotation.MyConfig; import org.reflections.Reflections new LinkedHashMap<>(); // 要扫描的包 String packageName = "com.zibo.api.entity"; Reflections f = new Reflections(packageName); // 获取扫描到的标记注解的集合 Set<Class<?
开始想着用反射自己写一个工具类,后面懒得去折腾,就直接用第三方工具包了 , 这里我用的是org.reflections . maven配置: <dependency> <groupId>org.reflections </groupId> <artifactId>reflections-spring</artifactId> <version>${reflections.version}</version> 另外就是代码引用了,以DelayedBaseMonitor接口为例: List<DelayedBaseMonitor> monitorLists = new ArrayList<>(); Reflections reflections = new Reflections(delayedFindPackageName); Set<Class<? extends DelayedBaseMonitor>> monitorClasses = reflections.getSubTypesOf(DelayedBaseMonitor.class);
com.tenmao.utils.mybatis.converter.CodedTypeTypeAdapter; import lombok.extern.slf4j.Slf4j; import org.reflections.Reflections ; import org.reflections.scanners.SubTypesScanner; import org.springframework.http.converter.json.GsonHttpMessageConverter public HttpMessageConverter() { final GsonBuilder builder = new GsonBuilder(); final Reflections reflections = new Reflections("com.tenmao", new SubTypesScanner(true)); final Set<String> allClasses = reflections.getStore().getSubTypesOf(CodedEnum.class.getName()); for (String klass : allClasses
io.swagger.annotations.ApiOperation;import org.apache.shiro.authz.annotation.RequiresPermissions;import org.reflections.Reflections ;import org.reflections.scanners.MethodAnnotationsScanner;import org.reflections.util.ClasspathHelper ;import org.reflections.util.ConfigurationBuilder;import org.springframework.web.bind.annotation. scanPackage 需要扫描的包路径 */ private static void getRequestMappingMethod(String scanPackage) { //设置扫描路径 Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(scanPackage
工具类 依赖 <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> ; import org.reflections.Reflections; import org.reflections.scanners.*; import org.reflections.util.ClasspathHelper ; import org.reflections.util.ConfigurationBuilder; import org.reflections.util.FilterBuilder; import reflections = new Reflections( "com.mh.others.reflect_", //指定被扫描的包名 /cmj/dump/a.txt"); //将数据保存到a.txt Reflections collect = reflections.collect(new File("/home/cmj
typeFromId:是反序列化的时候告诉序列化器怎么根据标识符来识别到具体类型,这里用了反射,在程序启动时,把要加载的包通过Reflections加载进来。 > getSubType(String type) { Reflections reflections = ReflectionsCache.getReflections(); class ReflectionsCache { private static Reflections reflections; public static void setReflections (Reflections _reflections) { reflections = _reflections; } public static Reflections reflections = new Reflections(packages); if (reflections !
(from one point on the earth’s surface to another distant point on the earth’s surface) by multiple reflections In this problem, the focus is particularly on reflections off the ocean surface. It has been found empirically that reflections off a turbulent ocean are attenuated more than reflections If additional reflections (2 through n) take place off calm oceans, what is the maximum number of hops (mss1) Part II: How do your findings from Part I compare with HF reflections off mountainous or rugged
首先添加一个辅助库: <dependency> <groupId>org.reflections</groupId> <artifactId>reflections </artifactId> <version>0.9.12</version> </dependency> 然后初始化Reflections,FilterBuilder ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner extends Object>> allClasses = reflections.getSubTypesOf(Object.class); Map<String
-- https://mvnrepository.com/artifact/org.reflections/reflections --> <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version >0.10.2</version> </dependency> 示例代码: import org.reflections.Reflections; import java.util.HashMap reflections = new Reflections("com.demo.xxx"); Set<Class<? >> subTypes = reflections.get(SubTypes.of(Handler.class).asClass()); for(Class<?
工具类包含3个后面会用到的方法: scanAnnotatedClass() :扫描指定包下的被指定注解标记的类(使用Reflections这个反射框架一行代码即可解决扫描获取指定注解的类)。 extends Annotation> annotation) { Reflections reflections = new Reflections(packageName, new >> annotatedClass = reflections.getTypesAnnotatedWith(annotation, true); log.info("The number > interfaceClass) { Reflections reflections = new Reflections(packageName); return reflections.getSubTypesOf
com.week7i.share.common.persistence.interceptor; import com.week7i.share.common.persistence.Page; import com.week7i.share.common.utils.Reflections boundSql.getParameterObject()); //解决MyBatis 分页foreach 参数失效 start if (Reflections.getFieldValue = null) { MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters "); Reflections.setFieldValue(newBoundSql, "metaParameters", mo);
现在,将为这个IOC容器添加DI注入功能 二、实现 在编写之前,我们先加一个工具类,用来获取接口所实现的子类Class对象,也是通过子类 <dependency> <groupId>org.reflections </groupId> <artifactId>reflections</artifactId> <version>0.10.2</version> </dependency> 代码如下, com.banmoon.test.mockioc.annotation.Bean; import com.banmoon.test.mockioc.annotation.Di; import lombok.extern.slf4j.Slf4j; import org.reflections.Reflections * 扫描的包路径 */ private String packagePath; /** * 扫描的包路径反射工具类 */ private Reflections packagePath) throws Exception { this.packagePath = packagePath; this.packageReflections = new Reflections
-- https://mvnrepository.com/artifact/org.reflections/reflections --> <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> org.apache.ibatis.annotations.SelectProvider; import org.apache.ibatis.session.SqlSessionFactory; import org.reflections.Reflections reflections = new Reflections(STRATEGY_IMPLEMENTATION_PACKAGE); // 获取含有 CacheNamespace 注解的所有 >> classList = reflections.getTypesAnnotatedWith(CacheNamespace.class); // 遍历所有符合条件的类,进行相关业务处理
AutoConfigurationPackages.get(beanFactory); for (String packageName : packageNames) { Reflections reflections = new Reflections(new ConfigurationBuilder() .forPackages(packageName addScanners(new MethodParameterScanner() ) // 添加方法参数扫描工具 ); Set<Method> methodSet = reflections.getMethodsAnnotatedWith
com.example.strategyTwo.annotation.Pay; import com.example.strategyTwo.service.PayStrategy; import org.reflections.Reflections static final Map<String, Class> STRATEGY_MAP = new HashMap<>(); // 获取所有支付策略 static { Reflections reflections = new Reflections(PAY_STRATEGY_IMPLEMENTATION_PACKAGE); Set<Class<? >> classSet = reflections.getTypesAnnotatedWith(Pay.class); classSet.forEach(aClass -> {
AutoConfigurationPackages.get(beanFactory); for (String packageName : packageNames) { Reflections reflections = new Reflections(new ConfigurationBuilder() .forPackages(packageName addScanners(new MethodParameterScanner() ) // 添加方法参数扫描工具 ); Set<Method> methodSet = reflections.getMethodsAnnotatedWith
代码实现自定义注解 步骤: 使用 @Interface 定义一个注解 使用反射包 reflections 获得被标记的所有类,以及执行它们被标记的方法 <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version
getBasePackages(importingClassMetadata); for (String basePackage : basePackages) { Reflections reflections = new Reflections(basePackage); Set<Class<? >> spiClasses = reflections.getTypesAnnotatedWith(Spi.class); if(!
getBasePackages(importingClassMetadata); for (String basePackage : basePackages) { Reflections reflections = new Reflections(basePackage); Set<Class<? >> spiClasses = reflections.getTypesAnnotatedWith(Spi.class); if(!
100%); border-radius: 20% 50% 50% 20%; opacity: 0.65; z-index: 0; } .ball .reflections '>