没有XML的Java Spring Interceptor

时间:2016-11-07 19:33:43

标签: java xml spring spring-mvc

我知道可以配置Spring应用程序without the use of XML config files,并且已经提交了这个方法。但是,我不确定如何以这种方式声明HTTP interceptors。我正在使用this tutorial,它声明了以下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>

如何在Java中执行此操作?没有@Interceptor注释。

SpringApplication.java

@SuppressWarnings("WeakerAccess")
@SpringBootApplication
@PropertySources(value = {@PropertySource("classpath:/application.properties")})

public class SpringbackendApplication {

    @Autowired
    Environment env;

    public static void main(String[] args) {
        SpringApplication.run(SpringbackendApplication.class, args);
        initializeFirebase();
    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Bean
    public UserController userController() {
        UserController userController = new UserController(getUserDAO(), getYodleeDAO());
        userController.setCobrandSession(cobrandSession());
        userController.setUserSessionManager(userSessionManager());
        userController.setAccountsService(accountsService());
        userController.setTransactionsService(transactionsService());
        return userController;
    }

    @Bean
    public TestController testController() {
        TestController testController = new TestController();
        testController.setCobrandSession(cobrandSession());
        testController.setUserSessionManager(userSessionManager());
        testController.setAccountsService(accountsService());
        testController.setTransactionsService(transactionsService());
        return testController;
    }

    @Bean
    public CobrandSession cobrandSession() {
        CobrandSession cobrandSession = new CobrandSession();
        cobrandSession.setApiBase(this.env.getProperty("API_BASE"));
        cobrandSession.setLogin(this.env.getProperty("LOGIN"));
        cobrandSession.setPassword(this.env.getProperty("PASSWORD"));
        cobrandSession.setLocale(this.env.getProperty("LOCALE"));
        cobrandSession.setRestTemplate(restTemplate());
        cobrandSession.setGson(gson());
        return cobrandSession;
    }

    @Bean
    public AccountsService accountsService() {
        AccountsService accountsService = new AccountsService();
        accountsService.setApiBase(this.env.getProperty("API_BASE"));
        accountsService.setRestTemplate(restTemplate());
        accountsService.setGson(gson());
        return accountsService;
    }

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setOutputStreaming(false); // If we don't turn this off, we may get HttpRetryException on 401's.
        return new RestTemplate(factory);
    }

    @Bean
    public Gson gson() {
        return new Gson();
    }

}

2 个答案:

答案 0 :(得分:4)

要将XML文件中定义的Spring bean移动到Configuration类(标有@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;
    }
}

...然后在类路径的某些propertiesFile.properties添加这些......

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

修改

我看到你正在从Environment获取你的道具,所以不要使用@Value注入,而是立即使用你在代码中拥有它的方式。

答案 1 :(得分:1)

您必须覆盖WebMvcConfigurerAdapter.addInterceptors()方法并添加拦截器:

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

不要忘记使用@Configuration

标记您的课程