实例化一个容器

5 篇文章 0 订阅
       实例化一个Spring IoC容器很简单。为定位的路径提供一个  ApplicationContext 构造器,他允许容器通过外面的各种资源加载配置元数据,比如系统文件。JAVA的加载方法,等等。
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "services.xml""daos.xml"});
注:在你了解完Spring IoC 容器后,你可能想了解更多关于Spring的资源抽象化,比如第六章对资源的描述。这些资源提供了一个简单的机制根据已经定位好的URL路径读取输入流。特殊情况,这些资源路径被用来构建应用的上下文,6.7节有相关的描述:“应用上下文与资源路径”。
       下面的例子展示了服务层的对象的配置文件(services.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
   
下面的例子展示访问数据的对象 dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>

在上面的例子里面,服务层是由 PetStoreServiceImpl 类构成的,两个访问数据对象的类型是 JpaAccountDao 和  JpaItemDao(基于 JPA Object/Relational 映射标准 )。property name 节点指的是JavaBean属性的名称,ref节点指的是另外一个bean定义的名称。这种 id 和 ref 之间的联接表达了协作对象之间的依赖关系。关于配置对象的依赖关系的详细解释,参考”依赖性“章节。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值