您的位置:首页 > 编程语言 > Java开发

Spring MVC 配置总结

2015-05-14 14:21 120 查看
学习Java Web也有一小段时间了,中间走了好多的弯路。今天总结一下这段时间的收获。

一开始我学习Web编程是从PHP开始的,PHP在一开始需要的配置不是很多(也有可能是我工作的那家公司已经配置好了框)。而且在编程的时候,只需要一种PHP脚本语言就足够了。无论是在MVC的哪一层。

但是Java Web编程一上来就是大量的配置,对于一个新手来说,很容易让人摸不到头绪。之前并没有接触过Java开发,对于Java语言本身,SSH架构都是完全的不了解。一开始就直接接触Spring MVC压力有点大。不过好在学校有个非常好的高手朋友对我进行指导,减轻了不少压力,也让我能够快速的认识到自己的不足和错误。

今天主要总结一下子Spring MVC在配置上的一些规范和技巧,以便以后的查阅和,也是对现有知识的巩固。

webxml

spring-mvcxml

application-contextxml

web.xml

<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<!-- 配置文件标签启动顺序 context-param -> listerner -> fileter -> servlet -->

<!--定义了WEB应用的名字-->
<display-name>SpringMVCApplication</display-name>

<!-- 程序的初始进入页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- SpirngMVC 配置 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/templates/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- <listener-class>org. springframework.web.util.IntrospectorCleanupListener</listener-class> -->
</listener>

<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/application-context.xml</param-value>
</context-param>

<!-- 配置字符集 -->
<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>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置Spring的Log4jConfigListener -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>


Spring MVC的入口是DispatcherServlet,所以在配置web.xml的时候一定首先配置。这里的Spring MVC的配置文件的默认路径是WEB-INF目录下,默认名称为[servlet name]-servlet.xml,比如我们的servlet名字为spring-mvc,那么配置文件的名字就应该为spring-mvc-servlet.xml。我们还可以自定义Spring MVC配置文件的路径和名字,通过
<init-param>


<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-mvc.xml</param-value>
</init-param>


这样我们就将Spring MVC的配置文件命名为spring-mvc.xml,并定位于/WEB-INF/config目录下。

<load-on-startup>1</load-on-startup>
的意义是在启动时就加载Spring MVC的Servlet。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/application-context.xml</param-value>
</context-param>


这一段代码,指定了Spring 配置文件的位置。

下面我们来分别总结一下Spring MVC和Spring的配置。

spring-mvc.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
<!-- 注解扫描包 -->
<context:component-scan base-package="com.controller" />
<!-- 开启注解 -->
<mvc:annotation-driven/>

<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 上传文件相关的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
</bean>
</beans>


application-context.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
<!-- 需要Spring注解扫描的类包 -->
<context:component-scan base-package="com" />

<!-- 加载JDBC配置文件 -->
<context:property-placeholder location="/WEB-INF/config/jdbc.properties" />

<!-- 连接数据库参数 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

<!-- 通过dataSource获取sessionFactory、获取实体、 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
</props>
</property>
<property name="packagesToScan" value="com.entity" />

</bean>

<!-- 声明事务 -->
<bean name="transactionAdvice" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置Spring事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager<
bdd0
/span>="transactionAdvice">
<tx:attributes>
<tx:method name="import*" propagation="REQUIRED" />
<tx:method name="init*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 装配HibernateTemplate实例 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<constructor-arg ref="sessionFactory" />
</bean>

<!-- 配置哪些类的哪些方法参与事务 -->
<aop:config>
<aop:advisor pointcut="execution(* com..*.*(..))"
advice-ref="txAdvice" />
</aop:config>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: