spring @PropertySource 读取资源文件

项目中经常出现需要读取资源文件进行文件的配置

spring3.1开始开启@@PropertySource注解,可以很快的读取到资源文件,配合Environment的使用可以很快的读取到所需要的数据。

在pom配置文件中增加了对spring的jar包

<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>

增加properties资源配置文件

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

增加service文件

package com.music.service;

import org.springframework.stereotype.Component;


@Component
public class CompactDiscServiceImpl implements CompactDiscService{

public String name= "hello";

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

}

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) ;
}
}

猜你喜欢

转载自www.cnblogs.com/a969695791/p/8983033.html