没有 XML 的 Java Spring 拦截器 [英] Java Spring Interceptor with no XML

查看:30
本文介绍了没有 XML 的 Java Spring 拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以在不使用 XML 配置的情况下配置 Spring 应用程序 文件,并已承诺使用此方法.但是,我不确定如何声明 HTTP 拦截器 以这种方式.我正在使用 本教程,声明以下内容XML.

I understand that it is possible to configure a Spring application without the use of XML config files, and have commited to this method. I am not sure, however, how to declare HTTP interceptors in this manner. I am using this tutorial, which declares the following 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 注释.

How to do this in Java? There is no @Interceptor annotation.

@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();
    }

}

推荐答案

XML 文件中定义的 Spring bean 移动到配置类(用 @Configuration 标记)) 你需要这样的东西:

To move your Spring beans defined in an XML file to a Configuration class (marked with @Configuration) you would need something like this:

@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 中添加这些...

...then in some propertiesFile.properties on the classpath add these...

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

编辑

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

I see you are getting your props from the Environment, so instead of @Value injection, use the way you have it in your code right now.

这篇关于没有 XML 的 Java Spring 拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆