【问题标题】:Custom Annotation in Spring with BeanPostProcessor使用 BeanPostProcessor 在 Spring 中自定义注解
【发布时间】:2016-08-25 12:00:01
【问题描述】:

我们正在尝试在 Spring 中为我们的 rest api 创建一个自定义注释。我是创建自定义注释的新手,我在下面给出了代码 sn-p

Spring Boot 应用程序 --

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

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 的类中处理自定义注释,但我遇到了问题

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

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

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

谢谢

【问题讨论】:

    标签: spring-boot annotations


    【解决方案1】:

    为了处理自定义注解@validateAuthentication,我们创建了继承HandlerInterceptorAdapter的Intercepter类。

    我们在 preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 方法中实现对请求标头的验证。

    【讨论】:

      【解决方案2】:

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

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

      我认为你应该使用除了 Annotation 之外的 Spring AOP 或 Interceptor。

      【讨论】:

        猜你喜欢
        • 2013-02-24
        • 2013-05-24
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-13
        相关资源
        最近更新 更多