wangchao 2013-05-23
在上一篇文章中主要说了一下需要导入的jar包,此篇文章来讲解具体的配置。
三、具体的配置文件的配置
1、首先配置一下web.xml。应该是个很主要的方面
a、欢迎界面的配置
<!-- 配置默认欢迎界面 --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
b、log4j文件位置的配置,以前可能都没配置过这个东西,因为默认情况下log4j.properties文件需要放在src目录下,即所谓的classpath:目录。下面还有个配置具体的意思不是很懂,一下配置一下就行了。还有个spring对log4j的支持。具体的配置如下所示
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:/edu/qdlms/logic/resources/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!-- 为了防止出现log4j异常 --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener>>
c、spring的配置,spring默认的文件路径是src目录下,看着有点不舒服,所以也把他放到resources包中。下面的listener必须配
<!-- 配置spring的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/edu/qdlms/logic/resources/applicationContext-common.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
d、struts的配置
<!-- struts 的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,classpath:/edu/qdlms/logic/resources/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
e、其它错误文件的配置
<error-page> <error-code>404</error-code> <location>/page/exception/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/page/exception/500.jsp</location> </error-page>
f、这里配置的比较少,下面把整个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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>QDLMS</display-name> <!-- 配置默认欢迎界面 --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:/edu/qdlms/logic/resources/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!-- 为了防止出现log4j异常 --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- 配置spring的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/edu/qdlms/logic/resources/applicationContext-common.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts 的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,classpath:/edu/qdlms/logic/resources/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <error-page> <error-code>404</error-code> <location>/page/exception/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/page/exception/500.jsp</location> </error-page> </web-app>
2、log4j.properties文件的配置,下面直接贴出代码,具体不懂得参考百度或者谷歌吧。
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n ### direct messages to file hibernate.log ### #log4j.appender.file=org.apache.log4j.FileAppender #log4j.appender.file.File=hibernate.log #log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout #log4j.logger.org.hibernate=info #log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL #log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### #log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=debug ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### log cache activity ### #log4j.logger.org.hibernate.cache=debug ### log transaction activity #log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
3、jdbc.properties文件的配置
a、hibernate的配置
#Hibernate hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
b、数据库连接池(c3p0)的配置
#JDBC jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/qdlmsl jdbc.username=root jdbc.password=admin #C3P0 #Hibernate4 C3P0 jdbc.miniPoolSize = 1 jdbc.maxPoolSize = 20 jdbc.initialPoolSize = 1 jdbc.maxIdleTime = 25000 jdbc.acquireIncrement = 1 jdbc.acquireRetryAttempts = 30 jdbc.acquireRetryDelay = 1000 jdbc.testConnectionOnCheckin = true jdbc.automaticTestTable = c3p0TestTable jdbc.idleConnectionTestPeriod = 18000 jdbc.checkoutTimeout=3000
c、整个jdbc.properties文件就不贴出来了
3、struts.xml文件就不用配置了,这里是用的时struts的零配置。所以大家就不用那么麻烦的去配置struts的配置文件了。其中我也在官方下载了一个原版的struts.xml的文件,这里没进行配置。struts的零配置并不是说一点都不需要配置,还有一点配置如下所示(全部注释掉了,回头如果有用到的地方,大家可以百度一下)
<?xml version="1.0" encoding="UTF-8" ?> <!-- /* * $Id: struts-plugin.xml 722219 2008-12-01 20:41:26Z musachy $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- <constant name="struts.convention.action.suffix" value="Controller"/> <constant name="struts.convention.action.mapAllMatches" value="true"/> <constant name="struts.convention.default.parent.package" value="rest-default"/> <constant name="struts.convention.package.locators" value="action"/> --> </struts>
4、 下面最重要的配置就是下面applicationContext-common.xml的配置了,一不小心就会出很多的问题
a、添加注解的支持,还有配置所需要扫描的文件包
<!-- post-processors for all standard config annotations --> <context:annotation-config /> <!-- Scans within the base package of the application for @Components to configure as beans --> <context:component-scan base-package="edu.qdlms.logic.*" />
b、jdbc.properties文件位置的配置
<!-- jdbc.properties文件位置的配置 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:/edu/qdlms/logic/resources/jdbc.properties" /> </bean>
c、c3p0连接池配置
<!-- c3p0连接池配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minPoolSize" value="${jdbc.miniPoolSize}" /> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> <property name="acquireIncrement" value="${jdbc.acquireIncrement}" /> <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" /> <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}" /> <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" /> <property name="automaticTestTable" value="${jdbc.automaticTestTable}" /> <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" /> <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" /> </bean>
d、配置SessionFactory
<!-- 配置SessionFactory,据说这个packagesToScan不能扫描子包 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>edu.qdlms.logic.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean>
e、transactionManager的配置
<!-- transactionManager --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!--采用注解来管理Bean --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* edu.qdlms.logic..service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>
f、最后把整个applicationContext-common.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:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:tool="http://www.springframework.org/schema/tool" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd" default-lazy-init="true" default-autowire="byName"> <!-- post-processors for all standard config annotations --> <context:annotation-config /> <!-- Scans within the base package of the application for @Components to configure as beans --> <context:component-scan base-package="edu.qdlms.logic.*" /> <!-- 配置数据库的信息 --> <!-- jdbc.properties文件位置的配置 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:/edu/qdlms/logic/resources/jdbc.properties" /> </bean> <!-- c3p0连接池配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minPoolSize" value="${jdbc.miniPoolSize}" /> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> <property name="acquireIncrement" value="${jdbc.acquireIncrement}" /> <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" /> <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}" /> <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" /> <property name="automaticTestTable" value="${jdbc.automaticTestTable}" /> <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" /> <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" /> </bean> <!-- 配置SessionFactory,据说这个packagesToScan不能扫描子包 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>edu.qdlms.logic.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean> <!-- transactionManager --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!--采用注解来管理Bean --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* edu.qdlms.logic..service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
-- 创建了一个 bean 名称自动代理,它描述如何应用拦截器。还有其他的方法定义拦截点,但是这种方法常见而简便。-- 将 Struts 动作注册为将被拦截的 bean。如果您想要拦截其他的 Stru