Spring-注入参数详解-[字面值及引用其他Bean]
- 概述
- 字面值
- XML中的特殊符号的处理
- 5个特殊符号
- 特殊符号的处理方式
- 关于注入值空格的处理
- XML中的特殊符号的处理
- 引用其他Bean
- 实例
- ref元素的3个属性
- 引用父容器中的Bean实例
- 内部Bean
- null值
- 级联属性
概述
在Spring配置文件中,不但可以将String、int等字面值注入bean中,还可以将集合、map等类型的数据注入Bean中, 此外还可以注入配置文件中其他定义的Bean.
字面值
所谓字面值一般指的是可以用字符串表示的值,这些值可以通过<value>
元素标签进行注入。
在默认情况下,基本数据类型及其封装类、String等类型都可以采用字面值注入的方式。
比如前几篇博文中经常使用的案例:
<bean id="plane" class="com.xgj.ioc.inject.set.Plane"><property name="brand"><value><![CDATA[Airbus&A380]]></value><!-- 或者使用转义序列<value>Airbus&A380</value>--></property><property name="color"><value>red</value></property><property name="speed"><value>700</value></property></bean>
XML中的特殊符号的处理
5个特殊符号
XML共有5个特殊符号 & < > " '
特殊符号的处理方式
- 添加
<![CDATA[ ]]>
特殊标签,阻止XML解析,让其当做普通的文本处理 - 使用XML转义序列处理这些特殊字符,如下
特殊符号 | 转移序列 |
---|---|
< |
<
|
> |
>
|
& |
&
|
“ |
"
|
‘ |
'
|
关于注入值空格的处理
一般情况下,xml解析器会忽略元素标签内部字符串的前后空格,但是Spring不会忽略空格。
举个例子:
<property name="brand"><value> Airbus&A380 </value></property>
运行结果如下:
引用其他Bean
Spring IoC容器中定义的Bean可以相互引用,IoC容器充当“红娘”的角色。
实例
警察类中有枪类型的属性
POJO类
package com.xgj.ioc.inject.construct.ref;public class Police {private Gun gun;public void setGun(Gun gun) {this.gun = gun;}public void userGun() {gun.fire();}}
POJO类
package com.xgj.ioc.inject.construct.ref;public class Gun {public void fire() {System.out.println("Gun Fires");}}
配置文件
<?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="police" class="com.xgj.ioc.inject.construct.ref.Police"><property name="gun"><!-- 通过ref应用容器中的gun ,建立police和gun的依赖关系 --><ref bean="gun" /></property></bean><bean id="gun" class="com.xgj.ioc.inject.construct.ref.Gun" />
</beans>
测试类
package com.xgj.ioc.inject.construct.ref;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class RefTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/inject/construct/ref/police-gun.xml");Police police = ctx.getBean("police", Police.class);police.userGun();}}
运行结果
ref元素的3个属性
bean:通过该属性可以应用同一容器或者父容器中的bean,这是最常见的形式
local:通过该属性只能引用同一个配置文件中定义的Bean,它可以利用XML解析器自动检查引用的合法性,以便开发人员在编写配置文件时及时发现错误
parent:引用父容器中的Bean,如
<ref parent="gun">
的配置说明gun的Bean是父容器中的Bean
引用父容器中的Bean实例
演示子容器对父容器Bean的引用。
其中 beans_father.xml被父容器加载,beans_son.xml被子容器加载。
POJO类
package com.xgj.ioc.inject.construct.refParentBean;public class Gun {private String brand;private int bulletNum;private double price;public void setBrand(String brand) {this.brand = brand;}public void setBulletNum(int bulletNum) {this.bulletNum = bulletNum;}public void setPrice(double price) {this.price = price;}public void gunInfo() {System.out.println("Gun Information brand:" + brand + ",bulletNum:"+ bulletNum + ",price:" + price);}
}
POJO类
package com.xgj.ioc.inject.construct.refParentBean;public class Police {private Gun gun;public void setGun(Gun gun) {this.gun = gun;}public void introduceGun() {gun.gunInfo();}
}
beans_father.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="gun" class="com.xgj.ioc.inject.construct.refParentBean.Gun"><property name="brand" value="父容器--伯莱塔92F" /><property name="bulletNum" value="11" /><property name="price" value="2888" /></bean></beans>
beans_son.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="gun" class="com.xgj.ioc.inject.construct.refParentBean.Gun"><property name="brand" value="柯尔特M2000" /><property name="bulletNum" value="7" /><property name="price" value="999" /></bean><bean id="police" class="com.xgj.ioc.inject.construct.refParentBean.Police"><property name="gun"><!-- 通过parent属性,引用父容器中的gun,如果定义为bean="gun"则引用本容器中的gun --><ref parent="gun"/></property></bean></beans>
测试类
package com.xgj.ioc.inject.construct.refParentBean;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class RefParentBeanTest {public static void main(String[] args) {// 父容器ApplicationContext pFather = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/inject/construct/refParentBean/beans_father.xml");// 指定pFather为该容器的父容器ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "com/xgj/ioc/inject/construct/refParentBean/beans_son.xml" },pFather);Police police = ctx.getBean("police", Police.class);// 观察是否输出为父容器设置的属性police.introduceGun();}}
运行结果
内部Bean
如果某个bean只会被其中一个引用,而不会被容器中的其它Bean引用,可以以内部Bean的方式注入。
内部bean和Java匿名内部类相似,既没有名字,也不会被前台bean引用,只能在声明处为外部Bean提供实例注入。
内部Bean即使提供了id、name、scope等属性,也会被忽略,scope默认prototype类型
举例:
其他类同上,区别仅在于beans的配置文件。
<?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="police" class="com.xgj.ioc.inject.construct.innerBean.Police"><property name="gun"><!-- 如果gun bean只会被police引用,而不会被容器中的其它Bean引用,可以以内部Bean的方式注入 --><bean class="com.xgj.ioc.inject.construct.innerBean.Gun"><property name="brand" value="柯尔特M2000"/><property name="bulletNum" value="7"/><property name="price" value="788"/></bean></property></bean></beans>
运行结果
null值
如果希望往一个属性中注入一个null值? 使用专用的<null/>
元素标签
POJO类
package com.xgj.ioc.inject.construct.nullValue;public class Pilot {private Plane plane;public void setPlane(Plane plane) {this.plane = plane;}public void drivePlane() {System.out.println("Pilot is driving plane....");plane.info();}
}
POJO类
package com.xgj.ioc.inject.construct.nullValue;public class Plane {private String brand;public void setBrand(String brand) {this.brand = brand;}public void info() {System.out.println("Plane brand:" + brand);}
}
配置文件
<?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="plane" class="com.xgj.ioc.inject.construct.nullValue.Plane"><!-- 设置brand为 null --><property name="brand" value="null" /><!-- 或者 <property name="brand"> <null/> </property> --><!-- 错误的写法,这是spring会将 <value></value>解析为一个空字符串<property name="brand"><value></value></property>--></bean><bean id="pilot" class="com.xgj.ioc.inject.construct.nullValue.Pilot"><property name="plane"><ref bean="plane" /></property></bean></beans>
测试类
package com.xgj.ioc.inject.construct.nullValue;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class NullValueTest {public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/inject/construct/nullValue/beans.xml");Pilot pilot = ctx.getBean("pilot", Pilot.class);pilot.drivePlane();}
}
运行结果:
级联属性
Spring支持级联属性的配置,假设我们希望在定义Pilot时,直接为Plane的属性提供注入值,可以采取如下方式:
<bean id="pilot" class="com.xgj.ioc.inject.construct.cascadeProperty.Pilot"><!-- 以圆点的方式定义级别属性 --><property name="plane.brand" value="A380"/></bean>
上述配置的含义:Spring将调用Pilot.getPlane().setBrand(“A380”)方法进行属性的注入操作。
此时,必须对Pilot的类进行改造,为Plane属性声明一个初始化对象。
实例:
POJO类
package com.xgj.ioc.inject.construct.cascadeProperty;public class Pilot {// 声明初始化对象private Plane plane = new Plane();// 获取实例public Plane getPlane() {return plane;}public void setPlane(Plane plane) {this.plane = plane;}public void introduce() {plane.info();}}
POJO类
package com.xgj.ioc.inject.construct.cascadeProperty;public class Plane {private String brand;public void setBrand(String brand) {this.brand = brand;}public void info() {System.out.println("【Plane brand:】" + brand);}
}
配置文件
<?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="pilot" class="com.xgj.ioc.inject.construct.cascadeProperty.Pilot"><!-- 以圆点的方式定义级别属性 --><property name="plane.brand" value="A380"/></bean>
</beans>
测试类
package com.xgj.ioc.inject.construct.cascadeProperty;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class CascadePropertyTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/inject/construct/cascadeProperty/beans.xml");Pilot pilot = ctx.getBean("pilot", Pilot.class);pilot.introduce();}
}
运行结果
如果没有未Plane属性提供Plan对象,Spring在设置级联属性时将抛出NullValueInNestedPathException异常。
我们在Plane类中 ,不对Plane进行new实例化操作,仅仅声明,再次运行NullValueInNestedPathException异常。
Caused by: org.springframework.beans.NullValueInNestedPathException: Invalid property 'plane' of bean class [com.xgj.ioc.inject.construct.cascadeProperty.Pilot]: Value of nested property 'plane' is null
Spring对级联属性的层级没有限制。
Spring-注入参数详解-[字面值及引用其他Bean]相关推荐
- 《精通Spring4.X企业应用开发实战》读后感第五章(注入参数详解)
转载于:https://www.cnblogs.com/Michael2397/p/7954596.html
- Spring 生命周期详解
Spring 生命周期详解 一.传统JAVA bean的生命周期 使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了. 一旦bean不再被使用,则由Java自动进行垃圾回 ...
- Java Spring Data Redis实战与配置参数详解 application.properties...
Redis作为开源分布式高并发缓存,使用范围非常广泛,主流互联网公司几乎都在使用. Java Spring Boot 2.0实战开发Redis缓存可以参考下面的步骤,Redis安装可以直接使用Linu ...
- Spring的注入方式详解
[html] view plaincopy Spring的注入方式详解 Spring有三个注入方式,type1,type2,type3 type1 接口依赖 type2 setter/getter ...
- spring依赖注入原理详解(转载)
spring依赖注入原理详解----转载 所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中.当spring容器启动后,spring容器初始化,创建并管理bean对象,以及销毁它.所 ...
- Java Spring Data Redis实战与配置参数详解 application.properties
Redis作为开源分布式高并发缓存,使用范围非常广泛,主流互联网公司几乎都在使用. Java Spring Boot 2.0实战开发Redis缓存可以参考下面的步骤,Redis安装可以直接使用Linu ...
- spring boot 实战 / 可执行war启动参数详解
概述 上一篇文章<spring boot 实战 / mvn spring-boot:run 参数详解>主要讲解了spring boot 项目基于maven插件启动过程中借助profil ...
- Spring Boot笔记—多线程系列(三)—配置参数详解
前言 前两篇文章,我们已经学会了如何使用spring boot的多线程和自定义线程池.这篇文章,我们要深入了解上一篇文章中线程池的配置具体含义. 准备工作 说明 为了方便观察线程的情况(如执行完毕数量 ...
- java spring启动和终止_springBoot jar启动以停止脚本参数详解
一.启动脚本 Springboot 项目打成jar包后,在Linux环境上一般有如下几种启动方式: 1. "java -jar XXX.jar " 命令结尾没有 "&am ...
最新文章
- 设计模式六:适配器模式
- openstack云主机无法绑定ip_智汇华云|OpenStack 虚拟机 GPU 性能优化
- 通用权限管理系统组件 (GPM - General Permissions Manager) 给信息管理系统加一个初始化的功能,调用存储过程...
- 音视频直播--技术架构
- puppet应用原理及安装部署
- 在线maven托管_使用Amazon s3托管您的Maven工件
- Windows XP系统安装SQL Server 2005(开发版)图解
- spring-boot-starter-swagger 1.3.0.RELEASE:新增对JSR-303的支持和host的配置
- android zip文件读写,如何直接从.zip文件中读取文件而不在android中提取它
- HDOJ-1014 Uniform Generator
- 新增字段属性“是否转义”,提高列表展示性能
- 如何在Byte[]和String之间进行转换
- 【Kindle】Kindle免费推送网
- 遥感导论(梅安新版)知识点概括
- php 9 698.00,698-00-0|2-溴-N,N-二甲基苯胺|2-bromo-N,N-dimethylaniline|2-Bromo-N,N-dimethylaniline......
- 【算法】两矩形相交的判定
- 近似商标可以申请吗?
- 萌新成长计划(开篇章)
- python:sxtwl(日历库)
- 三、python爬虫学习--动态下载网站漫画