Spring入门(4)-注入Bean属性
Spring入门(4)-注入Bean属性
本文介绍如何注入Bean属性,包括简单属性、引用、内部Bean、注入集合等。
0. 目录
- 注入简单值
- 注入引用
- 注入内部Bean
- 装配集合
- 装配空值
- 使用命名空间p
1. 注入简单值
前面介绍过注入简单值的例子,在这里回顾一下。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void show() {
System.out.println(id);
}
public void addPerson(Person p) {
System.out.println("add person: " + p.getName());
}
}
<?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 name="PersonBll" class="com.chzhao.springtest.PersonBll" >
<property name="id" value="abcdefg"></property>
</bean>
</beans>
2. 注入引用
大部分情况下简单值不能满足要求,往往是需要一个引用。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void show() {
System.out.println(this.person.getName());
}
public void addPerson(Person p) {
System.out.println("add person: " + p.getName());
}
}
<?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 name="p" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" >
<property name="person" ref="p"></property>
</bean>
</beans>
3. 注入内部Bean
除了上面这种方式之外,也可以把Bean定义为内部Bean,防止别的类调用。
<?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 name="PersonBll" class="com.chzhao.springtest.PersonBll" >
<property name="person" >
<bean name="p" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
</property>
</bean>
</beans>
4. 装配集合
Spring也支持装配集合,支持的集合如下:
| 集合元素 | 用途 |
|---|---|
| list | 装配list类型的值,允许重复 |
| set | 装配set类型的值,不允许重复 |
| map | 装配map类型的值 |
| props | 装配properties类型的值,名称和值都必须是String |
4.1. 装配list或set
装配list和set差不多,只是set元素不能重复
package com.chzhao.springtest;
import java.util.List;
public class PersonBll implements IPersonBll {
private List<String> idList;
public List<String> getIdList() {
return idList;
}
public void setIdList(List<String> idList) {
this.idList = idList;
}
public void show() {
for (String s : this.idList) {
System.out.println(s);
}
}
public void addPerson(Person p) {
System.out.println("add person: " + p.getName());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
<property name="idList">
<list>
<value>wang</value>
<value>zhao</value>
<value>li</value>
</list>
</property>
</bean>
</beans>
4.2. 装配map
package com.chzhao.springtest;
import java.util.Map;
public class PersonBll implements IPersonBll {
private Map<Integer, Person> pmap;
public Map<Integer, Person> getPmap() {
return pmap;
}
public void setPmap(Map<Integer, Person> pmap) {
this.pmap = pmap;
}
public void show() {
for (Integer i : this.pmap.keySet()) {
System.out.println(i);
System.out.println(this.pmap.get(i).getName());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="laoli" class="com.chzhao.springtest.Person">
<property name="name" value="老李" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
<property name="pmap">
<map>
<entry key="1" value-ref="laowang"></entry>
<entry key="2" value-ref="laoli"></entry>
</map>
</property>
</bean>
</beans>
MAP的属性包括
| 属性 | 用途 |
|---|---|
| key | key为String |
| key-ref | key为引用 |
| value | 值为string |
| value-ref | 值为引用 |
4.3. 装配properties
package com.chzhao.springtest;
import java.util.Properties;
public class PersonBll implements IPersonBll {
private Properties pro;
public Properties getPro() {
return pro;
}
public void setPro(Properties pro) {
this.pro = pro;
}
public void show() {
System.out.println(this.pro.get("1"));
System.out.println(this.pro.get("2"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
<property name="pro">
<props>
<prop key="1">老王</prop>
<prop key="2">老李</prop>
</props>
</property>
</bean>
</beans>
5. 装配空值
也可以把属性赋值为空
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void show() {
if (this.id == null) {
System.out.println("null");
} else {
System.out.println(this.id);
}
}
}
<?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 name="PersonBll" class="com.chzhao.springtest.PersonBll">
<property name="id" ><null></null></property>
</bean>
</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 name="PersonBll" class="com.chzhao.springtest.PersonBll">
<property name="id" ><null/></property>
</bean>
</beans>
6. 使用命名空间p
Spring提供了命名空间p简化Bean属性定义,需要在XML中增加
xmlns:p="http://www.springframework.org/schema/p"
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void show() {
System.out.println(this.person.getName());
System.out.println(this.id);
}
public void addPerson(Person p) {
System.out.println("add person: " + p.getName());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean name="p" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll"
p:person-ref = "p" p:id="0000">
</bean>
</beans>
Spring入门(4)-注入Bean属性的更多相关文章
- Spring学习笔记--注入Bean属性
这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...
- Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)
在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...
- spring实战一:装配bean之注入Bean属性
内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...
- spring学习笔记之---bean属性注入
bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...
- 依赖注入Bean属性
一.Bean属性依赖注入 对于类成员变量,注入方式有三种 •构造函数注入 •属性setter方法注入 •接口注入 Spring支持前两种 1.构造函数 属性注入 使用构造方法注入,在Spring配置文 ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- spring boot 动态注入bean
方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...
- spring 初始化时注入bean实现listener的方法
两种方法: 1.实现ApplicationListener<ContextRefreshedEvent>的onApplicationEvent(ContextRefreshedEvent ...
- spring注解方式注入bean
用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包 applicationContext.xml <?xml version="1.0" en ...
随机推荐
- NFC(2)NFC、蓝牙和红外之间的差异
NFC(2)NFC.蓝牙和红外之间的差异表
- C# Index 定义索---引具体使用
using System;using System.Collections.Generic;namespace TestThisIndex{ public class Program { ...
- ogre, dx, opengl坐标矩阵
opengl 右手坐标系 列向量 左乘 列主序存储矩阵osg 右手坐标系 行向量 右乘 行主序存储矩阵d3d 左手坐标系 行向量 右乘 行主序存储矩阵ogre 右手坐标系 列向量 ...
- 使用DX绘制3D物体时新手常犯错误,看不见物体时可以一一排查
1.镜头不对: 物体不在镜头范围内,检查视图矩阵,世界矩阵,投影矩阵. 2.颜色全黑: 打开光照情况下,MATERIAL全为0, 或,在没有打开光照情况下,颜色值为0,造成全黑.检查当前Materia ...
- bzoj1567: [JSOI2008]Blue Mary的战役地图
将矩阵hash.s[0]忘了弄成0,输出中间过程发现了. hash.sort.判重.大概这样子的步骤吧. #include<cstdio> #include<cstring> ...
- 8款PHP调试工具
Web 开发并不是一项轻松的任务,有超级多服务端脚本语言提供给开发者,但是当前 PHP 因为具有额外的一些强大的功能而越来越流行.PHP 是最强大的服务端脚本语言之一,同时也是 web 开发者和设计者 ...
- 漫谈 polling 和 Websocket
Http被设计成了一个单向的通信的协议,即客户端发起一个request,然后服务器回应一个response.这让服务器很为恼火:我特么才是老大,我居然不能给小弟发消息... 轮询 老大发火了,小弟们自 ...
- OK335xS psplash 进度条工作原理 hacking
#!/bin/sh # # rc This file is responsible for starting/stopping # services when the runlevel changes ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 【DFS】NYOJ-325-zb的生日
[题目链接:NYOJ-325] 一道以我名字命名的题目,难道要我生日的时候再A? 思路:依旧深搜,但这个问题应该有一个专有名词吧,看别的博客说是 “容量为 sum/2 的背包问题”,不懂... // ...