applicationContext.xml详解
springhibernatebeanservice数据库class
[html] view plaincopyprint?
以下是详解Spring的applicationContext.xml文件代码:
[html] view plaincopyprint?
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"> <beans>
[html] view plaincopyprint?
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"> <!-- 数据库驱动,我这里使用的是Mysql数据库 --> [html] view plaincopyprint?
<propertyname="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property>
[html] view plaincopyprint?
<propertyname="url"> <value> jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8 </value> </property> <!-- 数据库的用户名 --> [html] view plaincopyprint?
<propertyname="username"> <value>root</value> </property> <!-- 数据库的密码 --> [html] view plaincopyprint?
<propertyname="password"> <value>123</value> </property> </bean> <!-- 把数据源注入给Session工厂 --> [html] view plaincopyprint?
<beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <propertyname="dataSource"> <refbean="dataSource"/> </property> <!-- 配置映射文件 --> [html] view plaincopyprint?
<propertyname="mappingResources"> <list> <value>com/alonely/vo/User.hbm.xml</value> </list> </property> </bean> <!-- 把Session工厂注入给hibernateTemplate --> [html] view plaincopyprint?
<beanid="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <constructor-arg> <reflocal="sessionFactory"/> </constructor-arg> </bean> <!-- 把DAO注入给Session工厂 --> [html] view plaincopyprint?
<beanid="userDAO"class="com.alonely.dao.UserDAO"> <propertyname="sessionFactory"> <refbean="sessionFactory"/> </property> </bean> <!-- 把Service注入给DAO --> [html] view plaincopyprint?
<beanid="userService"class="com.alonely.service.UserService"> <propertyname="userDAO"> <reflocal="userDAO"/> </property> </bean> <!-- 把Action注入给Service --> [html] view plaincopyprint?
<beanname="/user"class="com.alonely.struts.action.UserAction"> <propertyname="userService"> <refbean="userService"/> </property> </bean> </beans> 以上Spring的applicationContext.xml文件我是用的SSH架构,如果您用Spring的MVC架构,其原理也是一样的。
对里面的一些概念还不熟悉,一个字"晕"啊,在网上搜搜资料,解释一下applicationcontext.xml 我们以项目中的订单Order为例简要说明一下Spring与Hibernate的集成。关于如何使用Hibernate来对数据库表做映射,我们在前面已经做了介绍,这里我们关心的是如何配置Spring,使它能管理Hibernate。其实,只要在Spring的配置文件(我们这里是applicationContext.xml)中配置一个叫做sessionFactory的bean,Spring就可以和Hibernate联系起来了。
[html] view plaincopyprint?
<beanid="sessionFactory"class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <propertyname="configLocation"> <value>com/ascent/bean/hibernate.cfg.xml</value> </property> </bean> 这样,Spring和Hibernate的第一步整合就完成了,现在到了关键的地方――如何让Spring和Hibernate双剑合璧来实现业务逻辑? 还是在applicationContext.xml文件中先做一个配置。
[html] view plaincopyprint?
<beanid="transactionManager"class="org.springframework.orm.hibernate.HibernateTransactionManager"> <propertyname="sessionFactory"> <reflocal="sessionFactory"/> </property> </bean> 在上面你大概可以感觉到Spring给我们带来的好处了,Spring的IoC模式可以统一管理各层,而又使各层松散耦合在一起,使各层之间实现最大的解耦性,这也是Web架构一向的追求。 但是,Spring带来的好处还不止于此,除了IoC还有AOP,Spring可以运用AOP来实现很多功能,最常用的就是事务处理。这里我们用了业务服务(business service)层和数据存取对象(Data Access Object)层,在业务服务层我们增加事务处理,数据存取对象层负责数据读写。 首先,组装配置好Service Beans。
[html] view plaincopyprint?
<beanid="orderService"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <propertyname="transactionManager"> <reflocal="transactionManager"/> </property> <propertyname="target"> <reflocal="orderTarget"/> </property> <propertyname="transactionAttributes"> <props> <propkey="find*">PROPAGATION_REQUIRED,-OrderException</prop> <propkey="save*">PROPAGATION_REQUIRED,-OrderMinimumAmountException</prop> </props> </property> </bean> 之后,需要把业务服务对象和数据存取对象也组装起来,并把这些对象配到一个事务管理器(transaction manager)里。 在Spring中的配置信息。
[html] view plaincopyprint?
<beanid="orderTarget"class="com.ascent.business.service.OrderServiceImpl"> <propertyname="orderDAO"> <reflocal="orderDAO"/> </property> </bean> <beanid="orderDAO"class="com.ascent.dao.hibernate.OrderHibernateDAO"> <propertyname="sessionFactory"> <reflocal="sessionFactory"/> </property> </bean> 每个对象都联系着Spring,并且能通过Spring注入到其他对象。把它与Spring的配置文件比较,观察他们之间的关系。 Spring就是这样基于配置文件,将各个Bean搭建在一起的。 这里我们使用一个TransactionProxyFactoryBean,它定义了一个setTransactionManager(),这个对象很有用,它能很方便地处理你申明的Service Object中的事物,你可以通过transaction Attributes属性来定义怎样处理。 TransactionProxyFactoryBean还有个setter,这会被业务服务对象(orderTarget)引用,orderTarget定义了业务服务层,并且它还有个属性,由setOrderDAO()引用这个属性。 还有一点要注意,bean可以用两种方式创造,这些都在单例模式(Sington)和原型模式(propotype)中定义了。默认的方式是singleton,这意味着共享的实例将被束缚,而原型模式是在Spring用到bean的时候允许新建实例的。当每个用户需要得到他们自己Bean的Copy时,你应该仅使用prototype模式。
这样,Spring和Hibernate就完成了对业务对象的管理。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |