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

搭建一个springMVC架构的应用程序

2013-03-06 00:00 267 查看

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>wuliuJXC</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<!-- 在web.xml启动时加载spring的总转发器(DispatcherServlet) servlet,拦截所有以liu结尾的请求 -->
<servlet>
<servlet-name>wuliu</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>wuliu</servlet-name>
<url-pattern>*.liu</url-pattern>
</servlet-mapping>

<!-- 使用spring提供的字符编码 filter 设置为utf-8 -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 使用spring的监听 加载指定位置的日志文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>

wuliu-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置试图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

<!-- 引入ApplicationContext.xml文件-->
<import resource="classpath:conf/ApplicationContext.xml"/>

</beans>



ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 自动扫描所有路径 扫描到以后 自动转化为相应的bean -->
<context:component-scan base-package="com.*" />

<!-- 加载指定位置的jdbc文件 引入到本文件中 方便于其它bean的引用 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:conf/jdbc.properties"></property>
</bean>

<!-- 通过加载进来的jdbc文件 配置数据库连接信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="100" />
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="5" />
<property name="maxIdleTime" value="30"/>
</bean>

<!-- 整合Hibernate的配置文件 -->
<import resource="classpath:conf/hibernate-Context.xml"/>

<!-- 指定spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 指定spring的事务拦截器 -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" >
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="register*">PROPAGATION_REQUIRED</prop>
<prop key="cancel*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="operate*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="count*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="export*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*ServerImpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<!-- 定义SESSION拦截器 -->
<bean id="authorizationInterceptor" class="com.wuliu.util.LoginIntercepter">
<property name="mappingURLList">
<list>
<value>login.jsp</value>
<value>login.liu</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="authorizationInterceptor" />
</list>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>

hibernate-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 通过spring加载hibernate的 sessionfactory 工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 引用 ApplicationContext.xml中的dataSource -->
<property name="dataSource" ref="dataSource"/>

<!-- hibernate 的属性配置信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.jdbc.fetch_size">25</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.connection.pool_size">5</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>

<!--
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">conf/ehcache.xml</prop>

<prop key="hibernate.cache.use_query_cache">true</prop>

<prop key="current_session_context_class">thread</prop>
-->
</props>
</property>

<!-- 加载hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>com/wuliu/model/Company.hbm.xml</value> <!-- 公司信息映射 -->
<value>com/wuliu/model/Department.hbm.xml</value> <!-- 部门信息类型映射 -->
<value>com/wuliu/model/Worker.hbm.xml</value> <!-- 工作人员信息映射 -->
<value>com/wuliu/model/Goods.hbm.xml</value> <!-- 货物信息映射 -->
<value>com/wuliu/model/People.hbm.xml</value> <!-- 发货人员信息映射 -->
<value>com/wuliu/model/Consignee.hbm.xml</value> <!-- 收货人员信息映射 -->
<value>com/wuliu/model/Car.hbm.xml</value> <!-- 汽车信息映射 -->
<value>com/wuliu/model/Driver.hbm.xml</value> <!-- 驾驶员信息映射 -->
</list>
</property>

</bean>
</beans>



jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wuliu
jdbc.username=root
jdbc.password=root



log4j.properties

log4j.rootLogger=INFO, A1, R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %-5p [%t] - %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.threshold=DEBUG
log4j.appender.R.File=../webapps/log/medical.log

log4j.appender.R.DatePattern='.'yyyy-MM-dd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} %p %t %c - %m%n
# add by sunhl 2011-12-06 增加缓存刷新机制
log4j.appender.monitorAppender.BufferedIO=true
log4j.appender.monitorAppender.BufferSize=8192
#view all about sql
log4j.logger.java.sql=DEBUG
#view sql
log4j.logger.java.sql.Connection=DEBUG

#view sql parameters
log4j.logger.java.sql.PreparedStatement=DEBUG

#view sql result
log4j.logger.java.sql.ResultSet=DEBUG

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