2016-08-25 3 views
2

우리는 Spring의 나머지 API를 위해 커스텀 어노테이션을 작성하려고합니다.BeanPostProcessor로 Spring의 커스텀 어노테이션

@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 공용 클래스 CustomAnnotatorProcessor는 {

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; 
} 

방법 콜백 BeanPostProcessor를 구현 -

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 : 콜백은 한 번 호출되지만 모든 요청에 ​​대해 유효성 검사를 적용 할 수 없습니다. hat이/service/v1/version API에 있습니다. 나는 혼자 (전체 요청 개체를 전달해야하는 경우가 아닌 다른 접근

Issue_2을 제안 해주십시오 경우,이 문제를 해결하는 방법 그렇다면 내가 모든 요청에 ​​확인해야 할 우리의 디자인/방법 올 헤더)를 내 @customAnnotation에 추가하려면 어떻게해야합니까?

사용자 정의 주석 @validateAuthentication을 처리하기 위해

감사

답변

0

을 추가 정보를 필요로하는 경우 알려 주시기 바랍니다, 우리는 HandlerInterceptorAdapter을 확장 Intercepter 클래스를 만들었습니다.

preHandle (HttpServletRequest 요청, HttpServletResponse 응답, 개체 처리기) 메서드에 요청 헤더 유효성 검사를 구현했습니다.

0

Issue_1 : 콜백이 한 번 호출되었지만 /service/v1/version API로 오는 모든 요청에 ​​대해 유효성 검사를 적용 할 수 없습니다. 나는 혼자 (전체 요청 개체를 전달해야하는 경우가 아닌 다른 접근

Issue_2을 제안 해주십시오 경우,이 문제를 해결하는 방법 그렇다면 내가 모든 요청에 ​​확인해야 할 우리의 디자인/방법 올 헤더)를 @customAnnotation으로 변경하려면 어떻게해야합니까?

Annotation 이외의 스프링 AOP 또는 인터셉터를 사용해야한다고 생각합니다.

관련 문제