spring - 如何使用自定义注释中的参数调用注释?
问题描述
如何使用触发另一个注释(例如@Bean(parameterFromCustomAnnotation))的参数创建@CustomAnnotation?
这甚至可能吗?
@Bean # How to add a parameter from the MyBean annotation to @Bean?
@Transactional(rollbackFor = Exception.class, timeout = 5)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyBean{}
解决方案
在下面找到示例:
第一步:创建您的自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DummyService
{
APIServiceName serviceName() ;
}
第二步:创建参数
public enum APIServiceName
{
ADD_USER,
GET_USER_BY_ID
}
第三步:为 API 使用您的自定义注释
@DummyService(serviceName = APIServiceName.ADD_USER)
@RequestMapping(value = /addUSer, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody dummyResponse addUserToDB(@RequestBody String userReq, HttpServletRequest request) {
logic goes here
}
推荐阅读
- google-sheets - 将相同的公式应用于每个单元格
- r - 如何使用 R 在特定目录中通过循环根据列表编写几个数据帧?
- reactjs - 材质 UI - 多个 makeStyles 或一个
- c# - Restsharp json 到 Datagridview
- r - 在一个单元格中的多个可分离(连接)值上加入 R 数据框
- angular - 我无法在 Angular 10 中使用 api 发布数据
- flutter - Dart - 如何在外部应用程序中打开文件
- node.js - 使用 NodeJS 中的 Promise 从两个不同的 SQL 查询将两个对象传递给 EJS 视图
- java - JAXB - XJC - 将 XML 加密模式映射到 Java 类
- swiftui - TabView 导致预览崩溃