是否可以使用注释连接Spring MVC拦截器,如果是这样,有人可以提供一个如何操作的示例吗?
通过连接通过注释,我指的是尽可能少地在XML配置中做。例如,我在http://www.vaannila.com/spring/spring-interceptors.html;
找到的配置文件中<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="loggerInterceptor" />
<bean id="loggerInterceptor" class="com.vaannila.interceptor.LoggerInterceptor" />
你有多少配置可以逃脱?我想@Autowired
将不再需要在第2行中显式声明bean,但是也可以使用注释来删除第1行吗?
答案 0 :(得分:73)
在搜索这个问题时偶然发现了这个问题。最后我发现它在Spring 3.1中使用@EnableWebMVC和WebMvcConfigurerAdapter一起工作。
简单示例:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="webapp.base.package")
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoggerInterceptor());
}
}
答案 1 :(得分:19)
据我所知,没有办法配置没有XML的Spring MVC拦截器。
但是,在最新版本的Spring 3.0.x(不是Spring 3.0.0中)中,mvc
命名空间有一些简化:
<mvc:interceptors>
<bean class="com.vaannila.interceptor.LoggerInterceptor" />
</mvc:interceptors>
另见:
答案 2 :(得分:3)
我根据Spring wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
注释的精神,使用自定义@Interceptor
注释实现了一个有效的解决方案:
@Controller
此注释应该应用于@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Component
public @interface Interceptor {
String[] pathPatterns() default {};
String[] excludePathPatterns() default {};
}
类型,如:
HandlerInterceptor
最后,处理器类@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;
}
}
是一个扩展InterceptorProcessor
并实现WebMvcConfigurerAdapter
的Spring bean,用于扫描自定义BeanPostProcessor
注释和注册bean在被覆盖的@Interceptor
方法中,HandlerInterceptor
的结果为<{1}}:
addInterceptors
请记住让你的spring应用程序扫描这个处理器bean,以便让它实际注册你的@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;
}
}
。类似的东西:
@Interceptor
答案 3 :(得分:0)
我不知道spring-AOP但是如果你通过Spring使用AspectJ,你可以使用@Aspect,@ Pointcut,@ Advise等......
还有一篇关于如何在Spring AOP中使用这些注释的文章: http://java-x.blogspot.com/2009/07/spring-aop-with-aspecj-annotations.html
答案 4 :(得分:-2)
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="webapp.base.package")
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping RequestMappingHandlerMapping= super.requestMappingHandlerMapping();
Object[] interceptors = new Object[1];
interceptors[0] = new RoleInterceptor();
RequestMappingHandlerMapping.setInterceptors(interceptors);
return RequestMappingHandlerMapping;
}
}