面url访问到后台程序是从一个url开始的,怎么根据url找到对应的处理程序,这个就要从RequestMapping原理理解。
SpringMVC 启动的时候,会加载 加了注解 @Controller 的 Bean.
栗子:
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = {"/show"})
public String testTest() {
return "/jsp/index";
}
}
@Controller和@RequestMapping结合起来完成了Spring MVC请求的派发流程。
简述:new 出来的,带有父类 AbstractHandlerMethodMapping 属性 mappingRegistry , mappingRegistry key value 中的 value 指的是 带有 requestMapping 注解的方法 。
如果想要 RequestMapping 注解生效,必须在 xml 文件中配置,< mvc:annotation-driven/>。
配置完 xml 之后 ,下一步解析 bean。
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
//如果该元素属于默认命名空间走此逻辑。Spring的默认namespace为:http://www.springframework.org/schema/beans“
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
//对document中的每个元素都判断其所属命名空间,然后走相应的解析逻辑
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
//如果该元素属于自定义namespace走此逻辑 ,比如AOP,MVC等。
delegate.parseCustomElement(root);
}
}
beans 等默认命名空间执行 parseDefaultElement() 方法,其他命名空间执行 parseCustomElement() 方法.
public BeanDefinition parseCustomElement(Element ele) {
return parseCustomElement(ele, null);
}
进入parseCustomElement(ele, null)方法。
public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
//获取该元素namespace url
String namespaceUri = getNamespaceURI(ele);
//得到NamespaceHandlerSupport实现类解析元素
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
if (handler == null) {
error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
return null;
}
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}
进入NamespaceHandlerSupport类的parse()方法。
public BeanDefinition parse(Element element, ParserContext parserContext) {
//此处得到AnnotationDrivenBeanDefinitionParser类来解析该元素
return findParserForElement(element, parserContext).parse(element, parserContext);
}
获取解析类
private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) {
String localName = parserContext.getDelegate().getLocalName(element);
BeanDefinitionParser parser = this.parsers.get(localName);
if (parser == null) {
parserContext.getReaderContext().fatal(
"Cannot locate BeanDefinitionParser for element [" + localName + "]", element);
}
return parser;
}
解析< mvc:annotation-driven/>元素 进入AnnotationDrivenBeanDefinitionParser类的parse()方法。
注解解析器处理
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
XmlReaderContext readerContext = parserContext.getReaderContext();
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
parserContext.pushContainingComponent(compDefinition);
RuntimeBeanReference contentNegotiationManager = getContentNegotiationManager(element, source, parserContext);
//生成RequestMappingHandlerMapping bean信息
RootBeanDefinition handlerMappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
handlerMappingDef.setSource(source);
handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
handlerMappingDef.getPropertyValues().add("order", 0);
handlerMappingDef.getPropertyValues().add("contentNegotiationManager", contentNegotiationManager);
......
//此处HANDLER_MAPPING_BEAN_NAME值为:RequestMappingHandlerMapping类名
//容器中注册name为RequestMappingHandlerMapping类名
parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, HANDLER_MAPPING_BEAN_NAME));
......
}
Spring 容器中注册了一个名为 “HANDLER_MAPPING_BEAN_NAME”,类型为 RequestMappingHandlerMapping.
RequestMappingHandlerMapping 实例化
RequestMappingHandlerMapping 继承图
RequestMappingHandlerMapping实现了 HandlerMapping 接口,同时还实现了 ApplicationContectAware 和 IntialingBean 接口。
这个接口只包含以下方法:
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
如果一个类实现了 ApplicationContextAware 接口,Spring容器在初始化该类时候会自动回调该类的setApplicationContext()方法。这个接口主要用来让实现类得到Spring 容器上下文信息。
这个接口包含下面方法:
void afterPropertiesSet() throws Exception;
如果一个bean实现了该接口,Spring 容器初始化bean时会回调afterPropertiesSet()方法。这个接口的主要作用是让bean在初始化时可以实现一些自定义的操作。
RequestMappingHandlerMapping 实现了 ApplicationContextAware 接口 , 那么实例化后会执行 setApplicationContext 方法
@Override
public final void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
if (context == null && !isContextRequired()) {
// Reset internal context state.
this.applicationContext = null;
this.messageSourceAccessor = null;
}
else if (this.applicationContext == null) {
// Initialize with passed-in context.
if (!requiredContextClass().isInstance(context)) {
throw new ApplicationContextException(
"Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
}
this.applicationContext = context;
this.messageSourceAccessor = new MessageSourceAccessor(context);
initApplicationContext(context);
}
else {
// Ignore reinitialization if same context passed in.
if (this.applicationContext != context) {
throw new ApplicationContextException(
"Cannot reinitialize with different application context: current one is [" +
this.applicationContext + "], passed-in one is [" + context + "]");
}
}
}
这个方法把容器上下文赋值给 applicationContext 变量,赋值的就是 spring mvc 容器。
RequestMappingHandlerMapping 实现了 InitializingBean 接口。设置完属性后回调 afterpropertiesSet 方法.
public void afterPropertiesSet() {
this.config = new RequestMappingInfo.BuilderConfiguration();
this.config.setTrailingSlashMatch(useTrailingSlashMatch());
this.config.setContentNegotiationManager(getContentNegotiationManager());
if (getPatternParser() != null) {
this.config.setPatternParser(getPatternParser());
Assert.isTrue(!this.useSuffixPatternMatch && !this.useRegisteredSuffixPatternMatch,
"Suffix pattern matching not supported with PathPatternParser.");
}
else {
this.config.setSuffixPatternMatch(useSuffixPatternMatch());
this.config.setRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch());
this.config.setPathMatcher(getPathMatcher());
}
super.afterPropertiesSet();
}
同时还调用了父类的 afterPropertiesSet 方法
public void afterPropertiesSet() {
//初始化handler函数
initHandlerMethods();
}
继续查看 RequestMappingHandlerMapping.initHandlerMethods 方法
protected void initHandlerMethods() {
if (logger.isDebugEnabled()) {
logger.debug("Looking for request mappings in application context: " + getApplicationContext());
}
//1.获取容器中所有bean 的name。
//根据detectHandlerMethodsInAncestorContexts bool变量的值判断是否获取父容器中的bean,默认为false。因此这里只获取Spring MVC容器中的bean,不去查找父容器
String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class));
//循环遍历bean
for (String beanName : beanNames) {
if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
Class<?> beanType = null;
try {
beanType = getApplicationContext().getType(beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
}
}
//2.判断bean是否含有@Controller或者@RequestMappin注解
if (beanType != null && isHandler(beanType)) {
//3.对含有注解的bean进行处理,获取handler函数信息。
detectHandlerMethods(beanName);
}
}
}
handlerMethodsInitialized(getHandlerMethods());
}
可以看出 RequestMappingHandlerMapping.initHandlerMethods 方法进行了如下操作:
查看 RequestMappingHandlerMapping.isHandle 处理逻辑
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
看对含义 Controller 和 RequestMapping 的怎么处理
查看 RequestMappingHandlerMapping.detectHandlerMethods 方法
protected void detectHandlerMethods(final Object handler) {
//1.获取bean的类信息
Class<?> handlerType = (handler instanceof String ?
getApplicationContext().getType((String) handler) : handler.getClass());
final Class<?> userType = ClassUtils.getUserClass(handlerType);
//2.遍历函数获取有@RequestMapping注解的函数信息
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
new MethodIntrospector.MetadataLookup<T>() {
@Override
public T inspect(Method method) {
try {
//如果有@RequestMapping注解,则获取函数映射信息
return getMappingForMethod(method, userType);
}
catch (Throwable ex) {
throw new IllegalStateException("Invalid mapping on handler class [" +
userType.getName() + "]: " + method, ex);
}
}
});
if (logger.isDebugEnabled()) {
logger.debug(methods.size() + " request handler methods found on " + userType + ": " + methods);
}
//3.遍历映射函数列表,注册handler
for (Map.Entry<Method, T> entry : methods.entrySet()) {
Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType);
T mapping = entry.getValue();
//注册handler函数
registerHandlerMethod(handler, invocableMethod, mapping);
}
};
detectHandlerMethods 主要功能就是获取该bean和父接口中所有用@RequestMapping注解的函数信息,并把这些保存到methodMap变量中。
查看 RequestMappingHandlerMapping.selectMethods 方法实现逻辑
public static <T> Map<Method, T> selectMethods(Class<?> targetType, final MetadataLookup<T> metadataLookup) {
final Map<Method, T> methodMap = new LinkedHashMap<Method, T>();
Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
Class<?> specificHandlerType = null;
//把自身类添加到handlerTypes中
if (!Proxy.isProxyClass(targetType)) {
handlerTypes.add(targetType);
specificHandlerType = targetType;
}
//获取该bean所有的接口,并添加到handlerTypes中
handlerTypes.addAll(Arrays.asList(targetType.getInterfaces()));
/对自己及所有实现接口类进行遍历
for (Class<?> currentHandlerType : handlerTypes) {
final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType);
//获取函数映射信息
ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
//循环获取类中的每个函数,通过回调处理
@Override
public void doWith(Method method) {
//对类中的每个函数进行处理
Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
//回调inspect()方法给个函数生成RequestMappingInfo
T result = metadataLookup.inspect(specificMethod);
if (result != null) {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
if (bridgedMethod == specificMethod || metadataLookup.inspect(bridgedMethod) == null) {
//将生成的RequestMappingInfo保存到methodMap中
methodMap.put(specificMethod, result);
}
}
}
}, ReflectionUtils.USER_DECLARED_METHODS);
}
//返回保存函数映射信息后的methodMap
return methodMap;
}
上面逻辑中doWith()回调了inspect(),inspect()又回调了getMappingForMethod()方法。
我们看看getMappingForMethod()是如何生成函数信息的。
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
//创建函数信息
RequestMappingInfo info = createRequestMappingInfo(method);
if (info != null) {
RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
if (typeInfo != null) {
info = typeInfo.combine(info);
}
}
return info;
}
查看RequestMappingHandlerMapping#createRequestMappingInfo()方法。
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
//如果该函数含有@RequestMapping注解,则根据其注解信息生成RequestMapping实例,
//如果该函数没有@RequestMapping注解则返回空
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
RequestCondition<?> condition = (element instanceof Class ?
getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element));
//如果requestMapping不为空,则生成函数信息MAP后返回
return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null);
}
看看RequestMappingHandlerMapping#createRequestMappingInfo是如何实现的。
protected RequestMappingInfo createRequestMappingInfo(
RequestMapping requestMapping, RequestCondition<?> customCondition) {
return RequestMappingInfo
.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
.methods(requestMapping.method())
.params(requestMapping.params())
.headers(requestMapping.headers())
.consumes(requestMapping.consumes())
.produces(requestMapping.produces())
.mappingName(requestMapping.name())
.customCondition(customCondition)
.options(this.config)
.build();
可以看到上面把RequestMapping注解中的信息都放到一个RequestMappingInfo实例中后返回。当生成含有@RequestMapping注解的函数映射信息后,最后一步是调用registerHandlerMethod 注册handler和处理函数映射关系。
protectedvoid registerHandlerMethod(Object handler, Method method, T mapping) {
this.mappingRegistry.register(mapping, handler, method);
}
看到把所有的handler方法都注册到了mappingRegistry这个变量中。
到此就把RequestMappingHandlerMapping bean的实例化流程就分析完了。
Spring MVC容器初始化流程,查看在 FrameworkServlet#initWebApplicationContext 方法。
protected WebApplicationContext initWebApplicationContext() {
//1.获得rootWebApplicationContext
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
......
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
//2.创建 Spring 容器
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
//3.初始化容器
onRefresh(wac);
}
......
return wac;
}
查看 DispatchServlet#onRefresh 方法
@Override
protected void onRefresh(ApplicationContext context) {
//执行初始化策略
initStrategies(context);
}
查看 initStrategies 方法
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
查看 initHandlerMappings 方法
private void initHandlerMappings(ApplicationContext context) {
this.handlerMappings = null;
if (this.detectAllHandlerMappings) {
// Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
//容器中查找HandlerMapping的实例
Map<String, HandlerMapping> matchingBeans =
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
if (!matchingBeans.isEmpty()) {
//把找到的bean放到hanlderMappings中。
this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
// We keep HandlerMappings in sorted order.
AnnotationAwareOrderComparator.sort(this.handlerMappings);
}
}
else {
try {
HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
this.handlerMappings = Collections.singletonList(hm);
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore, we'll add a default HandlerMapping later.
}
}
// Ensure we have at least one HandlerMapping, by registering
// a default HandlerMapping if no other mappings are found.
if (this.handlerMappings == null) {
this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
if (logger.isDebugEnabled()) {
logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");
}
}
}
最终是 实例化的 RequestMappingHandlerMapping 放到了一个 HandlerMapping 中。
DispatchServlet 继承自Servlet,那所有的请求都会在service()方法中进行处理。
查看service()方法。
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求方法
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
//若是patch请求执行此逻辑
if (HttpMethod.PATCH == httpMethod || httpMethod == null) {
processRequest(request, response);
}
else {
//其它请求走此逻辑
super.service(request, response);
}
}
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (logger.isDebugEnabled()) {
String resumed = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : "";
logger.debug("DispatcherServlet with name '" + getServletName() + "'" + resumed +
" processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]");
}
// Keep a snapshot of the request attributes in case of an include,
// to be able to restore the original attributes after the include.
Map<String, Object> attributesSnapshot = null;
if (WebUtils.isIncludeRequest(request)) {
attributesSnapshot = new HashMap<String, Object>();
Enumeration<?> attrNames = request.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = (String) attrNames.nextElement();
if (this.cleanupAfterInclude || attrName.startsWith("org.springframework.web.servlet")) {
attributesSnapshot.put(attrName, request.getAttribute(attrName));
}
}
}
// Make framework objects available to handlers and view objects.
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());
FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);
if (inputFlashMap != null) {
request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
}
request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);
try {
doDispatch(request, response);
}
finally {
if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Restore the original attribute snapshot, in case of an include.
if (attributesSnapshot != null) {
restoreAttributesAfterInclude(request, attributesSnapshot);
}
}
}
}
得到不管get、post最后都会执行到DispatcherServlet#doDispatch(request, response);
查看 doDispatch 方法
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
}
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Error err) {
triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
查看 mappedHandler = getHandler(processedRequest); 方法
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
//获取HandlerMapping实例
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
//得到处理请求的handler
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
上面遍历handlerMappings获得所有HandlerMapping实例,还记得handlerMappings变量吧,这就是前面initHandlerMappings()方法中设置进去的值,其实就是为了找到 RequestMappingHandlerMapping。
@Override
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Object handler = getHandlerInternal(request);
......
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
......
return executionChain;
}
进入 AbstractHandlerMethodMapping#getHandlerInternal 方法
@Override
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
//获取函数url
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
......
try {
//查找HandlerMethod
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
......
}
finally {
this.mappingRegistry.releaseReadLock();
}
}
进入 lookupHandlerMethod 方法
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
List<Match> matches = new ArrayList<Match>();
List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
......
}
最后从 mappingRegistry 找到对应的处理方法 这个 mappingRegistry 就是来自 实例化后的 RequestMappingHandlerMapping 。
在初始化容器时,会 new 一个RequestMappingHandlerMapping,其父类是AbstractHandlerMethodMapping 。并把这个RequestMappingHandlerMapping 放到一个 HandlerMapping 中。
实例化后带有属性 mappingRegistry , mappingRegistry 的 value 指的是带有 requestMapping 注解的方法。
在服务请求时,遍历 HandlerMapping ,并根据请求信息,从RequestMappingHandlerMapping 中找到对应的处理方法handler
欢迎关注公众号:程序员开发者社区
整项目地址:vue-element-admin
https://github.com/PanJiaChen/vue-element-admin
做这个 vueAdmin-template 的主要原因是: vue-element-admin 这个项目的初衷是一个 vue 的管理后台集成方案,把平时用到的一些组件或者经验分享给大家,同时它也在不断的维护和拓展中,比如最近重构了dashboard,加入了全屏功能,新增了 tabs-view 等等。所以项目会越来越复杂,不太适合很多初用 vue 的同学来构建后台。所以就写了这个基础模板,它没有复杂的功能,只包含了一个后台需要最基础的东西。 vueAdmin-template 主要是基于vue-cli webpack模板为基础开发的,引入了如下dependencies:
该项目只做了一个管理后台需要极简的功能,封装了axios请求,支持无限层级路由,动态权限和动态侧边栏。 如果需要更多复杂的功能可以参考 vue-element-admin,若还有不足,欢迎提issue或者pr。下文会简单说一下用该模板需要注意的地方。
路由懒加载应该是写大一点的项目都会用的一个功能,只有在使用这个component的时候才会加载这个相应的组件,这样写大大减少了初始页面 js 的大小并且能更好的利用浏览器的缓存。
const Foo = resolve => require(['./Foo.vue'], resolve)
//或者
const Foo = () => import('./Foo');
复制代码
在懒加载页面不多的情况下一切是那么的美好,但我司后台业务在不断地迭代,现在项目近百个路由,这时候使用路由懒加载在开发模式下就是一件痛苦的事情了,随手改一行代码热更新都是要6000ms+的,这怎么能忍。楼主整整花了一天多的时间找原因,能webpack优化的方法都用了,什么 dll, HappyPack 等方法都是过了,但提升的效果都不是很明显,正好那段时间出了 webpack3 楼主也升级了,编译速度也得到了很大幅度的提升,不过也要2000ms+。后来经过大神 @jzlxiaohei 的指点发现原来是路由懒加载搞得鬼,楼主猜测可能是异步加载导致 webpack 每次的 cache 失效了,所以每次的rebuild 才会这么的慢。找到了原因我们就可以对症下药了,我们就自己封装了一个_import()的方法,只有在正式环境下才使用懒加载。这样解决了困扰多事的rebuild慢问题。代码
const _import = require('./_import_' + process.env.NODE_ENV);
const Foo = _import('Foo');
复制代码
整整比原来6000ms快了十多倍,我终于又能愉快的开发了。
在手摸手,带你用vue撸后台 系列二(登录权限篇)这章中其实已经详细介绍过了。该项目中权限的实现方式是:通过获取当前用户的权限去比对路由表,生成当前用户具的权限可访问的路由表,通过router.addRoutes动态挂载到router上。 但其实很多公司的业务逻辑可能不是这样的,举一个例子来说,很多公司的需求是每个页面的权限是动态配置的,不像本项目中是写死预设的。但其实原理是相同的。如这个例子,你可以在后台通过一个tree控件或者其它展现形式给每一个页面动态配置权限,之后将这份路由表存储到后端。当用户登录后根据role,后端返回一个相应的路由表或者前端去请求之前存储的路由表动态生成可访问页面,之后就是router.addRoutes动态挂载到router上,你会发现原来是相同的,万变不离其宗。
侧边栏:本项目里的侧边栏是根据 router.js 配置的路由并且根据权限动态生成的,这样就省去了写一遍路由还要再手动写侧边栏这种麻烦事,同是使用了递归组件,这样不管你路由多少级嵌套,都能愉快的显示了。权限验证那里也做了递归的处理。
面包屑:本项目中也封装了一个面包屑导航,它也是通过watch $route动态生成的。代码
由于侧边栏导航和面包屑亦或是权限,你会发现其实都是和router密切相关的,所以基于vue-router路由信息对象上做了一下小小的拓展,自定义了一些属性
icon : the icon show in the sidebar
element-ui自带的图标不是很丰富,但管理后台图标的定制性又很强。这里只给大家推荐使用阿里的 iconfont ,简单好用又方便管理。本项目中已经嵌入了一些 iconfont 作为例子,大家可以自行替换。 这里来简单介绍一下 iconfont 的使用方式。首先注册好 iconfont 账号之后,可以在我的项目中管理自己的 iconfont 。我司所有的项目都是用这个管理的,真心推荐使用。
创建好图标库后如果有更新替换也很方便,这里我使用了 Symbol 的方式引入,这里还有unicode,font-class的引入方式,有兴趣的可以自行研究。 之后我们点击下载 Symbol,会发现有如下这些文件,我们只要关心iconfont.js就可以了
我们将它替换项目中的 iconfont.js 就可以了。本项目中也封装了一个svg component 方便大家使用。
<icon-svg icon-class="填入你需要的iconfont名字就能使用了"></icon-svg>
复制代码
每个项目都需要有一个属于自己的favicon。
其实实现起来非常的方便,我们主需要借助html-webpack-plugin
//webpack config
function resolveApp(relativePath) {
return path.resolve(relativePath);
}
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
favicon: resolveApp('favicon.ico')
}),
复制代码
你只要将本项目跟目录下的favicon.ico文件替换为你想要的图标即可。
vue cli 默认提供了standard和airbnb 两种 lint 规范,说真的一个j检查校验的太松一个又太紧,而且每个团队的 lint 规范又是不同的,所以楼主干脆在项目里把大部分常用的 lint 规范都列举了出来并写上了注释方便大家修改代码地址,大家也可以把自己的规范上传到npm,像 vue 一样 vue-eslint-config。配置 eslint 对多人协作的项目有很大的好处,同时配置好lint 在加 ide 的 lint 插件写代码简直要起飞。相关配置可见第一篇教程。
相信大部分 vue 的项目都是基于 vue-cli 来开发的,不过毕竟每个人需求都是不太一样的,需要自定义一些的东西。就比如拿 postcss 来说 vue-cli 有一个小坑,它默认 autoprefixer 只会对通过 vue-loader 引入的样式有作用,换而言之也就是 .vue 文件里面的 css autoprefixer 才会效果。相关问题issues/544,issues/600。解决方案也很简单粗暴
//app.vue
<style lang="scss">
@import './styles/index.scss'; // 全局自定义的css样式
</style>
复制代码
你在 .vue 文件中引入你要的样式就可以了,或者你可以改变 vue-cli的文件在 css-loader 前面在加一个 postcss-loader,在前面的issue地址中已经给出了解决方案。 这里再来说一下 postcss 的配置问题,新版的vue-cli webpack 模板 inti 之后跟目录下默认有一个.postcssrc.js 。vue-loader 的 postcss 会默认读取这个文件的里的配置项,所以在这里直接改配置文件就可以了。配置和postcss是一样的。
//.postcssrc.js
module.exports = {
"plugins": {
// to edit target browsers: use "browserlist" field in package.json
"autoprefixer": {}
}
}
//package.json
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
复制代码
如上代码所述,autoprefixe r回去读取 package.json 下 browserslist的配置文件
本项目暂时没有兼容性需求,如有兼容性需求可自行使用babel-polyfill。 在Node/Browserify/webpack中使用
npm install --save babel-polyfill //下载依赖
复制代码
在入口文件中引入
import 'babel-polyfill';
// 或者
require('babel-polyfill');//es6
复制代码
在webpack.config.js中加入babel-polyfill到你的入口数组:
module.exports = {
entry:["babel-polyfill","./app/js"]
}
复制代码
具体可参考 link
或者更简单暴力 polyfill.io 使用它给的一个 cdn 地址,引入这段js之后它会自动判断游览器,加载缺少的那部分 polyfill,但国内速度肯能不行,大家可以自己搭 cdn。
楼主 vue 群里的小伙伴们问的最多的问题还是关于跨域的,其实跨域问题真的不是一个很难解决的问题。这里我来简单总结一下我推荐的几种跨域解决方案。
开发环境 生成环境 cors cors proxy nginx
这里我只推荐这两种方式跨域,其它的跨域方式都很多,但真心主流的也就这两种方式。
vue-element-admin 由于是一个纯前端个人项目,所以所以的数据都是用mockjs生成的,它的原理是:拦截了所有的请求并代理到本地模拟数据,所以 network 中没有任何的请求发出。不过这并不符合实际业务开发中的场景,所以这个项目中使用了前不久刚出的 easy-mock,支持跨域,mockjs 的语法,支持Swagger 这几点还是挺不错的。相关文章
线上或者测试环境接口的 base_url 不一样是很长见得需求,或者你在本地用了如 easy-mock 这种模拟数据到线上环境你想用自己公司生产环境的数据,这些需求都可以简单的通过用 baseurl 来解决。首先我们在config/下有dev.env.js和prod.env.js这两个配置文件。用它来区分不同环境的配置参数。
//dev.env.js
module.exports = {
NODE_ENV: '"development"',
BASE_API: '"https://easy-mock.com/mock/5950a2419adc231f356a6636/vue-admin"',
}
//prod.env.js
module.exports = {
NODE_ENV: '"production"',
BASE_API: '"https://prod-xxx"',
}
复制代码
同时本项目封装了axios拦截器,方便大家使用,大家也可根据自己的业务自行修改。
import axios from 'axios';
import { Message } from 'element-ui';
import store from '../store';
// 创建axios实例
const service = axios.create({
baseURL: process.env.BASE_API, // api的base_url 读取config配置文件
timeout: 5000 // 请求超时时间
});
// request拦截器
service.interceptors.request.use(config => {
if (store.getters.token) {
config.headers['X-Token'] = store.getters.token; // 让每个请求携带自定义token 请根据实际情况自行修改
}
return config;
}, error => {
// Do something with request error
console.log(error); // for debug
Promise.reject(error);
})
// respone拦截器
service.interceptors.response.use(
response => {
/**
* code为非20000是抛错 可结合自己业务进行修改
*/
const res = response.data;
if (res.code !== 20000) {
Message({
message: res.data,
type: 'error',
duration: 5 * 1000
});
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('FedLogOut').then(() => {
location.reload();// 为了重新实例化vue-router对象 避免bug
});
})
}
return Promise.reject(error);
} else {
return response.data;
}
},
error => {
console.log('err' + error);// for debug
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
});
return Promise.reject(error);
}
)
export default service;
复制代码
由于axios每一个都是一个实例,你的请求都是基于这个实例来的,所以所以配置的参数属性都继承了下来.
//api.xxx.js
import fetch from '@/utils/fetch';
export function getInfo(token) {
return fetch({
url: '/user/info',
method: 'get',
params: { token }
});
}
//你可以直接这样使用,之前拦截器写的东西都是生效的,
//它自动会有一个你之前配置的baseURL,
//但你说我这个请求baseURL和其它的不同,
//这也是很方便的,你可以字请求内部修改,
//它会自动覆盖你在创建实例时候写的参数如
export function getInfo(token) {
return fetch({
baseURL: https://api2-xxxx.com
url: '/user/info',
method: 'get',
params: { token }
});
}
复制代码
这篇文章主要是介绍了 vueAdmin 做了哪些事情,希望大家如果有后台新项目要开发,建议基于 vue-admin-template 来开发,而 vue-element-admin 更多的是用来当做一个集成方案,你要什么功能就去里面找拿来用,因为两者的基础架构是一样的,所以复用成本也很低。
NET (C#) 中,发送 HTTP GET 和 POST 请求可以通过多种方式实现,主要依赖于 .NET Framework 或 .NET Core/5+ 的版本。.NET中提供了多种方法来发送HTTP请求,每种方法都有其优缺点。选择哪种方法取决于具体需求和环境。
HttpClient 是 .NET 中处理 HTTP 请求的现代方法。它是线程安全的,支持异步操作,并且可以用于多个请求。
适用平台:.NET Framework 4.5+, .NET Standard 1.1+, .NET Core 1.0+
其它平台的移植版本可以通过Nuget来安装。(Nuget使用方法:VS(Visual Studio)中Nuget的使用-CJavaPy)
命名空间:using System.Net.Http;
HttpClient推荐使用单一实例共享使用,发关请求的方法推荐使用异步的方式,代码如下,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace ConsoleApplication
{
class Program
{
private static readonly HttpClient client = new HttpClient();
static void Main(string[] args)
{
//发送Get请求
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
//发送Post请求
var values = new Dictionary
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
}
}
}
2、使用第三方类库
除了上述方法,还有许多第三方库可以用于发送HTTP请求,例如 RestSharp 和 Flurl。这些库通常提供更高级的功能,例如支持多种身份验证方法和自动重试机制。
1)Flurl.Http(可以通过Nuget来安装)
Flurl.Http 是一个在 .NET 环境下使用的流行的 HTTP 客户端库。它提供了一个简洁的 API 来创建 HTTP 请求,并支持异步操作。Flurl.Http 使得发送 HTTP 请求、接收响应、处理异常和解析数据变得非常简单。
命名空间:using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl.Http;
namespace ConsoleApplication
{
class Program
{
public static async Task Main(string[] args)
{
// 示例URL
string getUrl = "https://example.com";
string postUrl = "https://example.com/post";
// 发送GET请求
string htmlContent = await GetHtmlAsync(getUrl);
Console.WriteLine("GET请求结果:");
Console.WriteLine(htmlContent);
// 发送POST请求
var postData = new { Name = "John", Age = 30 };
var postResponse = await PostJsonAsync<object>(postUrl, postData);
Console.WriteLine("POST请求结果:");
Console.WriteLine(postResponse);
// 发送带有查询参数和头信息的GET请求
string htmlContentWithHeaders = await GetHtmlWithHeadersAsync(getUrl);
Console.WriteLine("带查询参数和头信息的GET请求结果:");
Console.WriteLine(htmlContentWithHeaders);
// 安全地发送GET请求
string safeHtmlContent = await SafeRequestAsync(getUrl);
Console.WriteLine("安全GET请求结果:");
Console.WriteLine(safeHtmlContent);
}
public static async Task<string> GetHtmlAsync(string url)
{
return await url.GetStringAsync();
}
public static async Task<TResponse> PostJsonAsync<TResponse >(string url, object data)
{
return await url
.PostJsonAsync(data)
.ReceiveJson<TResponse>();
}
public static async Task<string> GetHtmlWithHeadersAsync(string url)
{
return await url
.SetQueryParams(new { param1 = "value1", param2 = "value2" })
.WithHeader("Accept", "application/json")
.GetStringAsync();
}
public static async Task<string> SafeRequestAsync(string url)
{
try
{
return await url.GetStringAsync();
}
catch (FlurlHttpException ex)
{
return "HTTP Error: " + ex.Message;
}
catch (Exception ex)
{
return "General Error: " + ex.Message;
}
}
}
}
2)RestSharp(可以通过Nuget来安装)
RestSharp 是一个用于在 .NET 中发送 HTTP 请求的简单 REST 和 HTTP API 客户端。它可以用于构建和发送各种 HTTP 请求,并处理响应。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//发送Get和Post请求
RestClient client = new RestClient("http://example.com");//指定请求的url
RestRequest req = new RestRequest("resource/{id}", Method.GET);//指定请求的方式,如果Post则改成Method.POST
request.AddParameter("name", "value"); // 添加参数到 URL querystring
request.AddUrlSegment("id", "123"); //替换上面指定请求方式中的{id}参数
//req.AddBody(body); /*如发送post请求,则用req.AddBody ()指定body内容*/
//发送请求得到请求的内容
//如果有header可以使用下面方法添加
//request.AddHeader("header", "value");
IRestResponse response = client.Execute(request);
//上传一个文件
//request.AddFile("file", path);
var content = response.Content; // 未处理的content是string
//还可以下面几种方式发送请求
//发送请求,反序列化请求结果
IRestResponse response2 = client.Execute(request);
var name = response2.Data.Name;
//发送请求下载一个文件,并保存到path路径
client.DownloadData(request).SaveAs(path);
// 简单发送异步请求
await client.ExecuteAsync(request);
// 发送异常请求并且反序列化结果
var asyncHandle = client.ExecuteAsync(request, response => {
Console.WriteLine(response.Data.Name);
});
//终止异步的请求
asyncHandle.Abort();
}
}
}
较旧的方法,现在通常推荐使用 HttpClient。但在一些旧项目或特定场景下,WebRequest 和 WebResponse 仍然有用。
适用平台:.NET Framework 1.1+, .NET Standard 2.0+, .NET Core 1.0+
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO; // for StreamReader
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//发送Get请求
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//发送Post请求
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var postData = "thing1=hello";
postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
}
}
C#中,WebClient类提供了一个简单的方法来发送和接收数据到和从一个URI(如一个网页或Web服务)上。
适用平台:.NET Framework 1.1+, .NET Standard 2.0+, .NET Core 2.0+
*请认真填写需求信息,我们会在24小时内与您取得联系。