标签:
采用junit test方式
1.配置 simple-test.xml
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean id="requestMappingHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> </beans>
2.Junit Test Case
package com.xxx.msa.test; import java.util.Map; import java.util.Map.Entry; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; public class TestUrl { @Autowired private static RequestMappingHandlerAdapter handlerAdapter; @Autowired private static RequestMappingHandlerMapping handlerMapping; @BeforeClass public static void before(){ ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{ "classpath*:config/spring/spring-main.xml", "classpath*:config/spring/springmvc/springMVC.xml" ,"classpath*:config/spring/simple-test.xml"}); handlerAdapter=(RequestMappingHandlerAdapter) ctx.getBean("requestMappingHandlerAdapter"); handlerMapping=(RequestMappingHandlerMapping) ctx.getBean("requestMappingHandlerMapping"); } @Test public void testUrls(){ Map<RequestMappingInfo, HandlerMethod> handlerMethods =handlerMapping .getHandlerMethods(); for (Entry<RequestMappingInfo, HandlerMethod> item : handlerMethods .entrySet()) { RequestMappingInfo mapping = item.getKey(); HandlerMethod method = item.getValue(); for (String urlPattern : mapping.getPatternsCondition() .getPatterns()) { System.out.println(method.getBeanType().getName() + "#" + method.getMethod().getName() + " <-- " + urlPattern); if (urlPattern.equals("some specific url")) { // add to list of matching METHODS } } } } }
3.结果
标签:
原文地址:http://www.cnblogs.com/qq-757617012/p/5411921.html