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 ...
随机推荐
- egret学习记录
最近h5小游戏比较流行,本来我是做cocos2dx的,一开始想用它的js版. 可惜看着js真是头大.于是选择了egret,egret采用typescript,学过面向对象的,上手还是比较快的,而且ap ...
- C# 回调与 javascritp 回调 比较
C#: using System; using System.Collections.Generic; using System.Text; namespace Delegate { //定义委托,它 ...
- SQL2005 全文检索超时的解决方案
如果没有 Internet 连接的服务器上运行的 SQL Server 2005 实例中运行全文查询时,您可能会遇到 45 秒钟的延迟 详细的解释请查看http://support.microsoft ...
- kubernetes namespace
namespace 通常用来划分项目 默认kubectl命令 操作的namespace是default kube-system是k8s的系统组件namespaces 切换namespaces: 查看配 ...
- JAVA的包装类2 【转】
怎么理解JAVA中的“包装类” JAVA是一种面向对象语言,java中的类把方法与数据连接在一起,构成了自包含式的处理单元.但在JAVA中不能定义基本类型(primitive type)对象,为了能 ...
- 获取FirefoxProfile配置文件以及使用方法介绍
使用默认方式构建的(WebDriver)FirefoxDriver实例: WebDriver driver = new FirefoxDriver(); 这种方式下,打开的Firefox浏览器将是不带 ...
- Android isUserAMonkey()
Monkey是Android上的一个自动化测试工具.产生随机事件由于压力测试等. ActivityManager.isUserAMonkey()判断当前是否有运行的Monkey测试.有就返回true. ...
- map用法小例子
一. Map< Key , Value > m_Eg; 一般赋值表示成: TypeElem value; m_Eg[key] = value; 或 m_Eg.insert(make_ ...
- ffmpeg h264+ts +udp传输
http://bbs.csdn.net/topics/370246456 http://1229363.blog.163.com/blog/static/19743427201001244711137 ...
- linux -- Linux下的五个查找命令:grep、find、locate、whereis、which
1.grep grep(General Regular Expression Parser,通用规则表达式分析程序)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 它的使 ...