简体   繁体   中英

Spring xml configuration rewrite in java class JAVA

How to rewrite this xml configuration in Java .class @Bean file which i am using in my application.

<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 public SimpleUrlHandlerMapping simpleURLHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MAX_VALUE - 2);

Properties urlProperties = new Properties();
urlProperties.put("/welcome.htm", "welcomeController");

mapping.setMappings(urlProperties);
mapping.setInterceptors(new Object[]{ maintenanceInterceptor(), executeTimeInterceptor() });

return mapping; }

Autowire your interceptors in your class before you use it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM