这里通过一个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属性的更多相关文章

  1. spring学习笔记之---bean属性注入

    bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...

  2. Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)

    在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...

  3. Spring学习笔记—装配Bean

    在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...

  4. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

  5. Spring学习笔记之Bean的一些属性设置

    1.beans 里边配置default-init-method="shunge",有这个方法的会执行,没有也不会报错 2.beans 里边配置default-destroy-met ...

  6. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  7. Spring学习笔记之bean配置

    1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...

  8. Spring学习笔记之Bean的实例化

    一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...

  9. Spring学习笔记——02 Bean的命名及实例化

    一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...

随机推荐

  1. sql数据类型总结

    一.数字数据类型 bigint int smallint tinyint decimal numeric money smallmoney float real Bit 二.字符数据类型 非unico ...

  2. JS地毯式学习二

    1.递归算法 a. function box(num){if(num<=1){ return 1;}else{ return num*box(num-1);}}alert(box(5)); b. ...

  3. Adnroid 反编译APK

    http://blog.csdn.net/vipzjyno1/article/details/21039349 http://blog.csdn.net/zx19899891/article/deta ...

  4. log4j日志pattern配置

    c category的名称,可使用{n}限制输出的精度.例如:logger名为"a.b.c",%c{2}将输出"b.c". C 产生log事件的java完全限定 ...

  5. 数据库之“on”“where”区别

    数据库在通过连接两张或者多张表返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户 在使用inner join(内连接)没有区别,但是 在使用left jion时,on和where条件的 ...

  6. selenium测试(Java)--学习总结

  7. 在安装ZooKeeper之前,请确保你的系统是在以下任一操作系统上运行

    在安装ZooKeeper之前,请确保你的系统是在以下任一操作系统上运行: 任意Linux OS - 支持开发和部署.适合演示应用程序. Windows OS - 仅支持开发. Mac OS - 仅支持 ...

  8. 【转】C# URL短地址压缩算法及短网址原理解析

    这篇文章主要介绍了C# URL短地址压缩算法及短网址原理解析,本文重点给出了算法代码,需要的朋友可以参考下 短网址应用已经在全国各大微博上开始流行了起来.例如QQ微博的url.cn,新郎的sinaur ...

  9. CentOS 7 Minimal编译安装MySQL5.6

    写在前面,编译安装MySQL的优势:平台无关.可设定参数按需安装.安装的MySQL目录独立(方便清楚).更好的平台耦合及运行性能(很多运维的观点):缺点:编译安装较慢. 一.撤换系统防火墙 注:Cen ...

  10. Python如何输出包含在对象中的中文字符?

    >>> bb = {'classes': ['\xe5\xb0\x96\xe6\xa4\x92\xe5\x9c\x9f\xe8\xb1\x86\xe4\xb8\x9d', '\xe5 ...