分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报

有的时候我们定义了Properties文件,并且使用Spring的PropertyPlaceholderConfigurer类在Spring配置文件中以占位符的方式替换数据库等配置文件,那么万一我们想将这些属性也同时注入到某个bean中呢?

这里做个说明,网上很多资料说的<util:properties>方式我们这里不做介绍,有兴趣的自己去看。我们这里只介绍使用PropertyPlaceholderConfigurer管理起来的方式。

其实很简单,比如你在db.properties中配置的db.driverclass=com.mysql.jdbc.Driver,则在我们的bean的某个set方法上可以用@Value("${db.driverclass}")来完成注入,

也可以在成员变量上注入。

例子代码如:

@Service

public class DatabaseInfo {

private String driverClass;     //也可以在这里注入

private void setDriverClass(String dc) {

this.driverClass = dc;

}

}

Spring中Value注解的使用的更多相关文章

  1. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  2. 全面解析Spring中@ModelAttribute注解的用法

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...

  3. EnableAutoConfiguration注解 Spring中@Import注解的作用和使用

    EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...

  4. Spring中常用注解的介绍

    spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...

  5. Spring中使用注解时启用<context:component-scan/>

    在spring中使用注解方式时需要在spring配置文件中配置组件扫描器:http://blog.csdn.net/j080624/article/details/56277315 <conte ...

  6. Spring中异步注解@Async的使用、原理及使用时可能导致的问题

    前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...

  7. Spring中@Import注解的使用

    Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...

  8. Spring中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service @Service与@Component功能相同,写在ServiceImpl类上 3.@Reposito ...

  9. 【进阶】Spring中的注解与反射

    [进阶]Spring中的注解与反射 目录 [进阶]Spring中的注解与反射 前言 一.内置(常用)注解 1.1@Overrode 1.2@RequestMapping 1.3@RequestBody ...

随机推荐

  1. Android UI开发第三十六篇——使用Volley加载图片列表

    Android开发者可能会使用Universal Image Loader或者Square`s newer Picasso这些第三方的库去处理图片的加载,那么Volley是怎么加载图片列表的呢,这一篇 ...

  2. mysql数据库导入到oracle数据库

    首先,写一个cmd脚本 xx.cmd sqlldr username/password control=xx.ctl errors=10000000 direct=y 再写一个bat脚本xx.bat ...

  3. Canvas-图片填充-预加载

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. c# + Sql server 事务处理

    事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位. 通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便 ...

  5. 安装Vmware ESX Server5.5 ——hardware virtualization is not a feature of the cpu or is not enabled in the BIOS

    Error信息: hardware virtualization is not a feature of the cpu or is not enabled in the BIOS 解决方案: F2进 ...

  6. C# 二进制文件操作(内容搜索、数据截取)

    private void button2_Click(object sender, EventArgs e) { var list = new List<Frame>(); byte[] ...

  7. 【Python之路】第十四篇--jQuery

    jquery简介 1.jquery是什么       ☛ 参考用法 jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. jQuery ...

  8. <2014 05 09> 程序员:从C++转到Java需注意的地方

    最近想玩玩Android的APP开发,从C++角度来学习Java.Java可以说是一个优化精简版的C++,去除了底层C的很多特性.找了这篇文章. --------------------------- ...

  9. Spring MVC 框架结构介绍(二)

    Spring MVC框架结构 Spring MVC是围绕DispatcherServlet设计的,DispatcherServlet向处理程序分发各种请求.处理程序默认基于@Controller和@R ...

  10. 解决ubuntu 无法挂载移动硬盘问题 unknown filesystem type 'exfat'

    Ubuntu 13.10 或以上 安装exfat-fuse: sudo apt-get install exfat-fuse Ubuntu 13.04 或以下 sudo apt-add-reposit ...