前端之家收集整理的这篇文章主要介绍了
applicationContext.xml文件详解,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<!-- 头文件,主要注意一下编码-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 建立数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库驱动,我这里使用的是MysqL数据库-->
<property name="driverClassName">
<value>com.MysqL.jdbc.Driver</value>
</property>
<!-- 数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦!-->
<property name="url">
<value>
jdbc:MysqL://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8
</value>
</property>
<!-- 数据库的用户名-->
<property name="username">
<value>root</value>
</property>
<!-- 数据库的密码-->
<property name="password">
<value>123</value>
</property>
</bean>
<!-- 把数据源注入给Session工厂-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 配置映射文件-->
<property name="mappingResources">
<list>
<value>com/alonely/vo/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 把Session工厂注入给hibernateTemplate -->
<!-- 解释一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在执行时自动建立HibernateCallback 对象,例如:load()、get()、save、delete()等方法。-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg>
<ref local="sessionFactory" />
</constructor-arg>
</bean>
<!-- 把DAO注入给Session工厂-->
<bean id="userDAO" class="com.alonely.dao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 把Service注入给DAO -->
<bean id="userService" class="com.alonely.service.UserService">
<property name="userDAO">
<ref local="userDAO" />
</property>
</bean>
使用<bean>来为每一个bean进行设定,“id”属性是用以设定Bean的实例别名,稍后可以使用id来取得Bean的实例,"class"属性用来指定Bean的类名称,<property>标签的userDAO设定了setter的名称(setUserDAO),并ref属性隐射到到已经设定的bean的id。还可以有value属性。
另外:
<bean id="user" class="com.alonely.struts.action.UserAction">
<constructor-arg index=“0”>
<value>汤青</value>
</constructor>
<constructor-arg index=“1”>
<value>女</value>
</constructor>
</bean>
必须是UserAction中有构造函数,和两个成员变量,index隐射,建议还要有无参数构造函数。这就是Constructor Injection 注入类型。
<!-- 把Action注入给Service -->
<bean name="/user" class="com.alonely.struts.action.UserAction">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
</beans>
下面是Struts+Spring+Hibernate的中applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--此配置文件整合了Spring和hibernate的配置文件!采用BasicDataSource注入到hibernate sessionFactory中,以得到数据库连接-->
<!-- dbcp相关参数配置见http://marzian.blog.163.com/blog/static/266863120086845013920-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@10.18.100.52:1521:dxcp1</value>
</property>
<property name="username">
<value>newgspls</value>
</property>
<property name="password">
<value>newgspls</value>
</property>
<property name="initialSize">
<value>1</value>
</property>
<property name="maxActive">
<value>60</value>
</property>
<property name="minIdle">
<value>1</value>
</property>
<property name="maxWait">
<value>6000</value>
</property>
<property name="validationQuery">
<value>select user from dual</value>
</property>
</bean>
<!--从连接池中抽取出本地数据库JDBC对象 几种JDBC对象抽取器,可根据不同的应用服务器进行调整
WebLogic:WebLogicNativeJdbcExtractor
WebSphere:WebSphereNativeJdbcExtractor
JBoss:JBossNativeJdbcExtractor
-->
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true"></bean>
<!-- s可以使用Spring的JDBC帮助类jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<!--Spring 提供了两种LobHandler 用于处理Blob数据
DefaultLobHandler:适用于大部分的数据库,如sqlServer,MysqL,对Oracle 10g也适用,但不适用于Oracle9i
oracleLobHandler:适用于Oracle 9i和Oracle 10g。
-->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor" />
</property>
</bean>
<!--Hibernate Session工厂配置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="lobHandler" ref="lobHandler"/>
<property name="mappingResources">
<list>
<!-- hibernate实体映射文件!即生成的*.hbm.xml-->
<value>com/dao/hibernate/xml/MaintenanceWork.hbm.xml</value>
<value>com/dao/hibernate/xml/SignIn.hbm.xml</value>
</list>
</property>
<!-- sessionFactory相关配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--采用Hibernate2.0的Hsql解释器,解决了中文问题-->
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<!--打开Query Cache开关,需要Cache的query需要单独配置-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<!--事务管理器配置-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!--AOP 事务配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<tx:method name="add*" read-only="false"/>
<tx:method name="insert*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="del*" read-only="false"/>
<tx:method name="audit*" read-only="false"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="SysFileOperation" expression="execution(* com.biz.system.SysFilesBiz.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="SysFileOperation"/>
</aop:config>
<!-- jdbc Dao 配置begion -->
<bean id="jdbcDao" class="com.gsww.newgspls.dao.JdbcDao">
<property name="ds">
<ref local="dataSource"/>
</property>
</bean>
<!-- 信息发布配置开始-->
<bean id="sysInfoBiz" class="com.biz.info.SysInfoBiz">
<property name="sysInfoDao">
<ref local="sysInfoDao" />
</property>
</bean>
<bean id="sysInfoDao" class="com.dao.info.SysInfoDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
业务类SysInfoBiz
在applicationContext.xml配置注入关系后,在业务类中通过set注入即可得到SysInfoDao
package com.biz.info;
/**
* @author 作者:周伟
*
* @version创建时间:2008-7-1 下午04:11:04
*
* 类说明
*/
import java.util.List;
import com.dao.info.SysInfoDao;
import com.domain.info.SysInfo;
public class SysInfoBiz {
private SysInfoDao sysInfoDao = null;
/**
* 判断用户是否包含在已读人列表,即通过clob字段里的用户ID查看登陆用户是否已经读过
*
* @param supportTable
* @param reader
* @return
*/
public boolean isRead(SysInfo sysInfo,String reader) {
if (sysInfo.getInfoReader() != null) {
String str = sysInfo.getInfoReader();
if (str.indexOf(",") < 0) {
return (str.contains(reader));
} else {
String pstr = reader + ",";
String astr = "," + reader;
String str1 = "," + reader + ",";
return (str.startsWith(pstr) || str.endsWith(astr) || str
.contains(str1));
}
} else {
return false;
}
}
/**
* 返回信息读者个数
*
* @param supportTable
* @return
*/
public int readTime(SysInfo sysInfo) {
if (sysInfo.getInfoReader() != null) {
String str = sysInfo.getInfoReader();
String[] str1 = str.split(",");
return str1.length;
} else {
return 0;
}
}
/**
* 对未在已读人列表的读者进行添加到SupReader
*
* @param supportTable
* @param reader
* @return
*/
public boolean addReader(SysInfo sysInfo,String reader) {
if (reader != null && !reader.equals("null")
&& !isRead(sysInfo,reader)) {
if (sysInfo.getInfoReader() != null
&& !sysInfo.getInfoReader().equals("null")
&& !sysInfo.getInfoReader().equals("")) {
sysInfo.setInfoReader(sysInfo.getInfoReader() + "," + reader);
} else {
sysInfo.setInfoReader(reader);
}
try {
sysInfoDao.update(sysInfo);
} catch (Exception re) {
re.printStackTrace();
}
return true;
} else {
return false;
}
}
/**
*删除
*/
public void delete(SysInfo persistentInstance) {
sysInfoDao.delete(persistentInstance);
}
public List findAll() {
return sysInfoDao.findAll();
}
public List findByExample(SysInfo instance) {
return sysInfoDao.findByExample(instance);
}
/**
*通过id得到实体
*/
public SysInfo findById(String id) {
return sysInfoDao.findById(id);
}
/**
*保存实体
*/
public void save(SysInfo transientInstance) {
transientInstance.setFlag("1");
sysInfoDao.save(transientInstance);
}
/**
*更新实体
*/
public void update(SysInfo transientInstance) {
transientInstance.setFlag("1");
sysInfoDao.update(transientInstance);
}
public SysInfoDao getSysInfoDao() {
return sysInfoDao;
}
public void setSysInfoDao(SysInfoDao sysInfoDao) {
this.sysInfoDao = sysInfoDao;
}
public PageDAO getPageDao() {
return pageDao;
}
public void setPageDao(PageDAO pageDao) {
this.pageDao = pageDao;
}
}
SysInfoDao 连接数据库类
package com.dao.info;
/**
* @author 作者:周伟
*
* @version创建时间:2008-7-1 下午04:11:04
*
* 类说明 信息发布公用表dao类
*/
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.gsww.newgspls.domain.info.SysInfo;
public class SysInfoDao extends HibernateDaoSupport {
/**
* 保存SysInfo实体
*
* @param transientInstance SysInfo对象
*/
public void save(SysInfo transientInstance) {
try {
getHibernateTemplate().save(transientInstance);
} catch (RuntimeException re) {
throw re;
}
}
/**
*更新SysInfo实体
*
* @param transientInstance SysInfo对象
*/
public void update(SysInfo transientInstance) {
try {
getHibernateTemplate().update(transientInstance);
} catch (RuntimeException re) {
throw re;
}
}
/**
*删除SysInfo实体
*
* @param transientInstance SysInfo对象
*/
public void delete(SysInfo persistentInstance) {
try {
getHibernateTemplate().delete(persistentInstance);
} catch (RuntimeException re) {
throw re;
}
}
/**
*通过ID得到SysInfo对象
*
* @param id信息主键
* @return
*/
public SysInfo findById(java.lang.String id) {
try {
SysInfo instance = (SysInfo) getHibernateTemplate().get(
"com.domain.info.SysInfo",id);
return instance;
} catch (RuntimeException re) {
throw re;
}
}
}
原文链接:https://www.f2er.com/xml/300009.html