java context.xml_[Java教程]applicationContext.xml详解

[Java教程]applicationContext.xml详解

0 2015-06-29 15:00:06

想必用过Spring的程序员们都有这样的感觉,Spring把逻辑层封装的太完美了(个人感觉View层封装的不是很好)。以至于有的初学者都不知道Spring配置文件的意思,就拿来用了。所以今天我给大家详细解释一下Spring的applicationContext.

以下是详解Spring的applicationContext.

[html] view plaincopyprint?

beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">

[html] view plaincopyprint?

[html] view plaincopyprint?

com.mysql.jdbc.Driver

[html] view plaincopyprint?

jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8

[html] view plaincopyprint?

root

[html] view plaincopyprint?

123

[html] view plaincopyprint?

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

[html] view plaincopyprint?

com/alonely/vo/User.hbm.

[html] view plaincopyprint?

class="org.springframework.orm.hibernate3.HibernateTemplate">

[html] view plaincopyprint?

[html] view plaincopyprint?

[html] view plaincopyprint?

以上Spring的applicationContext.

对里面的一些概念还不熟悉,一个字"晕"啊,在网上搜搜资料,解释一下applicationcontext.

我们以项目中的订单Order为例简要说明一下Spring与Hibernate的集成。关于如何使用Hibernate来对数据库表做映射,我们在前面已经做了介绍,这里我们关心的是如何配置Spring,使它能管理Hibernate。其实,只要在Spring的配置文件(我们这里是applicationContext.[html] view plaincopyprint?

com/ascent/bean/hibernate.cfg.

这样,Spring和Hibernate的第一步整合就完成了,现在到了关键的地方——如何让Spring和Hibernate双剑合璧来实现业务逻辑?

还是在applicationContext.[html] view plaincopyprint?

在上面你大概可以感觉到Spring给我们带来的好处了,Spring的IoC模式可以统一管理各层,而又使各层松散耦合在一起,使各层之间实现最大的解耦性,这也是Web架构一向的追求。

但是,Spring带来的好处还不止于此,除了IoC还有AOP,Spring可以运用AOP来实现很多功能,最常用的就是事务处理。这里我们用了业务服务(business service)层和数据存取对象(Data Access Object)层,在业务服务层我们增加事务处理,数据存取对象层负责数据读写。

首先,组装配置好Service Beans。[html] view plaincopyprint?

PROPAGATION_REQUIRED,readOnly,-OrderException

PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException

之后,需要把业务服务对象和数据存取对象也组装起来,并把这些对象配到一个事务管理器(transaction manager)里。

在Spring中的配置信息。[html] view plaincopyprint?

每个对象都联系着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就完成了对业务对象的管理。

本文网址:http://www.shaoqun.com/a/122436.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

xml

0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值