使用BeanPostProcessor在Spring中自定义注释

时间:2016-08-25 12:00:01

标签: spring-boot annotations

我们正在尝试为Spring中的rest api创建一个Custom Annotation。我是创建自定义注释的新手,我已经在下面给出了代码片段

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
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,我应该怎么做?

如果您需要更多详细信息,请与我们联系

由于

2 个答案:

答案 0 :(得分:0)

为了处理自定义注释@validateAuthentication,我们创建了扩展 HandlerInterceptorAdapter 的Intercepter类。

我们在 preHandle(HttpServletRequest请求,HttpServletResponse响应,对象处理程序)方法中实现了对请求标头的验证。

答案 1 :(得分:0)

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

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

我认为你应该使用除注释之外的spring AOP或Interceptor。