基于Spring容器的组装

spring 中application-context.xml所做内容

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

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

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>/WEB-INF/config/jdbc.properties</value>

</list>

</property>

</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<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="autoCommitOnClose" value="true"/>

<property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>

<property name="initialPoolSize" value="${cpool.minPoolSize}"/>

<property name="minPoolSize" value="${cpool.minPoolSize}"/>

<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>

<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>

<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>

<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>

</bean>

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

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="mappingLocations">

<list>

<value>classpath*:/com/jeecms/core/entity/hbm/*.hbm.xml</value>

<value>classpath*:/com/jeecms/cms/entity/main/hbm/*.hbm.xml</value>

<value>classpath*:/com/jeecms/cms/entity/assist/hbm/*.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<value>

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

</value>

</property>

<property name="entityInterceptor">   

<ref local="treeInterceptor"/>

</property>

<property name="cacheProvider">

<ref local="cacheProvider"/>

</property>

<property name="lobHandler">

<ref bean="lobHandler" />

</property>

</bean>

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

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />(使用annotation定义事务)

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

<context:annotation-config/>

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

<context:component-scan base-package="com.xyz.callcenter" />

5、可能还需国际化配置

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

<property name="cacheSeconds" value="-1"/>

<property name="basenames">

<list>

<value>/WEB-INF/languages/jeecms_front/messages</value>

<value>/WEB-INF/languages/jeecms_tpl/messages</value>

<value>/WEB-INF/languages/fck/messages</value>

</list>

</property>

</bean>

web.xml 所做内容

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

如listener、filter 、 servlet

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

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

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

</param-value>

</context-param>

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

<filter>

<filter-name>encoding</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>

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

<filter>

<filter-name>osivFilter</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

</filter>

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

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

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

   <filter> 

<!-- 定义核心filter的名字 -->

<filter-name>struts2</filter-name>

<!-- 定义核心filter的实现类 -->

<filter-class>

org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

</filter>

<!-- FilterDispatcher用来初始化struts2并且处理所有的web请求 -->

<filter-mapping>

<filter-name>struts2</filter-name>

<!--默认拦截以.action结尾的请求  -->

<url-pattern>/*</url-pattern>

</filter-mapping>

相关推荐