前端之家收集整理的这篇文章主要介绍了
applicationContext.xml配置分析,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> < beans> //读取
配置文件init.properties的信息 <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:init.properties</value> </property> </bean> //数据源的配置(使用了上面init.properties的信息) 试用了c3p0连接池 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"> <property name="driverClass"> <value>${datasource.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${datasource.url}</value> </property> <property name="user"> <value>${datasource.username}</value> </property> <property name="password"> <value>${datasource.password}</value> </property> <property name="acquireIncrement"> <value>${c3p0.acquireIncrement}</value> </property> <property name="initialPoolSize"> <value>${c3p0.initialPoolSize}</value> </property> <property name="minPoolSize"> <value>${c3p0.minPoolSize}</value> </property> <property name="maxPoolSize"> <value>${c3p0.maxPoolSize}</value> </property> <property name="maxIdleTime"> <value>${c3p0.maxIdleTime}</value> </property> <property name="idleConnectionTestPeriod"> <value>${c3p0.idleConnectionTestPeriod}</value> </property> <property name="maxStatements"> <value>${c3p0.maxStatements}</value> </property> <property name="numHelperThreads"> <value>${c3p0.numHelperThreads}</value> </property> </bean> //配置spring的heibernate工厂sessionFactory <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSession
factorybean"> <property name="dataSource"> <ref local="dataSource" /> </property> 配置读取所有
数据库表格的
配置文件 <property name="mappingResources"> <list> <value>com/laoer/bbscs/bean/UserInfo.hbm.xml</value> <value>com/laoer/bbscs/bean/UserDetail.hbm.xml</value> <value> com/laoer/bbscs/bean/Config-${datasource.type}.hbm.xml </value> <value> com/laoer/bbscs/bean/Board-${datasource.type}.hbm.xml </value> <value> com/laoer/bbscs/bean/BoardMaster-${datasource.type}.hbm.xml </value> <value>com/laoer/bbscs/bean/BoardTag.hbm.xml</value> <value>com/laoer/bbscs/bean/Permission.hbm.xml</value> <value> com/laoer/bbscs/bean/Role-${datasource.type}.hbm.xml </value> <value> com/laoer/bbscs/bean/UserGroup-${datasource.type}.hbm.xml </value> <value>com/laoer/bbscs/bean/UserOnline.hbm.xml</value> <value>com/laoer/bbscs/bean/LoginError.hbm.xml</value> <value> com/laoer/bbscs/bean/BoardPermission.hbm.xml </value> <value>com/laoer/bbscs/bean/UserLevel.hbm.xml</value> <value>com/laoer/bbscs/bean/BoardSave.hbm.xml</value> <value>com/laoer/bbscs/bean/Friend.hbm.xml</value> <value> com/laoer/bbscs/bean/Note-${datasource.type}.hbm.xml </value> <value>com/laoer/bbscs/bean/BookMark.hbm.xml</value> <value> com/laoer/bbscs/bean/ForumMain-${datasource.type}.hbm.xml </value> <value> com/laoer/bbscs/bean/ForumHistory-${datasource.type}.hbm.xml </value> <value>com/laoer/bbscs/bean/Subscibe.hbm.xml</value> <value>com/laoer/bbscs/bean/Vote.hbm.xml</value> <value>com/laoer/bbscs/bean/VoteItem.hbm.xml</value> <value>com/laoer/bbscs/bean/VoteUser.hbm.xml</value> <value>com/laoer/bbscs/bean/AgreeAgainst.hbm.xml</value> <value>com/laoer/bbscs/bean/Commend.hbm.xml</value> <value>com/laoer/bbscs/bean/ForumBuy.hbm.xml</value> <value> com/laoer/bbscs/bean/BoardAuthUser.hbm.xml </value> <value>com/laoer/bbscs/bean/SysNumStat.hbm.xml</value> <value>com/laoer/bbscs/bean/UserTop.hbm.xml</value> <value> com/laoer/bbscs/bean/Elite-${datasource.type}.hbm.xml </value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_
sql"> ${hibernate.show_
sql} </prop> <prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size} </prop> <prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size} </prop> </props> </property> </bean> //配置事务处理 <bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com.laoer.bbscs.web.action.BaseAction</value> </list> </property> </bean> <bean id="sysOptionsValues" class="com.laoer.bbscs.comm.SysOptionsValues"> <property name="messageSource"> <ref bean="messageSource" /> </property> </bean> <bean id="sysConfig" class="com.laoer.bbscs.service.config.SysConfig"> <constructor-arg> <ref bean="configService" /> </constructor-arg> <property name="isLoad"> <value>${bbscs.isloadconfig}</value> </property> </bean> <bean id="userConfig" class="com.laoer.bbscs.service.config.UserConfig"> <property name="safePath"> <value>${bbscs.safePath}</value> </property> </bean> <bean id="forumConfig" class="com.laoer.bbscs.service.config.ForumConfig"> <property name="safePath"> <value>${bbscs.safePath}</value> </property> </bean> //boardDAO 是配置的ID //com.laoer.bbscs.dao.hibernate.BoardHibernateDAO 需要实例化的类 //以后com.laoer.bbscs.dao.hibernate.BoardHibernateDAO这个类在其他类和
方法里不用 new BoardHibernateDAO()这样的语句去实例化直接用他刚才定义好的ID
名称boardDAO去
调用即可。 com.laoer.bbscs.dao.hibernate.BoardHibernateDAO这个类实例化的时候有些
方法需要参数,比如说他
调用数据库,Hibernian
调用数据库时候使用sessionFactory工厂 。 sessionFactory工厂是那里定义的呢 就是上面说的 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSession
factorybean"> <property name="dataSource">。。。。 这里定义的 所以直接用 <property name="sessionFactory"> <ref local="sessionFactory" /> </property> 这种
方法传递给com.laoer.bbscs.dao.hibernate.BoardHibernateDAO类 举一个简单的例子例: BoardHibernateDAO类: public class BoardHibernateDAO{ 这里使用sessionFactory以前的习惯是这样: 先用new去实例化 SessionFactory sessionFactory = new sessionFactory(); 在
方法里
调用 现在的
方法是 先定义一个
属性 再用get set
方法(spring里的说法是依赖注入)来
获取刚才配置里的已经实例化好的sessionFactory private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return sessionFactory; } } <bean id="boardDAO" class="com.laoer.bbscs.dao.hibernate.BoardHibernateDAO"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> < /beans>