不含XML的Java Spring拦截器

不含XML的Java Spring拦截器,java,xml,spring,spring-mvc,Java,Xml,Spring,Spring Mvc,我知道可以配置Spring应用程序,并已承诺使用此方法。不过,我不知道如何以这种方式申报。我使用的是,它声明了以下XML <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springfra

我知道可以配置Spring应用程序,并已承诺使用此方法。不过,我不知道如何以这种方式申报。我使用的是,它声明了以下XML

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/welcome.htm">welcomeController</prop>
            </props>
        </property>
        <property name="interceptors">
            <list>
                <ref bean="maintenanceInterceptor" />
                <ref bean="executeTimeInterceptor" />
            </list>
        </property>
    </bean>

    <bean
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="executeTimeInterceptor" />
            </list>
        </property>
    </bean>

    <bean id="welcomeController"
                  class="com.mkyong.common.controller.WelcomeController" />
    <bean class="com.mkyong.common.controller.MaintenanceController" />

    <bean id="executeTimeInterceptor"
                 class="com.mkyong.common.interceptor.ExecuteTimeInterceptor" />

    <bean id="maintenanceInterceptor"
                class="com.mkyong.common.interceptor.MaintenanceInterceptor">
        <property name="maintenanceStartTime" value="23" />
        <property name="maintenanceEndTime" value="24" />
        <property name="maintenanceMapping" value="/SpringMVC/maintenance.htm" />
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

您必须重写
WebMVCConfigureAdapter.addInterceptors()
方法并添加拦截器:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new CustomInterceptor());
} 

不要忘记用
@Configuration

标记您的类要将
XML
文件中定义的SpringBean移动到配置类(用
@Configuration
标记),您需要这样的东西:

@Configuration
public class MyConfig {
    @Bean(name="executeTimeInterceptor")
    public ExecuteTimeInterceptor getExecuteTimeInterceptor() {
        return new com.mkyong.common.interceptor.ExecuteTimeInterceptor();
    }

    @Bean(name="maintenanceInterceptor")
    public MaintenanceInterceptor getMaintenanceInterceptor(@Value("${properties.maintenanceStartTime}") int maintenanceStartTime,
                                                            @Value("${properties.maintenanceEndTime}") int maintenanceEndTime,
                                                            @Value("${properties.maintenanceMapping}") String maintenanceMapping) {

        MaintenanceInterceptor myInt = new MaintenanceInterceptor();
        myInt.setMaintenanceStartTime(maintenanceStartTime);
        myInt.setmMaintenanceEndTime(maintenanceEndTime);
        myInt.setMaintenanceMapping(maintenanceMapping);
        return myInt;
    }
}
…然后在类路径上的一些
属性file.properties
中添加这些

properties.maintenanceStartTime=23
properties.maintenanceEndTime=24
properties.maintenanceMapping=/SpringMVC/maintenance.htm
编辑


我看到您正在从
环境
获取道具,因此,不要使用
@Value
注入,而是使用您现在在代码中使用的方式。

我当前有一个应用程序类,我在其中声明了我所有的
@Bean
。我想这就是我要重写的类,你能粘贴你的代码吗?您正在使用spring boot吗?发布我的主类您可以在应用程序类上添加
@EnableAutoConfiguration
,然后创建另一个扩展
WebMVCConfigureAdapter
的类。然后重写它的
addInterceptors()
方法将扩展
WebMVCConfigureAdapter
确保运行
addInterceptors()
,或者在标记主类(使用
@SpringBootApplication
时,我必须从任何地方调用它吗?它应该:)使用类似于
@EnableAutoConfiguration
或类似的注释(来自配置“家族”),您可以告诉spring搜索配置类-它们使用例如
@configuration
注释进行注释。您可以使用
@ComponentScan
来判断应该根据配置类扫描哪些包。请参阅
properties.maintenanceStartTime=23
properties.maintenanceEndTime=24
properties.maintenanceMapping=/SpringMVC/maintenance.htm