个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助。不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想。实践出真知。

1、基本概念


1.1、Spring


Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

1.2、SpringMVC

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

2.环境搭建详解

2.1引入相应的包

springMVC和spring包的结构发生了很大的变化,各个包都分开了,灵活,要求使用者更加深入的学习使用,当我们引入包的时候,以少为原则,少的话可以根据报错来找到相应的包,如果过多的话,包会报错异常的混乱,不容易分辨;sprinMVC和spring本身就是一家的,所以引入的包来说基本上和spring需要的架构包是一致的.

2.2  新建注解xml文件  :springAnnotation-servlet.xml

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.tgb.web.controller.annotation" />
<!-- 开启注解 --> <mvc:annotation-driven/> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
</beans> </span></span></span>

2.3 建springxml文件 springAnnotation-core.xml

通过bean注入要调用的接口实现类

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><beans>
<bean id="springManager" class="com.tgb.web.controller.annotation.SpringManager"></bean>
</beans></span></span></span>

2.3.1  ISpring接口

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public interface ISpring {

    public String get();
}</span></span></span>

2.3.2  ISpring实现类

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public class SpringManager implements ISpring {

	@Override
public String get() {
//判定是否调用了
System.out.println("------I am springManager----"); return "I am getMethod";
} }</span></span></span>

2.3.3  SpringController类

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public class SpringController {
//这样代替了再xml中配置属性的过程
@Resource(name="springManager")
private ISpring springManager;//注入springManager @RequestMapping("/spring/get")
public String get(){
System.out.println(springManager.get());
return "/success";
}
}</span></span></span>

一个项目的全局配置点在web.xml,一个项目需要使用了多少框架,通过xml可以查看.

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springMVC8</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/springAnnotation-core.xml</param-value>
<!-- <param-value>classpath*:config/springAnnotation-servlet.xml</param-value> -->
</context-param> <!-- 配置spring启动listener入口 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springMVC启动DispatcherServlete入口 -->
<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*:config/springAnnotation-core.xml</param-value> -->
<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- encoding filter for jsp page -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app></span></span></span>

注解:springMVC是通过dispastservlet来监听的,spring使用linstener监听的,他们之间的启动顺序,web容器有又一个即,

第一:context-param

第二:Listerer

第三:Filter

第四:servlet

这样的启动顺序是有一定联系的

总结

一张图胜过千言万语哈!

接下来是<SpringMVC+Hibernate+Spring三大架构的集成实例>,请期待学习!

springMVC系列之(三) spring+springMVC集成(annotation方式)的更多相关文章

  1. spring+springMVC集成(annotation方式)

    spring+springMVC集成(annotation方式) SpringMVC+Spring4.0+Hibernate 简单的整合 MyBatis3整合Spring3.SpringMVC3

  2. SpringMVC系列(三):REST风格

    一.什么叫REST REST:即 Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所 ...

  3. SpringMVC学习记录三——8 springmvc和mybatis整合

    8      springmvc和mybatis整合 8.1      需求 使用springmvc和mybatis完成商品列表查询. 8.2      整合思路 springmvc+mybaits的 ...

  4. SpringMVC系列(二): SpringMVC各个注解的使用

    1.@RequestMapping 1.@RequestMapping除了能修饰方法,还能修饰类(1)修饰类:提供初步的请求映射信息,相对于web请求的根目录(2)修饰方法:提供进一步的细分映射信息相 ...

  5. SpringMVC系列(一)SpringMVC概述和搭建SpringMVC的第一个helloWord入门程序

    一.SpringMVC 概述 • Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一 • Spring3.0 后全面超越 Struts2,成为最优秀的 MVC ...

  6. SpringMVC学习(三)———— springmvc的数据校验的实现

    一.什么是数据校验? 这个比较好理解,就是用来验证客户输入的数据是否合法,比如客户登录时,用户名不能为空,或者不能超出指定长度等要求,这就叫做数据校验. 数据校验分为客户端校验和服务端校验 客户端校验 ...

  7. springMVC入门(三)------springMVC的处理器映射器和处理器适配器配置

    简介 springMVC的处理器映射器和处理器适配器存在多种配置,因此在此专门做一个总结 常见处理器映射器.适配器的配置 springmvc多个映射器多个处理器可以并存 所有的映射器都实现了Handl ...

  8. SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池

    三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...

  9. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

随机推荐

  1. [SDOI2008]烧水问题

    题目描述 把总质量为1kg的水分装在n个杯子里,每杯水的质量均为(1/n)kg,初始温度均为0℃.现需要把每一杯水都烧开.我们可以对任意一杯水进行加热.把一杯水的温度升高t℃所需的能量为(4200*t ...

  2. UVALive - 3530:Martian Mining

    dp 可以发现,对于(i,j),要么把它运上去,那么把它运到左边,枚举一下即可 #include<cstdio> #include<cstdlib> #include<a ...

  3. ●BZOJ 2560 串珠子

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2560 题解: 容斥,状压计数dp 首先求出一个数组 g[s] 表示集合内的点的连边方案数(两 ...

  4. BZOJ4942【noi2017】整数

    题目背景 在人类智慧的山巅,有着一台字长为10485761048576 位(此数字与解题无关)的超级计算机,著名理论计算机科 学家P博士正用它进行各种研究.不幸的是,这天台风切断了电力系统,超级计算机 ...

  5. VM11 CentOS6.7 i386 安装 oracle 11g r2

    CentOS 6.7 i386:最小桌面版本--中文1.网络配置 ifup eht0 vim /etc/sysconfig/network-scripts/ifcfg-eth0 修改:NBOOT=ye ...

  6. 作为开发也要了解的 mysql 优化思路

    作为开发人员,数据库知识掌握的可能不是很深入,但是一些基本的技能还是要有时间学习一下的.作为一个数据库菜鸟,厚着脸皮来总结一下 mysql 的基本的不能再基本的优化方法. 为了更好的说明,我假想出来了 ...

  7. 好用的jquery.animateNumber.js数字动画插件

    在做公司的运营报告页面时,有一个数字累计增加的动画效果,一开始,毫无头绪,不知如何下手,于是上网查资料,发现大多都是用的插件来实现的,那么今天,我也来用插件jquery.animateNumber.j ...

  8. idea+jsp+jstl c标签页面异常

    先在Schema and DTDs配置C.tld文件 最后提示是少包 网上很多方法都说少jstl.jar 折腾了很久 其实还少standard.jar 以前的解决方法(看下面) 把这两个包分别加到项目 ...

  9. Chrome浏览器Postman插件安装使用

    最近调试后台接口一直在使用的工具,由于换了新的电脑重新安装了一下PostMan.随便记录一下如何安装使用这个插件. 闲言不要谈,直接上步骤: 1. 首先必须有chrome浏览器,这个相信大家肯定都安装 ...

  10. Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密

    本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1.  摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...