---
title: "基于Spring容器的组装"
author: "ljl.java"
date: "2013-04-03T15:05:44Z"
tags: []
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2013-04-11T16:33:25Z"
id: 1841461
url: "https://ljl-java.iteye.com/blog/1841461"
---

spring 中application-context.xml所做内容

1、Spring 作为容器框架连接数据库使用 hibernate 而hibernate连接交与Spring 管理 装载组建如下

使用com.mchange.v2.c3p0.ComboPooledDataSource实例 (统称c3p0)

&lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;

&lt;property name="locations"&gt;

&lt;list&gt;

&lt;value&gt;/WEB-INF/config/jdbc.properties&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;

&lt;property name="driverClass" value="${jdbc.driverClassName}" /&gt;

&lt;property name="jdbcUrl" value="${jdbc.url}" /&gt;

&lt;property name="user" value="${jdbc.username}" /&gt;

&lt;property name="password" value="${jdbc.password}" /&gt;

&lt;property name="autoCommitOnClose" value="true"/&gt;

&lt;property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/&gt;

&lt;property name="initialPoolSize" value="${cpool.minPoolSize}"/&gt;

&lt;property name="minPoolSize" value="${cpool.minPoolSize}"/&gt;

&lt;property name="maxPoolSize" value="${cpool.maxPoolSize}"/&gt;

&lt;property name="maxIdleTime" value="${cpool.maxIdleTime}"/&gt;

&lt;property name="acquireIncrement" value="${cpool.acquireIncrement}"/&gt;

&lt;property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/&gt;

&lt;/bean&gt;

2 、hibernate对数据库的操作注入dataSource 使用org.springframework.orm.hibernate3.LocalSessionFactoryBean实例

&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;

&lt;property name="dataSource" ref="dataSource"/&gt;

&lt;property name="mappingLocations"&gt;

&lt;list&gt;

&lt;value&gt;classpath\*:/com/jeecms/core/entity/hbm/\*.hbm.xml&lt;/value&gt;

&lt;value&gt;classpath\*:/com/jeecms/cms/entity/main/hbm/\*.hbm.xml&lt;/value&gt;

&lt;value&gt;classpath\*:/com/jeecms/cms/entity/assist/hbm/\*.hbm.xml&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;property name="hibernateProperties"&gt;

&lt;value&gt;

hibernate.dialect=org.hibernate.dialect.Oracle9Dialect

hibernate.show\_sql=false

hibernate.format\_sql=false

hibernate.query.substitutions=true 1, false 0

hibernate.jdbc.batch\_size=20

hibernate.cache.use\_query\_cache=true

&lt;/value&gt;

&lt;/property&gt;

&lt;property name="entityInterceptor"&gt;   

&lt;ref local="treeInterceptor"/&gt;

&lt;/property&gt;

&lt;property name="cacheProvider"&gt;

&lt;ref local="cacheProvider"/&gt;

&lt;/property&gt;

&lt;property name="lobHandler"&gt;

&lt;ref bean="lobHandler" /&gt;

&lt;/property&gt;

&lt;/bean&gt;

3、hibernate操作数据库事务管理 注入sessionFactroy 使用org.springframework.orm.hibernate3.HibernateTransactionManager实例

&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;

&lt;property name="sessionFactory" ref="sessionFactory" /&gt;

&lt;/bean&gt;

&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;(使用annotation定义事务)

4、如何代码需要注解引导还需

&lt;context:annotation-config/&gt;

 &lt;!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 组件扫描策略--&gt;

&lt;context:component-scan base-package="com.xyz.callcenter" /&gt;

5、可能还需国际化配置

&lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;

&lt;property name="cacheSeconds" value="-1"/&gt;

&lt;property name="basenames"&gt;

&lt;list&gt;

&lt;value&gt;/WEB-INF/languages/jeecms\_front/messages&lt;/value&gt;

&lt;value&gt;/WEB-INF/languages/jeecms\_tpl/messages&lt;/value&gt;

&lt;value&gt;/WEB-INF/languages/fck/messages&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;/bean&gt;

 

web.xml 所做内容

web.xml作为项目的起始容器主要能使配置的框架运作起来

如listener、filter 、 servlet

1、所需加载spring的xml文件(Spring ApplicationContext配置文件的路径供用于后面的Spring Context Loader)

&lt;context-param&gt;

&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

&lt;param-value&gt;

/WEB-INF/config/application-context.xml

&lt;/param-value&gt;

&lt;/context-param&gt;

2、项目中文转码问题过滤

&lt;filter&gt;

&lt;filter-name&gt;encoding&lt;/filter-name&gt;

&lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;

&lt;init-param&gt;

&lt;param-name&gt;encoding&lt;/param-name&gt;

&lt;param-value&gt;UTF-8&lt;/param-value&gt;

&lt;/init-param&gt;

&lt;/filter&gt;

3、hibernate懒加载配置(这里使用spinrg提供的jar 另外两种分别是代码设置，和hibernate 文件属性设置lazy="false" )

&lt;filter&gt;

&lt;filter-name&gt;osivFilter&lt;/filter-name&gt;

&lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;

&lt;/filter&gt;

4、启动Spring配置信息(这里主要读取配置的spring的xml)

&lt;listener&gt;

&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;

&lt;/listener&gt;

5、如果web框架使用struts则在配置

   &lt;filter&gt; 

&lt;!-- 定义核心filter的名字 --&gt;

&lt;filter-name&gt;struts2&lt;/filter-name&gt;

&lt;!-- 定义核心filter的实现类 --&gt;

&lt;filter-class&gt;

org.apache.struts2.dispatcher.FilterDispatcher

&lt;/filter-class&gt;

&lt;/filter&gt;

 

&lt;!-- FilterDispatcher用来初始化struts2并且处理所有的web请求 --&gt;

&lt;filter-mapping&gt;

&lt;filter-name&gt;struts2&lt;/filter-name&gt;

&lt;!--默认拦截以.action结尾的请求  --&gt;

&lt;url-pattern&gt;/\*&lt;/url-pattern&gt;

&lt;/filter-mapping&gt;