spring @PropertySource 讀取資源文件

項目中常常出現須要讀取資源文件進行文件的配置java

spring3.1開始開啓@@PropertySource註解,能夠很快的讀取到資源文件,配合Environment的使用能夠很快的讀取到所須要的數據。web

在pom配置文件中增長了對spring的jar包spring

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zky</groupId>
<artifactId>hello</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hello Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>hello</finalName>
</build>

</project>apache

增長properties資源配置文件api

disc.title=just test properties
disc.context=i want to studymvc

增長service文件app

package com.music.service;dom

import org.springframework.stereotype.Component;maven


@Component
public class CompactDiscServiceImpl implements CompactDiscService{

public String name= "hello";

public void play() {
System.out.println("hello i go to study"+Math.random());ide

}

public CompactDiscServiceImpl(){
}

public CompactDiscServiceImpl(String title,String desc){

System.out.println(title+"%%%%%"+desc);
}

}

增長項目配置文件Config

package com.music;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.music.service.CompactDiscServiceImpl;
@Configuration
@ComponentScan
@PropertySource(value = { "classpath:/pro/app.properties"})
public class MusicConfig {
@Autowired
Environment env;
@Bean
public CompactDiscServiceImpl getDesc(){
return new CompactDiscServiceImpl(env.getProperty("disc.title"),env.getProperty("disc.desc"));

}

}

 

經過@PropertySource(value = { "classpath:/pro/app.properties"})將properties文件讀入。配合

@Autowired
Environment env;

的env.getProperty("disc.title")可以及時讀取到資源文件的內容。

若是須要採用佔位符(${"disc.title"})的模式則須要注入

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

 

進行測試

package music;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.music.MusicConfig;
import com.music.service.CompactDiscService;
import com.music.service.CompactDiscServiceImpl;

 

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=MusicConfig.class)public class MusicTest {/* @Autowired private CompactDiscService musicService;*/ @Autowired private CompactDiscServiceImpl compactDiscServiceImpl; @Test public void name() { compactDiscServiceImpl.play(); //musicService.play(); //assertNotNull(musicService) ; }}

相關文章
相關標籤/搜索