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 ...
随机推荐
- php memcached在windows上的使用
php的memcached是比memcache,效率更高的memcache缓存扩展. 然而windows下并没有这个扩展,于是做单元测试时要把代码上传到linux服务器,再运行,甚是麻烦. (当然另外 ...
- 【C#】解析XML
最近在尝试用WPF搞点桌面小应用. C#中使用System.Xml.XmlDocument类对XML文件进行操作. 文档详情如下: https://msdn.microsoft.com/en-us/l ...
- Remote Desktop Connection没法全屏解决方案
Remote Desktop Connection无法全屏解决方案Sometimes, Remote Desktop Connection总是一个窗口,不自动全屏,任务栏不能自动隐藏起来,要拖动滚动条 ...
- linux下使用yum安装telnet
参考文章: http://futeng.iteye.com/blog/2039490?utm_source=tuicool&utm_medium=referral
- FastCGI 进程管理器(FPM)
FPM(FastCGI 进程管理器)用于替换 PHP FastCGI 的大部分附加功能,对于高负载网站是非常有用的. 它的功能包括: 支持平滑停止/启动的高级进程管理功能: 可以工作于不同的 uid/ ...
- 【转】MFC 无边框窗口的拖动
MFC中无边框窗口的拖动 void CXXXXDialog::OnLButtonDown(UINT nFlags, CPoint point) { PostMessage(WM_NCLBUTTONDO ...
- Spring Boot 官方文档学习(二)特点
一.SpringApplication banner,就是启动时输出的信息,可以在classpath下添加 banner.txt,或者设置 banner.location 来指向特定的文件.(默认编码 ...
- e636. Listening to All Key Events Before Delivery to Focused Component
Registering a key event dispatcher with the keyboard focus manager allows you to see all key events ...
- linux -- ubuntu dash bash
终端:即所谓的命令行界面,又称命令终端,用户输入shell命令用的窗口,跟Windows里的DOS界面差不多. shell:意为“壳”,是操作系统与用户交互用的接口,在命令终端里可以使用shell.s ...
- Ubuntu 12.04.3 X64 使用 NFS 作为文件共享存储方式 安装 Oracle11g RAC
nfs-server 在 Ubuntu上可以选择 : nfs-kernel-server:如果在windows上,可以选择:haneWIN NFS Server nfs-client Ubuntu上使 ...