使用Spring注入Properies文件方法:

1、src中新建一个settings.properties文件,内容如下:

db_driverClassName=com.mysql.jdbc.Driver
db_url=jdbc:mysql://127.0.0.1/test
db_username=root
db_password=root test_userName=Robin
test_age=

2、在spring的applicationContext.xml中加入这段:

    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:settings.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false" />
<property name="properties" ref="configProperties" />
</bean>

之后的spring的xml配置中可以直接这样使用了:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db_driverClassName}"/>
<property name="url" value="${db_url}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />
</bean>

3、新建一个class:Settings.java

package com.my.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class Settings {
private static Settings instance;
public static Settings getInstance() {
return instance;
}
public Settings() {
instance = this;
} @Value("#{configProperties['test_userName']}")
private String userName;
@Value("#{configProperties['test_age']}")
private int age; public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

然后可以在代码中这样调用了:

Settings.getInstance().getUserName()

[Spring] - Property注入的更多相关文章

  1. spring 属性注入

    Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...

  2. Spring 依赖注入方式详解

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  3. Spring的注入问题

    作下笔记,Spring的注入问题[多个实例问题] 解决方案如下: package student.life.support.platform.service.impl; import javax.an ...

  4. Spring依赖注入三种方式详解

    在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...

  5. Spring依赖注入 --- 简单使用说明

    Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...

  6. Spring依赖注入 --- 模拟实现

    Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...

  7. Spring依赖注入的三种方式

    看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...

  8. 模拟Spring依赖注入

    通过读取xml文件,利用反射动态加载类和方法,其实就是spring的注入机制模拟,可以清晰的看出整个运行流程 1.配置文件 applicationContext.xml <beans> & ...

  9. Spring、Spring依赖注入与编码剖析Spring依赖注入的原理

    Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...

随机推荐

  1. 获取ip

    需要引用System.Web http://stackoverflow.com/questions/4879837/smart-way-to-get-the-public-internet-ip-ad ...

  2. ERROR 1018 (HY000): Can't read dir of './test/' (errno: 13)

    不能查看mysql中数据库的表. 一.查看 mysql> desc test; ERROR 1046 (3D000): No database selected mysql> use te ...

  3. java高薪之路__004_泛型

    参考地址: 1. http://www.cnblogs.com/lwbqqyumidi/p/3837629.html2. http://www.cnblogs.com/abcwt112/p/47350 ...

  4. 火狐浏览器Firefox上DownThemAll插件

    DownThemAll插件支持断点续传.多线程下载,可以大幅度提高下载速度. 在Windows平台上,要下载大量的文件,迅雷自然是首选:但在非Windows平台上,只要安装一个火狐浏览器,再安装Dow ...

  5. 基础知识复习(一)——C语言位运算符详解

    常用的位运算符:与(&),取反(~),或(|),异或(^),左移(«),右移(») 1. 与(&)操作符,按位与,全为1 时,结果取1 11001 &10011 结果:1000 ...

  6. 【安全测试】burpsuite安装方法

    burp suite需要安装Java环境才可以运行,最好安装jdk1.6以上版本. 1.将jdk安装路径添加到环境变量-path里,加到bin即可: C:\Program Files\Java\jdk ...

  7. C++中 接口的定义 COM

    首先定义一个虚基类的接口,其中包含虚函数AddRef Release QueryInterface,(MFC 类IUnKnown unknwn.h)分别是增加减去引用计数和查询接口然后定义一个实现类, ...

  8. update 多表

    update energylog set value=(a.value+c.value)/2from energylog as a, energylog as cwhere a.idvariable= ...

  9. MongoDB整库备份与还原以及单个collection备份、恢复方法

    mongodb数据库维护离不开必要的备份.恢复操作,而且一般不会出错,所以我们在使用的时候大部分时候使用备份和恢复操作就可以了   mongodump.exe备份的原理是通过一次查询获取当前服务器快照 ...

  10. ITPUB网站的知识索引汇总

    1. ITPUB知识索引树 http://www.itpub.net/tree/ http://www.itpub.net/pubtree/index.htm 2. ITPUB知识索引贴——全文索引 ...