Spring @PropertySource Read Resource file

Source: Internet
Author: User

The configuration of files that need to read the resource files often appears in the project

spring3.1 start to open @ @PropertySource annotations, you can quickly read to the resource file, with the use of environment can quickly read to the required data.

Added a jar package to spring in the 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 Profile

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 Config 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"));

}

}

The properties file is read in by @propertysource (value = {"Classpath:/pro/app.properties"}). With

@Autowired
Environment env;

Env.getproperty ("Disc.title") can read the contents of the resource file in a timely manner.

If you need a pattern with a placeholder (${"Disc.title"}) you need to inject

@Bean
public static Propertysourcesplaceholderconfigurer Propertysourcesplaceholderconfigurer () {
return new Propertysourcesplaceholderconfigurer ();
}

To test

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

Spring @PropertySource Read resource file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.