java - 如何向方法运行时添加注释?春季AOP
问题描述
我对接口中的注释方法有疑问
public interface CsvImportService {
@Import
void importFile(Long userId, String organizationId, MultipartFile file, String charset) throws Exception;
}
接口的实现
@Service
public class CsvImportServiceImpl implements CsvImportService {
@Override
public void importFile(Long userId, String organizationId, MultipartFile file, String charset) {
...
}
}
我试图通过 Spring AOP 处理它
@Slf4j
@Aspect
@Component
public class ImportAspect {
@AfterReturning(pointcut = "@annotation(com.backend.annotations.Import)")
public void handleImport(JoinPoint joinPoint) throws Throwable {
LOGGER.info("handle");
}
}
但是发现注解不去impl,所以才意识到要在实现方法中添加注解,开始写BPP
@Component
public class ImportAnnotationBeanPostProcessor implements BeanPostProcessor {
Map<String, Class<?>> map = new HashMap<>();
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Class<?> beanClass = bean.getClass();
if (CsvImportService.class.isAssignableFrom(beanClass)) {
map.put(beanName, beanClass);
}
return bean;
}
@SneakyThrows
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class<?> beanClass = map.get(beanName);
if (beanClass != null) {
for (Method declaredMethod : CsvImportService.class.getDeclaredMethods()) {
if (declaredMethod.isAnnotationPresent(Import.class)) {
Method importMethod = beanClass.getDeclaredMethod(declaredMethod.getName(), declaredMethod.getParameterTypes());
//TODO Add annotation to method
}
}
}
return bean;
}
}
并且没有找到给方法加注解的反射方法。我该怎么做?
解决方案
推荐阅读
- python-3.x - 通过python进行网页抓取问题,无法读取html文件?
- json - Golang 将嵌套的 json 解组为结构
- linux - mq_receive 在 NASM 中给出“消息太长”错误
- ios - UIView 在子视图中不可见
- flutter - 尝试在颤振上写相机,麦克风和地图的权限
- string - 朱莉娅最快的方式来读取值字符串到数组
- windows - VTune 中缺少 amplxe-sepreg.exe
- java - 为什么我的计数器不计数并且我的 ArrayList 没有将新的整数值添加到数组中?
- java - 持有具有不同数量参数的函数映射
- python - “Post.user”必须是“用户”实例