springMVC系列之(三) spring+springMVC集成(annotation方式)

Love The Way You Lie 2022-08-10 15:54 239阅读 0赞
  1. 个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助。不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想。实践出真知。

1、基本概念

1.1、Spring

  1. 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

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

2.环境搭建详解

2.1引入相应的包

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

Center

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

  1. <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  13. <!-- 注解扫描包 -->
  14. <context:component-scan base-package="com.tgb.web.controller.annotation" />
  15. <!-- 开启注解 -->
  16. <mvc:annotation-driven/>
  17. <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  18. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
  19. <!-- 静态资源访问 -->
  20. <mvc:resources location="/img/" mapping="/img/**"/>
  21. <mvc:resources location="/js/" mapping="/js/**"/>
  22. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  23. <property name="prefix" value="/"></property>
  24. <property name="suffix" value=".jsp"></property>
  25. </bean>
  26. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  27. <property name="defaultEncoding" value="utf-8" />
  28. <property name="maxUploadSize" value="10485760000" />
  29. <property name="maxInMemorySize" value="40960" />
  30. </bean>
  31. </beans> </span></span></span>

2.3 建springxml文件 springAnnotation-core.xml

  1. 通过bean注入要调用的接口实现类
  2. <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><beans>
  3. <bean id="springManager" class="com.tgb.web.controller.annotation.SpringManager"></bean>
  4. </beans></span></span></span>

2.3.1 ISpring接口

  1. <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public interface ISpring {
  2. public String get();
  3. }</span></span></span>

2.3.2 ISpring实现类

  1. <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public class SpringManager implements ISpring {
  2. @Override
  3. public String get() {
  4. //判定是否调用了
  5. System.out.println("------I am springManager----");
  6. return "I am getMethod";
  7. }
  8. }</span></span></span>

2.3.3 SpringController类

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

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

  1. <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>springMVC8</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath*:config/springAnnotation-core.xml</param-value>
  15. <!-- <param-value>classpath*:config/springAnnotation-servlet.xml</param-value> -->
  16. </context-param>
  17. <!-- 配置spring启动listener入口 -->
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <!-- 配置springMVC启动DispatcherServlete入口 -->
  22. <servlet>
  23. <servlet-name>springMVC</servlet-name>
  24. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  25. <init-param>
  26. <param-name>contextConfigLocation</param-name>
  27. <!-- <param-value>classpath*:config/springAnnotation-core.xml</param-value> -->
  28. <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
  29. </init-param>
  30. <load-on-startup>1</load-on-startup>
  31. </servlet>
  32. <filter>
  33. <filter-name>encodingFilter</filter-name>
  34. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  35. <init-param>
  36. <param-name>encoding</param-name>
  37. <param-value>UTF-8</param-value>
  38. </init-param>
  39. <init-param>
  40. <param-name>forceEncoding</param-name>
  41. <param-value>true</param-value>
  42. </init-param>
  43. </filter>
  44. <!-- encoding filter for jsp page -->
  45. <filter-mapping>
  46. <filter-name>encodingFilter</filter-name>
  47. <url-pattern>/*</url-pattern>
  48. </filter-mapping>
  49. <servlet-mapping>
  50. <servlet-name>springMVC</servlet-name>
  51. <url-pattern>/</url-pattern>
  52. </servlet-mapping>
  53. </web-app></span></span></span>

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

第一:context-param

第二:Listerer

第三:Filter

第四:servlet

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

Center 1

Center 2

总结

  1. 一张图胜过千言万语哈!

Center 3

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

发表评论

表情:
评论列表 (有 0 条评论,239人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot集成SpringMVC

    有段时间没有写博客,最近入职,准备静下心来好好学习技术,把以前没用的博客删掉,从头开始写博客。本文是介绍springboot如何与springmvc完成合作,而且也只是简单介绍