混合索引java_Spring混合配置装配bean

书不记,熟读可记;义不精,细思可精;惟有志不立,直是无着力处。

——朱熹

前面学习了Spring通过自动化配置、Java代码装配bean,现在我们学习最后一种XML配置装配bean的方式。

XML配置

在创建XML配置文件时,需要在配置文件顶部声明多个XML模式(XSD)文件,这些文件定义了配置Spring的XML元素。创建和管理XML配置文件的一种简单方式是使用Spring Tool Suite插件。如图,可在Eclipse Marketplace中直接搜索并安装该插件。

9067cfaf1a2e6a9a9341dd3a258625dd.png

安装完毕并重启Eclipse之后,便可在方便快捷的创建XML配置。

ef4242131def25c7697d6dda3a851e5a.png

借助构造器注入初始化bean

1) 元素。

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

2)使用Spring 3.0中引入的c-命名空间

(1) 直接饮用构造器参数的名称

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

(2)使用索引来识别构造器参数

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

(3)若只有一个构造器参数,可以不用标示参数

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

借助构造器注入字面量值

在借助构造器装配bean和字面量值方面,两者的和c-命名空间的功能是相同的,只是装配bean使用的ref属性,而注入字面量值(如字符串)使用的是value属性。

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

借助构造器注入集合

能够将集合装配到构造器参数中,而c-命名空间无法做到。的子元素元素或元素可以将一个包含bean引用(或是值)的列表传递到构造器中。

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

LOL

和元素的区别不大,其中最重要的不同在于当Spring创建要装配的集合时,所创建的是java.util.Set还是java.util.List。如果是Set那么集合中的元素是无序且不重复的。

通过属性(set方法)注入bean

1)使用元素的子元素

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

2)使用p-命名空间

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

借助util命名空间,将集合单独声明为bean

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

在Java代码配置中引入XML配置

packagechapter2.practice2;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Import;importorg.springframework.context.annotation.ImportResource;/*** 创建一个Java根配置类

* @ToDo 将两个或更多的Java配置类或XML配置文件组合起来,并启用组件扫描

**/@Configuration

@ComponentScan

@Import(GamerConfig.class)

@ImportResource("classpath:lol-config.xml")public classGameSystemConfig {

}

在XML配置中引用Java代码配置

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值