Spring MVC的配置文件

一:對於單個實體時,需要配置個bean.xml文件,詳細配置改實體在dao,service的信息,然後再交給spring容器管理。此時需要的配置文件有如下:


其中spring-beans.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="com.zhd.daoImpl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<bean id="userServiceBase" class="com.zhd.service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 此處爲代理 -->
<bean name="userService" parent="transactionProxy">
<property name="target" ref="userServiceBase"></property>
</bean>

</beans>

配置有:UserDaoImpl的全名類,給他定義了個名字id=“userDao”,UserServiceImpl的全名類,他是在userDao的基礎上,同時還有代理對象transactionProxy,他配置再spring-common.xml中!

spring-common.xml的配置內容有:

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

<!-- 配置數據源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property value="com.mysql.jdbc.Driver" name="driverClassName"/>
<property value="jdbc:mysql://localhost:3306/test" name="url"/>
<property value="root" name="username"/>
<property value="root" name="password"/>
</bean>
<!-- 配置SessionFactory -->
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.zhd.entity.User</value>
</list>
</property>
</bean>
<!-- 配置一個事務管理器 -->
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事務,使用代理的方式 -->
<bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id="transactionProxy" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>

spring-servlet.xml的配置內容有:

<!-- 註解掃描包 --> 
<context:component-scan base-package="com.zhd" /> 
<!-- 開啓註解 --> 
 <mvc:annotation-driven />  
<!-- 靜態資源(js/image)的訪問 --> 
<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.xml的配置文件了。

它只需要兩個配置就行了。


spring-config.xml的配置文件有:與一不同之處就兩個地方!

<!-- 掃描註解Bean -->
<context:component-scan base-package="com.zhd">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 配置數據源 -->
<bean id="dataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
   <property name="url" value="jdbc:mysql://localhost:3306/test4"></property>
   <property name="username" value="root"></property>
   <property name="password" value="java"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.zhd.entity</value>
</list>
</property>

</bean>
<!-- 配置一個事務管理器 --><!-- 開啓註釋。註釋在servlet.xml中配置着 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事務,使用代理的方式 -->
<bean id="transactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>



spring-servlet.xml的配置文件和一相同!只有,掃描包,開啓註解, 靜態資源(js/image)的訪問,定義視圖解析器 。等

還需要配置:web.xml的文件:有,加載所有的配置文件,配置Spring監聽,配置SpringMVC, 配置字符集 ,配置Session等



<!-- 加載所有的配置文件 --> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>classpath*:config/spring-*.xml</param-value> 
</context-param> 


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


<!-- 配置SpringMVC --> 
<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/spring-mvc.xml</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>springMVC</servlet-name> 
<url-pattern>/</url-pattern> 
</servlet-mapping> 


<!-- 配置字符集 --> 
<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> 


<!-- 配置Session --> 
<filter> 
<filter-name>openSession</filter-name> 
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
</filter> 
<filter-mapping> 
<filter-name>openSession</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 
</web-app> 









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章