Projects often need to read resource files for file configuration
Spring3.1 started to open @@PropertySource annotation, you can quickly read resource files, and with the use of Environment, you can quickly read the required data.
Added spring jar package to pom configuration file
<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>
Add properties resource configuration file
disc.title=just test properties
disc.context=i want to study
Add service file
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);
}
}
Add project configuration file 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"));
}
}
Read in the properties file via @PropertySource(value = { "classpath:/pro/app.properties"}). Cooperate
@Autowired
Environment env;
The env.getProperty("disc.title") can read the content of the resource file in time.
If you need to use a placeholder (${"disc.title"}) pattern, you need to inject
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
carry out testing
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) ;
}
}