使用 BeanPostProcessor 在 Spring 中自定义注解 [英] Custom Annotation in Spring with BeanPostProcessor

查看:51
本文介绍了使用 BeanPostProcessor 在 Spring 中自定义注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试为 Spring 中的 rest api 创建自定义注释.我是创建自定义注释的新手,我提供了下面的代码片段

We are trying to create a Custom Annotation for our rest api in Spring. I am new to creating custom annotation, I have given the code snippet below

Spring Boot 应用程序 --

Spring Boot App --

@SpringBootApplication
@ComponentScan(basePackageClasses = {ServiceController.class, CustomAnnotatorProcessor.class})
public class ServiceApp {
    public static void main(String[] args) {            
        SpringApplication.run(ServiceApp.class, args);
    }
}

RestController --

RestController --

@RestController
public class ServiceController {

    @RequestMapping(method = RequestMethod.GET, value="/service/v1/version")
    @ApiResponses(value = { 
            @ApiResponse(code = 200, message = "Success", response = String.class),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure")}) 
    @CustomAnnotation()
    public String getVersion() {
        return "success";
    }
}

自定义注解 --

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface CustomAnnotation {

}

注解处理器 --

@Component
public class CustomAnnotatorProcessor implements BeanPostProcessor {    

private ConfigurableListableBeanFactory configurableBeanFactory;

@Autowired
public CustomAnnotatorProcessor(ConfigurableListableBeanFactory beanFactory) {
    this.configurableBeanFactory = beanFactory;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    MethodCallback methodCallback = new CustomAnnotationMethodCallback(configurableBeanFactory, bean);
    ReflectionUtils.doWithMethods(bean.getClass(), methodCallback);
    return bean;
}

方法回调--

public class CustomAnnotationMethodCallback implements MethodCallback{
    @Override
    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
        if (method.isAnnotationPresent(CustomAnnotation.class)) {
            System.out.println("doWith is getting called for CustomAnnotationMethodCallback");
            ReflectionUtils.makeAccessible(method);     
            //DO VALIDATION WHETHER A SPECIFIC HEADER IS PRESENT IN THE GIVEN REQUEST
            return;
        }       
    }

}   

我正在尝试在实现 BeanPostProcessor 的类中处理自定义注释,但我遇到了问题

I am trying to process the custom annotation in a class which implements BeanPostProcessor but I have an issue

Issue_1:回调被调用一次,但我无法对传入/service/v1/version API 的每个请求应用验证.我需要验证每个请求,如果是,我们的设计/方法是否正确,如何解决这个问题,如果不是,请提出不同的方法

Issue_1: The callback is getting called once but I am not able apply the validation for every request that is coming to the /service/v1/version API. I need to validate on every request, is our design / approach correct if so how to solve this problem, if not please suggest a different approach

Issue_2:如果我需要将完整的请求对象(单独带有标头)传递给我的 @customAnnotation,我应该怎么做?

Issue_2: If I need to pass the complete request object (alone with the header) to my @customAnnotation, how should I do that?

如果您需要更多详细信息,请告诉我

Please let me know if you need further details

谢谢

推荐答案

Issue_1: 回调被调用一次,但我无法对进入 /service/v1/version API.我需要验证每个请求,如果是,我们的设计/方法是否正确,如何解决这个问题,如果不是,请提出不同的方法

Issue_1: The callback is getting called once but I am not able apply the validation for every request that is coming to the /service/v1/version API. I need to validate on every request, is our design / approach correct if so how to solve this problem, if not please suggest a different approach

Issue_2:如果我需要将完整的请求对象(单独带有标头)传递给我的 @customAnnotation,我应该怎么做?

Issue_2: If I need to pass the complete request object (alone with the header) to my @customAnnotation, how should I do that?

我认为你应该使用 Spring AOP 或 Interceptor 而不是 Annotation.

I think you should use spring AOP or Interceptor other than Annotation.

这篇关于使用 BeanPostProcessor 在 Spring 中自定义注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆