搭建 SpringMVC 框架

来源:互联网 发布:凯文老师的淘宝店 编辑:程序博客网 时间:2024/06/06 18:30

如果创建一个 Spring 项目

Spring MVC 框架在 Java 的 Web 项目中应该是无人不知的吧,你不会搭建一个 Spring 框架?作为身为一个刚刚学习Java的我都会,如果你不会的话,那可真令人忧伤。

1.在 MyEclipse 创建项目后,可以以选择的方式去配置一个 Spring 项目,这里不在讨论。因为我只用 Eclipse。

2.手动搭建。就是动手。

新建一个 Java Web 项目

1.打开 Eclipse ,在Project Explorer选项卡下面点击右键,选择Web - Dynamic Web Prodect(这一步应该都知道阿!!!)。

newProject.png

2.点击Next。起一个你认为还不错的项目名,注意:命名很重要,把每一次命名都当做给自己孩子起名字一样庄严神圣。

SpringDemo.png

3.没有了,完成。

demoMenu.png

搞到 Spring 框架的 jar 包

无论你用坑蒙拐骗,还是死皮赖脸,只要你搞到 Spring 框架的 jar 包就行。我这里给你个地址,你可以体面的去下载就行了。地址:http://projects.spring.io/spring-framework/
找到适合自己的版本,下载下来保存到合适的位置就可以了,就这么简单。解压后,应该是这样的:

spring4.2.6.png

你看包的命名,你可能就大致明白了这个 jar 包是干嘛的了,接下来就是引入你需要的了。
然后,你要你需要的 jar 包,复制到项目的/WebContent/WEB-INF/lib下,为什么要这么做,下面会说的。

导入 jar 包

记得当年一个学 Java 的朋友抱怨说: Java 每天都在导包,不如 .Net 爽。我现在并不这么认为。
在项目名上,点击右键,Build Path - Configure Bulid Path... - Libraries - Add JARs...,在弹出的框里边找到项目的/WebContent/WEB-INF/lib,这样就看到刚刚你复制过来的 jar 包了。

add-jars.png

配置配置配置

搭建 Spring 框架最重要的步骤应该就是配置了。官网对框架的解释说明如下:

Spring MVC 框架是围绕一个 DispatcherServlet 来设计的,这个 Servlet 会把请求分发给各个处理器,并支持可配置的处理器映射、视图渲染、本地化、时区与主题渲染等,甚至还能支持文件上传。处理器是你的应用中注解了 @Controller 和 @RequestMapping 的类和方法,Spring 为处理器方法提供了极其多样灵活的配置。

所以,首先我们应该在/WebContent/WEB-INF/下新建web.xml文件,接下来在这个文件中配置 DispatcherServlet。

  1. <servlet>
  2. <servlet-name>springMVC</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>springMVC</servlet-name>
  8. <url-pattern>/</url-pattern>
  9. </servlet-mapping>
  10. <context-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>/WEB-INF/applicationContext.xml</param-value>
  13. </context-param>

还可以配置字符编码,默认启动页面什么的,这里不在配置,具体见示例项目:https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/web.xml,因为这里是把 DispatcherServlet 命名为springMVC,并且让它在 Web 项目一启动就加载。接下来我们需要在/WebContent/WEB-INF/目录下创建一个springMVC-servlet.xml的Spring配置文件。Spring官方文档上推荐的默认的文件名是[servlet-name]-servlet.xml文件,这里 servlet-name 叫 springMVC ,因此,我新建了一个springMVC-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  9. http://www.springframework.org/schema/util
  10. http://www.springframework.org/schema/util/spring-util-4.2.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
  15. <!-- 使用默认的注解映射 -->
  16. <mvc:annotation-driven />
  17. <mvc:resources location="/" mapping="/index.html" />
  18. <!-- 自动扫描controller包中的控制器 -->
  19. <context:component-scan base-package="cn.mayongfa.api.controller" />
  20. <context:component-scan base-package="cn.mayongfa.controller" />
  21. <!-- 上传文件拦截,设置最大上传文件大小 30M=30*1024*1024(B)=31457280 bytes -->
  22. <bean id="multipartResolver"
  23. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  24. <property name="maxUploadSize" value="31457280" />
  25. </bean>

具体详见:https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/springMVC-servlet.xml
我们在web.xml文件中定义的contextConfigLocation,指定要装入的 Spring 配置文件,一般文件都命名为applicationContext.xml,这个文件中我们可以进行扫描类包、读取配置文件、数据源管理、AOP配置、缓存以及消息队列等配置,所以,接下来就新建applicationContext.xml文件。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd">
  14. <!-- 将多个配置文件读取到容器中,交给Spring管理 -->
  15. <bean id="propertyConfigurer"
  16. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  17. <property name="locations">
  18. <list>
  19. <value>classpath:global.properties</value>
  20. <value>classpath:jdbc.properties</value>
  21. </list>
  22. </property>
  23. </bean>
  24. <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
  25. <context:component-scan base-package="cn.mayongfa.common" />
  26. <context:component-scan base-package="cn.mayongfa.service" />
  27. <context:component-scan base-package="cn.mayongfa.dao" />
  28. <!--master 配置数据源 -->
  29. <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource"
  30. init-method="init" destroy-method="close">
  31. <property name="driverClassName">
  32. <value>${master.jdbc.driverClassName}</value>
  33. </property>
  34. <property name="url">
  35. <value>${master.jdbc.url}</value>
  36. </property>
  37. <property name="username">
  38. <value>${master.jdbc.username}</value>
  39. </property>
  40. <property name="password">
  41. <value>${master.jdbc.password}</value>
  42. </property>
  43. ...
  44. </bean>
  45. <!--slave 配置数据源 -->
  46. <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource"
  47. init-method="init" destroy-method="close">
  48. ...
  49. </bean>
  50. <bean id="dataSource" class="cn.mayongfa.service.imp.DynamicDataSource">
  51. <property name="targetDataSources">
  52. <map>
  53. <entry key="slave" value-ref="slaveDataSource" />
  54. </map>
  55. </property>
  56. <property name="defaultTargetDataSource" ref="masterDataSource" />
  57. </bean>
  58. <!-- 配置Jdbc模板 -->
  59. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  60. <property name="dataSource" ref="dataSource"></property>
  61. </bean>
  62. <!-- 配置事务管理器 -->
  63. ...
  64. <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
  65. ...

上面只是简单的配置,文件并不完整,具体完整项目示例见GitHub:https://github.com/mafly/SpringDemo

到这里,其实我们已经配置完成了,接下来就是新建我们需要的Package包,来实现不同包来完成不同的事儿的。

新增 Package 包

分层的意义及优缺点我这里不在唠叨,按照正常的分层架构一般都会分为 View 层、Action 层、Service 层、Dao 层,这里我们也是这样做的,下面就开始新建包,.Net 下面是叫类库。

package.png

按照这样的方式新建就可以了,具体的架构如下图:
demoLastMenu.png

到这里,搭建 Spring MVC 框架的工作算是完成了。接下来就是配置具体的数据源、缓存、AOP、JMS 这些东西了。祝你好运!



http://blog.mayongfa.cn/

0 0
原创粉丝点击