Spring学习笔记--注入Bean属性
这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果。
package com.moonlit.myspring; import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Properties; public class MoonlightPoet {
private String name;
private int age;
private Poem poem;
private List<String> list;
private Map<String, String> map;
private Properties properties;
public void perform() {
System.out.println("name : " + name);
System.out.println("age : " + age);
poem.recite();
for (String val : list)
System.out.println("in list : " + val);
for (Entry<String, String> entry : map.entrySet())
System.out.println("in map : " + entry.getKey() + " -- " + entry.getValue());
for (Entry<Object, Object> entry : properties.entrySet())
System.out.println("in properties : " + entry.getKey() + " -- " + entry.getValue());
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-idol.xml");
MoonlightPoet moonlightPoet = (MoonlightPoet) context.getBean("moonlightPoet");
moonlightPoet.perform();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Poem getPoem() {
return poem;
}
public void setPoem(Poem poem) {
this.poem = poem;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
该bean在xml文件中定义如下:
<bean id="moonlightPoet" class="com.moonlit.myspring.MoonlightPoet">
<property name="name" value="moonlit" />
<property name="age" value="22" />
<property name="poem" ref="sonnet29" />
<property name="list">
<list>
<value>hello</value>
<value>world</value>
<!-- if bean, use <ref bean="XX"> -->
</list>
</property>
<property name="map">
<map>
<entry key="key1" value="value1" />
<entry key="key2" value="value2" />
<entry key="key3" value="value3" />
</map>
</property>
<property name="properties">
<props>
<prop key="GUITAR">STRUM STRUM STRUM</prop>
<prop key="CYMBAL">CRASH CRASH CRASH</prop>
<prop key="HARMONICA">HUM HUM HUM</prop>
</props>
</property>
</bean>
输出结果:
name : moonlit
age : 22
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself, and curse my fate,
Wishing me like to one more rich in hope,
Featur'd like him, like him with friends possess'd,
Desiring this man's art and that man's scope,
With what I most enjoy contented least;
Yet in these thoughts myself almost despising,
Haply I think on thee, and then my state,
Like to the lark at break of day arising
From sullen earth, sings hymns at heaven's gate;
For thy sweet love remember'd such wealth brings
That then I scorn to change my state with kings.
in list : hello
in list : world
in map : key1 -- value1
in map : key2 -- value2
in map : key3 -- value3
in properties : HARMONICA -- HUM HUM HUM
in properties : CYMBAL -- CRASH CRASH CRASH
in properties : GUITAR -- STRUM STRUM STRUM
理解:
注入简单值:
<property name="XX" value="YY" />
其中XX是变量名,YY是值。
引用其他Bean:
<property name="XX" ref="YY">
其中XX是变量名,YY是引用的bean的id。
也可以注入内部类:
<property name="XX">
<bean class="YY" />
</preperty>
其中XX是变量名,YY是内部类对应的类名。
装配List、Set和Array:
<property name="XX">
<value>YY</value>
或者
<ref bean="ZZ">
</property>
其中XX是变量名,YY是值,ZZ是引用的bean。
装配Map:
<map>
<entry key="XX" value="YY" />
或者
<entry key="XX" value-ref="YY" />
或者
<entry key-ref="XX" value="YY" />
或者
<entry key-ref="XX" value-ref="YY" />
</map>
因为map的key和value可以对应一个基础类型的值,也可以对应一个bean,所以key,value对应值,key-ref,value-ref对应bean。
装配Properties集合:
<property name="XX">
<props>
<prop key="AA">BB</prop>
....
</props>
</property>
其中AA对应key,BB对应value。
装配空值:使用<null/>元素。
使用Spring的命名空间p装配属性
我们可以在beans中添加
xmlns:p="http:www.springframework.org/schema/beans"
来使用p:作为<bean>元素所有属性的前缀来装配Bean的属性。用法如下:
<bean id="kenny" class="XX"
p:song = "Jingle Bells"
p:instrument-ref = "saxphone" />
-ref后缀作为一个标识来告知Spring应该装配一个引用而不是字面值。
Spring学习笔记--注入Bean属性的更多相关文章
- spring学习笔记之---bean属性注入
bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...
- Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)
在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...
- Spring学习笔记—装配Bean
在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...
- Spring学习笔记(3)——Bean的注入方式
依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...
- Spring学习笔记之Bean的一些属性设置
1.beans 里边配置default-init-method="shunge",有这个方法的会执行,没有也不会报错 2.beans 里边配置default-destroy-met ...
- Spring学习笔记(2)——Bean的配置
要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...
- Spring学习笔记之bean配置
1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...
- Spring学习笔记之Bean的实例化
一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...
- Spring学习笔记——02 Bean的命名及实例化
一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...
随机推荐
- C++笔记 4
1.类和对象 类就是对对象的描述,主要从属性和行为两个方面描述. 对于属性一般作成private , 行为作为public 函数 (1)构造函数,初始化所有的成员变量,系统自动调用,可以重 ...
- CocosIDE导出Android APK的注意事项
近期在用CocosIDE来开发新的游戏,整体感觉非常不错.支持断点调试.真机调试,调试时候的变量信息也比非常多vs的lua插件丰富.用起来也比一些专门的lua调试工具要方便.并且有一定的语法差错功能. ...
- 获取本地IP和mac等信息
1获取mac protected string getHostMacName() { string mac = ""; ManagementClass mc; mc = new M ...
- vshare
vshare 基于百度分享开发的支持VUE2.X的分享插件,为您带来更多的流量!提供多种风格按钮,代码加载更快,引入社会化流量,提升网页抓取速度等优点.github地址:https://github. ...
- Python中的字符串操作总结(Python3.6.1版本)
Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...
- 支持向量机(SVM)(三)-- 最优间隔分类器(optimal margin classifier)
在之前为了寻找最有分类器,我们提出了例如以下优化问题: 在这里我们能够把约束条件改写成例如以下: 首先我们看以下的图示: 非常显然我们能够看出实线是最大间隔超平面,如果×号的是正例,圆圈的是负例.在虚 ...
- DataGridView:根据条件改变单元格的颜色
根据条件改变DataGridView行的颜色可以使用RowPrePaint事件. 示例程序界面如下: 示例程序代码如下: using System; using System.Collections. ...
- Linux - tar命令 压缩 和 解压
压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg //将目录里所有jpg文件打包成jpg.tar后 ...
- Oracle的sql语句中关键字冲突用双引号
select distinc user from instrument where created>"TO_DATE"('2015-02-05 12:00:00', 'yyy ...
- 看了这个才发现jQuery源代码不是那么晦涩
很多人觉得jquery.ext等一些开源js源代码 十分的晦涩,读不懂,遇到问题需要调试也很费劲.其实我个人感觉主要是有几个方面的原因: 1.对一些js不常用的语法.操作符不熟悉 2.某个functi ...