一、用placeholder给bean运行时注入值的步骤

Spring取得placeholder的值是用${...}

1.声明placeholder bean

(1)java方式

In order to use placeholder values, you must configure either a PropertyPlaceholder-
Configurer bean or a PropertySourcesPlaceholderConfigurer bean. Starting with
Spring 3.1, PropertySourcesPlaceholderConfigurer is preferred because it resolves
placeholders against the Spring Environment and its set of property sources.
The following @Bean method configures PropertySourcesPlaceholderConfigurer
in Java configuration:

@Bean
public
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

(2)xml方式

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:property-placeholder
location="com/soundsystem/app.properties" /> <bean class="com.soundsystem.BlankDisc"
c:_0 = "${disc.title}"
c:_1 = "${disc.artist}"/> </beans>

2.注入

1.在java文件中用 @Value

如构造函数

public BlankDisc(
@Value("${disc.title}") String title,
@Value("${disc.artist}") String artist) {
this.title = title;
this.artist = artist;
}

2.xml

<bean id="sgtPeppers"
class="soundsystem.BlankDisc"
c:_title="${disc.title}"
c:_artist="${disc.artist}" />

最新文章

  1. Window10可用的转串口驱动CH340
  2. mysql入门1
  3. noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T11——T20
  4. Android 自定义View 三板斧之一——继承现有控件
  5. Spring Cache和MyBatis的使用
  6. weblogic &lt;BEA-000438&gt;
  7. RSA
  8. poj1160 dp
  9. 在yii中使用gearman
  10. poj1190 生日蛋糕 dfs
  11. Spring Boot的启动器Starter详解
  12. jquery列队动画简单演示
  13. Maven快速指南
  14. python爬虫之基本知识
  15. 第二篇--上传git 代码
  16. 爬虫基础(一)-----request模块的使用
  17. Eclipse使用Maven,创建项目出现:Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resour
  18. Shell学习之环境变量配置文件(三)
  19. 我的博客:C# PHP J2ee Java Android js WP Asp.net mvc Python
  20. Inter exchange Client Address Protocol (ICAP)- 互换客户端地址协议

热门文章

  1. hibernate--could not initialize proxy - no Session--懒加载问题
  2. 树莓派配置AP模式
  3. and or判别
  4. touches,motion触摸事件响应
  5. XML, XPath, Xslt及解析/Parse
  6. STL标准模板库 向量容器(vector)
  7. 学习C++ Primer 的个人理解(九)
  8. Ubuntu 14.04 eclipse 提示框背景色更改
  9. php header示例代码(推荐)
  10. java nio使用方法(转)