Spring java配置在java类JAVA中重写

时间:2016-05-12 08:44:13

标签: java xml spring

如何在我的应用程序中使用的Java .class @Bean文件中重写此xml配置。

<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>

2 个答案:

答案 0 :(得分:0)

Pyvisa

答案 1 :(得分:0)

在使用之前,在您班级中自动拦截您的拦截器。

@Autowired
private MaintenanceInterceptor maintenanceInterceptor;

@Autowired
private ExecuteTimeInterceptor executeTimeInterceptor;

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping(){
    SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();

    //Add your mappings
    Properties mappings = new Properties();
    mappings.setProperty("/welcome.htm", "welcome.htm");
    simpleUrlHandlerMapping.setMappings(mappings);

    //Add your interceptors
    List<HandlerInterceptorAdapter> interceptors = new ArrayList<>();
    interceptors.add(maintenanceInterceptor);
    interceptors.add(executeTimeInterceptor);
    simpleUrlHandlerMapping.setInterceptors(interceptors.toArray());

    return simpleUrlHandlerMapping;
}