查看“Spring框架内容”的源代码
←
Spring框架内容
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
=== SpringMVC简介 === ==== Spring Web MVC是什么 ==== Spring Web MVC 是一种基于 Java 的实现了 Web MVC 设计模式的请求驱动类型的轻量级 Web 框架,即使用了 MVC 架构模式的思想,将 web 层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC 也是要简化我们日常 Web 开发的。在 传统的 Jsp/Servlet 技术体系中,如果要开发接口,一个接口对应一个 Servlet,会导致我们开发出许多 Servlet,使用 SpringMVC 可以有效的简化这一步骤。<br /> Spring Web MVC 也是服务到工作者模式的实现,但进行可优化。前端控制器是 DispatcherServlet;应用控制器可以拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为 Controller 接口(仅包含 ModelAndView handleRequest(request, response) 方法,也有人称作 Handler)的实现(也可以是任何的 POJO 类);支持本地化(Locale)解析、主题(Theme)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了强大的约定大于配置(惯例优先原则)的契约式编程支持。 ==== Spring Web MVC能帮我们做什么==== *让我们能非常简单的设计出干净的 Web 层和薄薄的 Web 层; *进行更简洁的 Web 层的开发; *天生与 Spring 框架集成(如 IoC 容器、AOP 等); *提供强大的约定大于配置的契约式编程支持; *能简单的进行 Web 层的单元测试; *支持灵活的 URL 到页面控制器的映射; *非常容易与其他视图技术集成,如 Velocity、FreeMarker 等等,因为模型数据不放在特定的 API 里,而是放在一个 Model 里(Map 数据结构实现,因此很容易被其他框架使用); *非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的 API; *提供一套强大的 JSP 标签库,简化 JSP 开发; *支持灵活的本地化、主题等解析; *更加简单的异常处理; *对静态资源的支持; *支持 RESTful 风格 === HelloWorld === 通过一个简单的例子来感受一下SpringMVC。 # 利用Maven创建一个web工程,在pom.xml文件中,添加spring-webmvc的依赖。<syntaxhighlight lang="xml"> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency> </dependencies> </syntaxhighlight>添加了 spring-webmvc 依赖之后,其他的 spring-web、spring-aop、spring-context 等等就全部都加入进来了。 # 准备一个 Controller,即一个处理浏览器请求的接口。<syntaxhighlight lang="java"> public class MyController implements Controller { /** * 这就是一个请求处理接口 * @param req 这就是前端发送来的请求 * @param resp 这就是服务端给前端的响应 * @return 返回值是一个 ModelAndView,Model 相当于是我们的数据模型,View 是我们的视图 * @throws Exception */ public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { ModelAndView mv = new ModelAndView("hello"); mv.addObject("name", "javaboy"); return mv; } } </syntaxhighlight>这里我们我们创建出来的 Controller 就是前端请求处理接口。 # 创建视图,这里我们就采用 jsp 作为视图,在 webapp 目录下创建 hello.jsp 文件,内容如下:<syntaxhighlight lang="jsp"> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>hello ${name}!</h1> </body> </html> </syntaxhighlight> # 在 resources 目录下,创建一个名为 spring-servlet.xml 的 springmvc 的配置文件,这里,我们先写一个简单的 demo ,因此可以先不用添加 spring 的配置。<syntaxhighlight lang="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 class="org.javaboy.helloworld.MyController" name="/hello"/> <!--这个是处理器映射器,这种方式,请求地址其实就是一个 Bean 的名字,然后根据这个 bean 的名字查找对应的处理器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" id="handlerMapping"> <property name="beanName" value="/hello"/> </bean> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" id="handlerAdapter"/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans> </syntaxhighlight> # 加载 springmvc 配置文件,在 web 项目启动时,加载 springmvc 配置文件,这个配置是在 web.xml 中完成的。<syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> </syntaxhighlight>所有请求都将自动拦截下来,拦截下来后,请求交给 DispatcherServlet 去处理,在加载 DispatcherServlet 时,还需要指定配置文件路径。这里有一个默认的规则,如果配置文件放在 webapp/WEB-INF/ 目录下,并且配置文件的名字等于 DispatcherServlet 的名字+ <code>-servlet</code>(即这里的配置文件路径是 webapp/WEB-INF/springmvc-servlet.xml),如果是这样的话,可以不用添加 init-param 参数,即不用手动配置 springmvc 的配置文件,框架会自动加载。 # 配置并启动项目。 # 项目启动成功后,浏览器输入 <nowiki>http://localhost:8080/hello</nowiki> 就可以看到如下页面: #[[文件:Hello.png]] === SpringMVC工作流程 === [[文件:Spring.png|左|736x736像素]] 微服务<br /> Microservice architectures are the ‘new normal’. <br /> 微服务架构正在成为事实标准<br /> Building small, self-contained, ready to run applications can bring great flexibility and added resilience to your code. <br /> 创建小的,自包含的,随时可运行的应用程序能够带来极大的灵活性,还能使代码有更强的适应性。<br /> Spring Boot’s many purpose-built features make it easy to build and run your microservices in production at scale. <br /> Spring Boot针对应用的多种自动配置方式,使得其在一定生产范围内变得容易构建和执行。<br /> And don’t forget, no microservice architecture is complete without Spring Cloud ‒ easing administration and boosting your fault-tolerance.<br /> 别忘了,没有集成到 Spring Cloud 的微服务框架是不完全的,Spring Cloud可以更容易的对应用进行监控及容错。<br /> What are microservices?<br /> 什么是微服务<br /> Microservices are a modern approach to software whereby application code is delivered in small, manageable pieces, independent of others.<br /> 微服务是这样一种软件,体量小、可控、相对独立。<br /> Why build microservices?<br /> 为什么创建微服务<br /> Their small scale and relative isolation can lead to many additional benefits, such as easier maintenance, improved productivity, greater fault tolerance, better business alignment, and more. *Reactive Reactive systems have certain characteristics that make them ideal for low-latency, high-throughput workloads. Project Reactor and the Spring portfolio work together to enable developers to build enterprise-grade reactive systems that are responsive, resilient, elastic, and message-driven. What is reactive processing? Reactive processing is a paradigm that enables developers build non-blocking, asynchronous applications that can handle back-pressure (flow control). Why use reactive processing? Reactive systems better utilize modern processors. Also, the inclusion of back-pressure in reactive programming ensures better resilience between decoupled components. *Cloud Developing distributed systems can be challenging. Complexity is moved from the application layer to the network layer and demands greater interaction between services. Making your code ‘cloud-native’ means dealing with 12-factor issues such as external configuration, statelessness, logging, and connecting to backing services. The Spring Cloud suite of projects contains many of the services you need to make your applications run in the cloud. *Web applications Spring makes building web applications fast and hassle-free. By removing much of the boilerplate code and configuration associated with web development, you get a modern web programming model that streamlines the development of server-side HTML applications, REST APIs, and bidirectional, event-based systems. *Serverless Serverless applications take advantage of modern cloud computing capabilities and abstractions to let you focus on logic rather than on infrastructure. In a serverless environment, you can concentrate on writing application code while the underlying platform takes care of scaling, runtimes, resource allocation, security, and other “server” specifics. What is serverless? Serverless workloads are “event-driven workloads that aren’t concerned with aspects normally handled by server infrastructure.” Concerns like “how many instances to run” and “what operating system to use” are all managed by a Function as a Service platform (or FaaS), leaving developers free to focus on business logic. Serverless characteristics? Serverless applications have a number of specific characteristics, including: Event-driven code execution with triggers Platform handles all the starting, stopping, and scaling chores Scales to zero, with low to no cost when idle Stateless *Event Driven Event-driven systems reflect how modern businesses actually work–thousands of small changes happening all day, every day. Spring’s ability to handle events and enable developers to build applications around them, means your apps will stay in sync with your business. Spring has a number of event-driven options to choose from, from integration and streaming all the way to cloud functions and data flows. Event-driven microservices When combined with microservices, event streaming opens up exciting opportunities—event-driven architecture being one common example. Spring simplifies the production, processing, and consumption of events, providing several useful abstractions. Streaming data Streaming data represents a constant flow of events. One example might be a stock ticker. Every time a stock price changes, a new event is created. It’s called “streaming data” because there are thousands of these events resulting in a constant stream of data. Integration The bedrock of any event-driven system is message handling. Connecting to message platforms, routing messages, transforming messages, processing messages. With Spring you can solve these integration challenges quickly. *Batch The ability of batch processing to efficiently process large amounts of data makes it ideal for many use cases. Spring Batch’s implementation of industry-standard processing patterns lets you build robust batch jobs on the JVM. Adding Spring Boot and other components from the Spring portfolio lets you build mission-critical batch applications. What is batch processing? Batch processing is the processing of a finite amount of data in a manner that does not require external interaction or interruption. Why build batch processes? Batch processes are an extremely efficient way of processing large amounts of data. The ability to schedule and prioritize work based on SLAs lets you allocate resources for best utilization.
返回至“
Spring框架内容
”。
导航菜单
个人工具
登录
名字空间
页面
讨论
变体
视图
阅读
查看源代码
查看历史
更多
搜索
导航
-==扬==-
-==帆==-
-==起==-
-==航==-
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息