spring的Bean注入和P标签使用

栏目: Java · 发布时间: 6年前

内容简介:[spring]xml配置文件中bean属性的两种写法(p:configLocation <=> <property name=”configLocation”/>)p:configLocation:<!– mybatis文件配置,扫描所有mapper文件 –>

[spring]xml配置文件中bean属性的两种写法(p:configLocation <=> <property name=”configLocation”/>)

p:configLocation:

<!– mybatis文件配置,扫描所有mapper文件 –>

<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”

p:dataSource-ref=”dataSource”

p:configLocation=”classpath:mybatis-config.xml”

p:mapperLocations=”classpath:com/eliteams/quick4j/web/dao/*.xml”/>

<property name=”configLocation”/>:

复制代码

<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>

<property name=”dataSource” ref=”dataSource”/>

<!– mapper和resultmap配置路径 –>

<property name=”classpath:com/eliteams/quick4j/web/dao/*.xml” />

<property name=”configLocation” value=”classpath:mybatis-config.xml”/>

</bean>

复制代码

注:org.mybatis.spring.SqlSessionFactoryBean:

SqlSessionTemplate中需要的是SqlSessionFactory,而不是SqlSessionFactoryBean。这个是因为SqlSessionFactoryBean继承了FactoryBean<SqlSessionFactory>

在spring的bean配置文件中我们常可以见到下面的例子:

<bean id=”boss” class=”com.zj.Boss” scope=”prototype”  p:name-ref=”name”>

</bean>

其中,p:name-ref=”name”使用了p标签来配置bean的name的引用。

在使用p标签配置bean的属性时,我们先要声明p标签的命名空间xmlns:p=”http://www.springframework.org/schema/p”。

然后,使用p标签时分两种情况。

第一种配置基本类型的属性:p:name=“zhujun”

第二种配置引用类型的属性:p:name-ref=“name”;

注意,在配置引用类型的属性时在属性后加上ref表明我要配置的属性时引用类型。

1.构造方法参数 对应 配置文件 <constructor-arg> 元素

spring的Bean注入和P标签使用

spring的Bean注入和P标签使用

可以index|name|type 三选一 、三选二  ; ref|value 二选一

2. setter方法注入(开发推荐)

spring的Bean注入和P标签使用

为需要注入属性提供setter方法

配置 每个注入属性, 对应<property> 元素

spring的Bean注入和P标签使用

3. p名称空间的使用

spring2.5以后,为了简化setter方法属性注入,引用p名称空间的概念,可以将<property> 子元素,简化为<bean>元素属性配置 !!

在applicationContext.xml 引入p 名称空间

<beans xmlns=”http://www.springframework.org/schema/beans”

xmlns:p=”http://www.springframework.org/schema/p”

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”>

b.  简化注入的配置

spring的Bean注入和P标签使用

4. 集合类型的属性注入(了解)

Spring 对每个常用集合对象,提供单独元素完成注入

List 对象 —- <list> 元素

Set 对象 —- <set> 元素

Map对象 —-<map>元素

Properties 对象 —- <props> 元素

spring的Bean注入和P标签使用

集合属性的注入,主要用于框架的配置 !

来源: https://www.cnblogs.com/xumaodun/p/4928136.html


以上所述就是小编给大家介绍的《spring的Bean注入和P标签使用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to Probability Theory and Its Applications

An Introduction to Probability Theory and Its Applications

William Feller / Wiley / 1991-1-1 / USD 120.00

Major changes in this edition include the substitution of probabilistic arguments for combinatorial artifices, and the addition of new sections on branching processes, Markov chains, and the De Moivre......一起来看看 《An Introduction to Probability Theory and Its Applications》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具