ich eine funktionierende Lösung implementiert eine benutzerdefinierte @Interceptor
Annotation im Geiste der Spring @Controller
Anmerkung mit :
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Component
public @interface Interceptor {
String[] pathPatterns() default {};
String[] excludePathPatterns() default {};
}
Diese Anmerkung s hould zu HandlerInterceptor
Typen wie so angewendet werden:
@Interceptor
public class BuildTimestampInterceptor extends HandlerInterceptorAdapter {
private final String buildTimestamp;
public BuildTimestampInterceptor(@Value("${build.timestamp}") String buildTimestamp) {
this.buildTimestamp = buildTimestamp;
}
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
req.setAttribute("buildTimestamp", buildTimestamp);
return true;
}
}
Schließlich ist die Prozessorklasse, InterceptorProcessor
ist eine Feder Bean, die WebMvcConfigurerAdapter
und implementiert BeanPostProcessor
, um für die benutzerdefinierte @Interceptor
Annotationen zu scannen erstreckt und registrieren Bohnen aufweist, die anntation als HandlerInterceptor
s innerhalb der überschrieben addInterceptors
Methode:
@Component
public class InterceptorProcessor extends WebMvcConfigurerAdapter implements BeanPostProcessor {
private final Map<HandlerInterceptor,Interceptor> interceptors = new HashMap<>();
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
scanForInterceptorAnnotation(bean, beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String string) throws BeansException {
return bean;
}
protected void scanForInterceptorAnnotation(Object bean, String beanName) {
Optional<Interceptor> optionalInterceptor = getInterceptorAnnotation(bean.getClass());
if (optionalInterceptor.isPresent() && bean instanceof HandlerInterceptor) {
interceptors.put((HandlerInterceptor) bean, optionalInterceptor.get());
}
}
private Optional<Interceptor> getInterceptorAnnotation(Class cls) {
Annotation[] annotations = cls.getAnnotationsByType(Interceptor.class);
if (hasValue(annotations)) {
return Optional.of((Interceptor) annotations[0]);
}
return Optional.empty();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
interceptors.forEach((HandlerInterceptor key, Interceptor val) -> {
InterceptorRegistration registration = registry.addInterceptor(key);
if (hasValue(val.pathPatterns())) {
registration.addPathPatterns(val.pathPatterns());
}
if (hasValue(val.excludePathPatterns())) {
registration.excludePathPatterns(val.excludePathPatterns());
}
});
}
private static <T> boolean hasValue(T[] array) {
return array != null && array.length > 0;
}
}
Denken sie daran, Ihre Feder Anwendung Scan für diesen Prozessor Bohne haben, um es tatsächlich registrieren Ihre @Interceptor
s.Etwas wie:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"org.my.controller", "org.my.utils.processor"})
public class WebConfig extends WebMvcConfigurerAdapter {...
Können Sie klären? Meinst du Spring MVC Abfangjäger oder AOP Abfangjäger? Was meinst du mit "Verkabelung"? – axtavt
Vielleicht möchten Sie dies sehen http://karthikg.wordpress.com/2009/10/12/athandlerinterceptor-for-spring-mvc/ – ashishjmeshram
Ich weiß, es ist ein paar Jahren, dass Sie diese Frage gestellt haben, aber @Markus Kreusch hat die richtige Antwort für die neue Version von Spring MVC –