app.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
default-autowire="byType">
<!-- 配置搜索组件的基础包(父包)将被注解的类的对象纳入springIOC容器(四个service) -->
<context:component-scan base-package="com.lin.dang" />
<!-- 配置注解来配置需要spring IOC 容器托管的对象以及对象之间的依赖关系 -->
<context:annotation-config></context:annotation-config>
<!-- 配置为基于AspectJ的切面自动生成代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置数据库连接池(空间换时间的优化) -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/dang?useUincode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="initialSize" value="10"></property>
<property name="maxTotal" value="50"></property>
<property name="maxWaitMillis" value="15000"></property>
</bean>
<!-- 配置hibernate的会话工厂()持久层需要依赖会话工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.lin.dang.model"></property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 配置spring为hiberntae提供的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置声明事务管理 -->
<tx:annotation-driven />
</beans>
fc-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.lin.dang.controller" />
<!-- 配置MVC的试图解析器 渲染视图 完成模型和视图的绑定 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置验证器 -->
<bean id="globalValidator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<mvc:annotation-driven validator="globalValidator"/>
<!-- 配置处理文件上传的解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1048576"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 进来后寻找index.do -->
<welcome-file-list>
<welcome-file>index.do</welcome-file>
</welcome-file-list>
web.xml
<!-- 通过上下文参数指定Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param>
<!-- 前端控制器 (这里用过滤器来充当) -->
<servlet>
<servlet-name>fc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 所有服务进入过滤器 修改为utf-8 由spring提供此过滤器 -->
<filter>
<filter-name>enc</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>
</filter>
<filter-mapping>
<filter-name>enc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置延长会话周期的配置 将视图渲染出来后才关闭会话 缺点:每个.do的都延长会话周期 特别不精打细算(应用访问量大 高并发的使用此方法不合适)
<filter>
<filter-name>osiv</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osiv</filter-name>
<url-pattern>*.do</url-pattern>(yinw)
</filter-mapping>-->
<!-- 配置创建spring IoC容器的监听器,当服务器已开启,立即启动IOC容器,服务器一关闭立刻关闭IOC容器 -->
<!-- 在spring-web-context -context loader listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
网友评论