搭建 SpringMVC 框架

 

如果创建一个 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. <!-- 使用默认的注解映射 -->
  17. <mvc:annotation-driven />
  18. <mvc:resources location="/" mapping="/index.html" />
  19.  
  20.  
  21. <!-- 自动扫描controller包中的控制器 -->
  22. <context:component-scan base-package="cn.mayongfa.api.controller" />
  23. <context:component-scan base-package="cn.mayongfa.controller" />
  24.  
  25. <!-- 上传文件拦截,设置最大上传文件大小 30M=30*1024*1024(B)=31457280 bytes -->
  26. <bean id="multipartResolver"
  27. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  28. <property name="maxUploadSize" value="31457280" />
  29. </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.  
  15. <!-- 将多个配置文件读取到容器中,交给Spring管理 -->
  16. <bean id="propertyConfigurer"
  17. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  18. <property name="locations">
  19. <list>
  20. <value>classpath:global.properties</value>
  21. <value>classpath:jdbc.properties</value>
  22. </list>
  23. </property>
  24. </bean>
  25.  
  26. <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
  27. <context:component-scan base-package="cn.mayongfa.common" />
  28. <context:component-scan base-package="cn.mayongfa.service" />
  29. <context:component-scan base-package="cn.mayongfa.dao" />
  30.  
  31. <!--master 配置数据源 -->
  32. <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource"
  33. init-method="init" destroy-method="close">
  34. <property name="driverClassName">
  35. <value>${master.jdbc.driverClassName}</value>
  36. </property>
  37. <property name="url">
  38. <value>${master.jdbc.url}</value>
  39. </property>
  40. <property name="username">
  41. <value>${master.jdbc.username}</value>
  42. </property>
  43. <property name="password">
  44. <value>${master.jdbc.password}</value>
  45. </property>
  46.  
  47. ...
  48. </bean>
  49.  
  50. <!--slave 配置数据源 -->
  51. <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource"
  52. init-method="init" destroy-method="close">
  53.  
  54. ...
  55. </bean>
  56.  
  57. <bean id="dataSource" class="cn.mayongfa.service.imp.DynamicDataSource">
  58. <property name="targetDataSources">
  59. <map>
  60. <entry key="slave" value-ref="slaveDataSource" />
  61. </map>
  62. </property>
  63. <property name="defaultTargetDataSource" ref="masterDataSource" />
  64. </bean>
  65.  
  66. <!-- 配置Jdbc模板 -->
  67. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  68. <property name="dataSource" ref="dataSource"></property>
  69. </bean>
  70.  
  71. <!-- 配置事务管理器 -->
  72. ...
  73.  
  74. <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
  75. ...

 

上面只是简单的配置,文件并不完整,具体完整项目示例见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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值