【问题标题】:how to add specific logic to the method by custom annotation in Spring boot?如何在 Spring Boot 中通过自定义注解为方法添加特定逻辑?
【发布时间】:2022-11-12 01:58:42
【问题描述】:

我的问题是这个。

我想为spring boot添加自定义注释并将其指定为已声明而不声明特定逻辑。

假设您有以下代码:

@MyCustomAnnotation
@Controller
public class ExController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model){
        return "index";
    }
}

我希望上面的代码执行以下逻辑。

@MyCustomAnnotation
@Controller
public class ExController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model){
        
        //Code added because it has an annotation
        
           model.addAttribute("key","value");  

        //Code added because it has an annotation
        
        return "index";
    }
}

一直在想Reflection等方法,但是想不出正确的方法

有人可以为这个问题提供解决方案或关键字吗?

【问题讨论】:

    标签: java spring-boot annotations inversion-of-control spring-annotations


    【解决方案1】:

    您可以使用 @Component 注解在 Spring boot 中通过自定义注解为方法添加特定逻辑。

    【讨论】:

    • ANISH KUMAR 谢谢你的回答。但是,我的问题不是关于如何创建 Bean,而是如果添加了注解,如何在不进行任何额外处理的情况下为方法添加逻辑。
    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2012-02-25
    • 2021-09-25
    • 1970-01-01
    • 2020-03-21
    • 2011-02-13
    相关资源
    最近更新 更多