Spring学习(14)--- 基于Java类的配置Bean 之 @ImportResource & @Value 注解

学习如何使用@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

 这下就可以了

 

posted @ 2015-07-07 17:28  Json_wangqiang  阅读(5850)  评论(1编辑  收藏  举报