配置文件有四個
web.xml(在WEB-INF目錄下)、
jci-servlet(在WEB-INF目錄下,jci是你的項目名字)、
applicationContext.xml(在src目錄下)、
jdbc.properties(在src目錄下)
—————–web.xml—————————
<?xml version="1.0" encoding="UTF-8"?><!-- 具體配置可參考《Spring.3.x企業應用開發實戰》書的第15章 -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>jci</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 1處從此開始 。1處是 業務層和持久層的Spring配置文件,這些文件被父容器所使用-->
<!-- 從類路徑下加載Spring配置文件,classpath關鍵字特指類路徑下加載 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 負責啓動Spring容器的監聽器,他將引用上面的上下文參數獲得Spring配置文件的地址 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- /1處到此截止 -->
<!-- SpringMVC的主控servlet -->
<servlet><!-- 此處聲明DispatcherServlet(即前置控制器) -->
<servlet-name>jci</servlet-name><!-- 此處配置了名爲jci的DispatcherServlet,
它默認自動加載WEB-INF/jci-servlet.xml 下的spring配置文件,啓動web層的Spring容器-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- SpringMVC處理的URL -->
<servlet-mapping><!-- servlet-mapping指定了DispatcherServlet處理的所有URL都是url-pattern裏的類型,
如以html爲後綴的HTTP請求都會被DispatcherServlet截獲-->
<servlet-name>jci</servlet-name>
<url-pattern>/</url-pattern><!-- <url-pattern>*.html</url-pattern> -->
</servlet-mapping>
</web-app>
—————-jci-servlet———————————
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 掃描 base-package裏的所有類,讓標註spring 註解的類生效
base-package裏的類是需要掃描的ctrl層的名字,因爲ctrl層就是處理前端action的類 -->
<context:component-scan base-package="com.ctrl,com.sys.ctrl" />
<!-- 視圖解析器,把視圖邏輯名解析爲/JSP/xxx.jsp的ModelAndView視圖對象,
如你的ctrl層只需寫上mav.setViewName("login");那麼此處就會自動解析爲返回/JSP/login.jsp頁面-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/JSP/" p:suffix=".jsp" />
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler />
<!-- 實現SpringMVC的註解驅動 -->
<mvc:annotation-driven/>
<!-- 用於文件上傳 設置最大支持200M-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8"
p:maxUploadSize="209715200"/><!-- 單位是字節 -->
</beans>
—————–applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
default-autowire="byName">
<context:annotation-config />
<!-- 掃描類包以啓動註解驅動的Bean -->
<context:component-scan base-package="com.biz,com.ctrl,com.dao,com.po,com.sys"/>
<!-- 引入屬性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 1、數據源,使用C3P0數據源實現 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:initialPoolSize="1"
p:maxPoolSize="50"
p:maxIdleTime="3600"
p:idleConnectionTestPeriod="3600"/>
<!-- 2、配置SessionFactory、映射信息、Hibernate屬性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="com.po,com.sys.po">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
</bean>
<!-- 3、基於數據源的事務管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- 4、事務的驅動註解,使程序能根據@Transactional來生成事務代理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
———————jdbc.properties———————————–
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.1.30.207:3306/jci?characterEncoding=utf8
jdbc.username=guest
jdbc.password=guest