【debug】getAnnotation(Class.class) 为空问题

本文讲述了在自定义类名与系统内置类名相同情况下,如何避免类导入错误的问题。通过检查和确认导入类的具体类型,避免因类加载器不同或切面编程导致的错误。

出现这种情况的原因有很多,

今天我遇到的是最坑的一种情况:

import 错误的类

我自定义了一个Person,可系统本身有2个Person类

所以,解决办法:确认要 getAnotation(params) 中 params 类型是你要的类型

导包还是要自己导啊。

其他问题参考:

1. 类加载器不同导致

2.切面编程的坑

一个小技巧,喜欢请点赞。(一股快手风 --!)

我的aop是这样的,/** * Copyright (c) 2018 人人开源 All rights reserved. * * https://www.renren.io * * 版权所有,侵权必究! */ package com.depower.datasource.aspect; import com.depower.datasource.annotation.DataSource; import com.depower.datasource.config.DynamicContextHolder; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * 多数据源,切面处理类 * * @author Mark sunlightcs@gmail.com */ @Aspect @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class DataSourceAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); @Pointcut("@annotation(com.depower.datasource.annotation.DataSource) " + "|| @within(com.depower.datasource.annotation.DataSource)") public void dataSourcePointCut() { } @Around("dataSourcePointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Class targetClass = point.getTarget().getClass(); Method method = signature.getMethod(); DataSource targetDataSource = (DataSource)targetClass.getAnnotation(DataSource.class); DataSource methodDataSource = method.getAnnotation(DataSource.class); if(targetDataSource != null || methodDataSource != null){ String value; if(methodDataSource != null){ value = methodDataSource.value(); }else { value = targetDataSource.value(); } DynamicContextHolder.push(value); logger.debug("set datasource is {}", value); } try { return point.proceed(); } finally { DynamicContextHolder.poll(); logger.debug("clean datasource"); } } }
最新发布
08-30
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@323ebcc6] 2025-08-28 10:06:27.938 DEBUG 24804 --- [ main] o.s.a.aspectj.AspectJExpressionPointcut : Could not access current invocation - matching with limited context: java.lang.IllegalStateException: No MethodInvocation found: Check that an AOP invocation is in progress and that the ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, note that advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor! In addition, ExposeInvocationInterceptor and ExposeInvocationInterceptor.currentInvocation() must be invoked from the same thread. 2025-08-28 10:06:27.943 DEBUG 24804 --- [ main] c.d.datasource.aspect.DataSourceAspect : Set datasource to: tdengine 2025-08-28 10:06:27.947 DEBUG 24804 --- [ main] c.d.datasource.aspect.DataSourceAspect : Restored datasource to: default 2025-08-28 10:06:27.948 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.948 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 6, missCount = 1] 2025-08-28 10:06:27.950 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.951 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 7, missCount = 1] 2025-08-28 10:06:27.951 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.951 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 8, missCount = 1] 2025-08-28 10:06:27.952 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.952 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 9, missCount = 1] 2025-08-28 10:06:27.955 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.955 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 10, missCount = 1] 2025-08-28 10:06:27.957 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.957 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 11, missCount = 1] 2025-08-28 10:06:27.958 DEBUG 24804 --- [ main] tractDirtiesContextTestExecutionListener : After test method: context [DefaultTestContext@4758820d testClass = SyncBmsDataTestServicess, testInstance = com.depower.SyncBmsDataTestServicess@6362dcf2, testMethod = syncDataTest@SyncBmsDataTestServicess, testException = java.lang.NullPointerException, mergedContextConfiguration = [WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null]. 2025-08-28 10:06:27.958 DEBUG 24804 --- [ main] o.s.t.c.w.ServletTestExecutionListener : Resetting RequestContextHolder for test context [DefaultTestContext@4758820d testClass = SyncBmsDataTestServicess, testInstance = com.depower.SyncBmsDataTestServicess@6362dcf2, testMethod = syncDataTest@SyncBmsDataTestServicess, testException = java.lang.NullPointerException, mergedContextConfiguration = [WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]. 2025-08-28 10:06:27.965 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.965 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 12, missCount = 1] 2025-08-28 10:06:27.965 DEBUG 24804 --- [ main] tractDirtiesContextTestExecutionListener : After test class: context [DefaultTestContext@4758820d testClass = SyncBmsDataTestServicess, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.support.DependencyInjectionTestExecutionListener.reinjectDependencies' -> true]], class annotated with @DirtiesContext [false] with mode [null]. 2025-08-28 10:06:27.972 DEBUG 24804 --- [extShutdownHook] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 2147483647 2025-08-28 10:06:27.972 DEBUG 24804 --- [extShutdownHook] o.s.c.support.DefaultLifecycleProcessor : Bean 'documentationPluginsBootstrapper' completed its stop procedure 2025-08-28 10:06:27.974 INFO 24804 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler' 2025-08-28 10:06:27.975 DEBUG 24804 --- [extShutdownHook] o.s.d.r.l.RedisMessageListenerContainer : Stopped RedisMessageListenerContainer 2025-08-28 10:06:27.975 INFO 24804 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2025-08-28 10:06:27.980 INFO 24804 --- [extShutdownHook] o.s.s.quartz.SchedulerFactoryBean : Shutting down Quartz Scheduler 2025-08-28 10:06:27.980 INFO 24804 --- [extShutdownHook] org.quartz.core.QuartzScheduler : Scheduler DepowerScheduler_$_D213791756346770213 shutting down. 2025-08-28 10:06:27.980 INFO 24804 --- [extShutdownHook] org.quartz.core.QuartzScheduler : Scheduler DepowerScheduler_$_D213791756346770213 paused. 2025-08-28 10:06:27.981 DEBUG 24804 --- [extShutdownHook] org.quartz.simpl.SimpleThreadPool : Shutting down threadpool... 2025-08-28 10:06:27.981 DEBUG 24804 --- [extShutdownHook] org.quartz.simpl.SimpleThreadPool : Shutdown of threadpool complete. 2025-08-28 10:06:27.981 DEBUG 24804 --- [extShutdownHook] o.s.s.quartz.LocalDataSourceJobStore : JobStore background threads shutdown. 2025-08-28 10:06:27.981 INFO 24804 --- [extShutdownHook] org.quartz.core.QuartzScheduler : Scheduler DepowerScheduler_$_D213791756346770213 shutdown complete. 2025-08-28 10:06:27.987 DEBUG 24804 --- [extShutdownHook] io.lettuce.core.RedisClient : Initiate shutdown (100, 100, MILLISECONDS) 2025-08-28 10:06:27.995 DEBUG 24804 --- [extShutdownHook] i.l.c.resource.DefaultClientResources : Initiate shutdown (0, 2, SECONDS) 2025-08-28 10:06:27.996 DEBUG 24804 --- [extShutdownHook] i.l.c.r.DefaultEventLoopGroupProvider : Initiate shutdown (0, 2, SECONDS) 2025-08-28 10:06:28.005 DEBUG 24804 --- [extShutdownHook] o.a.s.spring.LifecycleBeanPostProcessor : Destroying bean [securityManager]... 2025-08-28 10:06:28.015 DEBUG 24804 --- [eduler_Worker-5] org.quartz.simpl.SimpleThreadPool : WorkerThread is shut down. 2025-08-28 10:06:28.015 DEBUG 24804 --- [eduler_Worker-3] org.quartz.simpl.SimpleThreadPool : WorkerThread is shut down. 2025-08-28 10:06:28.015 DEBUG 24804 --- [eduler_Worker-7] org.quartz.simpl.SimpleThreadPool : WorkerThread is shut down.
08-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值