博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring学习笔记--注入Bean属性
阅读量:7225 次
发布时间:2019-06-29

本文共 4275 字,大约阅读时间需要 14 分钟。

这里通过一个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
list; private Map
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
entry : map.entrySet()) System.out.println("in map : " + entry.getKey() + " -- " + entry.getValue()); for (Entry
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
getList() { return list; } public void setList(List
list) { this.list = list; } public Map
getMap() { return map; } public void setMap(Map
map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; }}

该bean在xml文件中定义如下:

hello
world
STRUM STRUM STRUM
CRASH CRASH CRASH
HUM HUM HUM

输出结果:

name : moonlitage : 22When, 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 arisingFrom sullen earth, sings hymns at heaven's gate;For thy sweet love remember'd such wealth bringsThat then I scorn to change my state with kings.in list : helloin list : worldin map : key1 -- value1in map : key2 -- value2in map : key3 -- value3in properties : HARMONICA -- HUM HUM HUMin properties : CYMBAL -- CRASH CRASH CRASHin 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的属性。用法如下:

-ref后缀作为一个标识来告知Spring应该装配一个引用而不是字面值。

转载地址:http://pfkfm.baihongyu.com/

你可能感兴趣的文章
MyCAT水平分库
查看>>
基于django的视频点播网站开发-step3-注册登录功能 ...
查看>>
进程与线程(三)——进程/线程间通信
查看>>
扩展资源服务器解决oauth2 性能瓶颈
查看>>
数据可视化之下发图实践
查看>>
如何用纯 CSS 创作一个记事本翻页动画
查看>>
微信公众平台生成二维码海报是如何做到的?
查看>>
2017-11-28 在线编程网站对中文代码的支持
查看>>
浅谈k8s cni 插件
查看>>
AES加密算法的JAVA实现
查看>>
面试系列-高并发之synchronized
查看>>
JAVA8给我带了什么——lambda表达
查看>>
我们在编写python代码时应该注意那几件事 !
查看>>
微软工程师认为 Mozilla 也应该拥抱 Chromium
查看>>
论文笔记系列-Neural Architecture Search With Reinforcement Learning
查看>>
使用文本框TextView/EditText的开源库清单
查看>>
通过一个实际例子理解Kubernetes里pod的自动scale - 水平自动伸缩
查看>>
手把手教您将 libreoffice 移植到函数计算平台
查看>>
Ansible批量修改root密码(playbook)
查看>>
c#-WPF string,color,brush之间的转换
查看>>