学习如何使用@ImportResource 和 @Value 注解进行资源文件读取

例子:

先创建一个MyDriverManager类(模拟读取数据库配置信息)

package com.beanannotation;

public class MyDriverManager {

	public MyDriverManager(String url,String username,String password){
System.out.println("url : "+url);
System.out.println("username : "+username);
System.out.println("password : "+password);
}
}

创建StoreConfig

package com.beanannotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; @Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig { @Value("${url}")
private String url; @Value("${username}")
private String username; @Value("${password}")
private String password; @Bean
public MyDriverManager myDriverManager(){
return new MyDriverManager(url,username,password);
}
}

XML配置(context:property-placeholder 指定资源文件的位置)

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:property-placeholder location="classpath:config.properties"/> <context:component-scan base-package="com.beanannotation">
</context:component-scan> </beans>

创建资源文件config.properties

url=127.0.0.1
username=root
password=123456

单元测试:

package com.beanannotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UnitTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beanannotation.xml");
MyDriverManager service=(MyDriverManager)context.getBean("myDriverManager");
System.out.println(service.getClass().getName()); }
}

结果:

2015-7-7 17:22:25 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@36b8bef7: startup date [Tue Jul 07 17:22:25 CST 2015]; root of context hierarchy
2015-7-7 17:22:25 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
2015-7-7 17:22:25 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
2015-7-7 17:22:25 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [config.properties]
2015-7-7 17:22:25 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [config.properties]
url : 127.0.0.1
username : Administrator
password : 123456
com.beanannotation.MyDriverManager

PS:有没有发现结果有异常----username 不是root 而是计算机的当前用户名

@Value("${username}") 在取值的时候,会去取当前用户的名称

所以在以后的使用中,要避免使用username

所以,修改一下config.properties 和StoreConfig

jdbc.url=127.0.0.1
jdbc.username=root
jdbc.password=123456
package com.beanannotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; @Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig { @Value("${jdbc.url}")
private String url; @Value("${jdbc.username}")
private String username; @Value("${jdbc.password}")
private String password; @Bean
public MyDriverManager myDriverManager(){
return new MyDriverManager(url,username,password);
}
}

测试同上:

结果:

2015-7-7 17:26:41 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32bf7190: startup date [Tue Jul 07 17:26:41 CST 2015]; root of context hierarchy
2015-7-7 17:26:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
2015-7-7 17:26:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
2015-7-7 17:26:41 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [config.properties]
2015-7-7 17:26:41 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [config.properties]
url : 127.0.0.1
username : root
password : 123456
com.beanannotation.MyDriverManager

这下就可以了

Spring学习(14)--- 基于Java类的配置Bean 之 @ImportResource & @Value 注解的更多相关文章

  1. Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解

    基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...

  2. Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)

    例子: 定义泛型Store package javabased; public interface Store<T> { } 两个实现类StringStore,IntegerStore p ...

  3. Spring学习(15)--- 基于Java类的配置Bean 之 @Bean & @Scope 注解

    默认@Bean是单例的,但可以使用@Scope注解来覆盖此如下: @Configuration public class MyConfiguration { @Bean @Scope("pr ...

  4. Spring IoC — 基于Java类的配置

    普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息. 基于Java类的配置方法和基 ...

  5. Spring使用注解开发及使用java类进行配置bean

    Spring使用注解开发 说明 在spring4之后,想要使用注解形式,必须得要引入aop的包 在配置文件当中,还得要引入一个context约束 <?xml version="1.0& ...

  6. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...

  7. Spring学习(4)IOC容器配置bean:定义与实例化

    一.  IOC容器配置 1. 一些概念 (1)IOC容器: 定义:具有管理对象和管理对象之间的依赖关系的容器. 作用:应用程序无需自己创建对象,对象由IOC容器创建并组装.BeanFactory是IO ...

  8. 《精通Spring4.X企业应用开发实战》读后感第五章(基于Java类的配置)

  9. 开涛spring3(12.4) - 零配置 之 12.4 基于Java类定义Bean配置元数据

    12.4  基于Java类定义Bean配置元数据 12.4.1  概述 基于Java类定义Bean配置元数据,其实就是通过Java类定义Spring配置元数据,且直接消除XML配置文件. 基于Java ...

随机推荐

  1. angular 实现自定义样式下拉菜单

    自己闲着没事写了一个自定义的下拉菜单希望和大家交流一下!望能和大神们成为朋友. 下面上代码: <!doctype html> <html lang="en" ng ...

  2. Excception and Error

    exception and error都是继承throwable类; Exception就是程序中出现的异常,程序会去捕获: 但是error是比较严重的错误,程序是不会去捕获的: erroe:一般都是 ...

  3. Laravel5中Cookie的使用

    今天在Laravel框架中使用Cookie的时候,碰到了点问题,自己被迷糊折腾了半多小时.期间研究了Cookie的实现类,也在网站找了许多的资料,包括问答.发现并没有解决问题.网上的答案都是互相抄袭, ...

  4. WebGIS开源解决方案之矢量数据导入

    前几篇介绍了开源WebGIS开发环境的搭建,本篇开始陆续介绍这些软件的使用,WebGIS的开发,首要的问题是解决数据来源,本篇主要介绍矢量数据在开源空间数据库PostgreSQL中的存储.后续篇幅中再 ...

  5. 软件开发的一些"心法"

    从事软件开发也有好几年了,和一开始那个懵懵懂懂的小菜鸟相比,自己也感觉到了一些变化. 也许是熟能生巧, 趟过很多坑,但核心的绝不是这些细节的东西. 打个比方,如果说对某种语言的特性和技巧的掌握属于身法 ...

  6. python中从文件中读取数据

    # average5.py def main(): fileName = input("What file are the numbers in?") infile = open( ...

  7. POJ 1007

    DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 83069 Accepted: 33428 Descrip ...

  8. JAVA 基础之Integer

    jdk1.5后增加了自动拆箱和自动装箱特性.java的八种 byte,short,int,long,float,double,char,boolean基本类型和各自对应的包装类型的相互转化. 装箱指的 ...

  9. robotframe 学习笔记(之一)

    在robot framework中,通过 Set variable关键字来定义变量 连接对象: 通过Catenate关键字可以连接多个信息 加上"SEPARATOR=",可以对多个 ...

  10. Listener与Filter

    一.监听器Listener javaEE的13们规范中 包括servlet技术和jsp技术 servlet规范中包括三门技术:(servlet的三大组件) servelt技术  Listener技术 ...