Spring uses @Value to inject properties file properties

This article is a simple record of Spring's use of @Value to directly inject properties in the properties file

1. The overall directory structure is as follows



2. pom 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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>chengf</groupId>
	<artifactId>spring-property-test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-property-test</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.5.RELEASE</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
	</dependencies>
</project>



3. Appconfig class
package chengf.spring.property.test;

import org.springframework.beans.factory.annotation.Value;

/**
 * @author chengf
 *
 */
public class AppConfig
{


	@Value("${name}")
	private String name;
	
	@Value("${age}")
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}



4.properties file

name=chengf
age=20


5. Test class
package chengf.spring.property.test;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration
public class AppConfigTest {
	
	@Configuration
	@PropertySource(value = { "classpath:test.properties" })
	public static class Config {
		
		@Bean
		public AppConfig appConfig() {
			return new AppConfig();
		}
		
		@Bean
		public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer  () {
			return new PropertySourcesPlaceholderConfigurer ();
		}
	}
	
	@Autowired
	private AppConfig appConfig;
	@Test
	public void test() {
		Assert.assertEquals("chengf", appConfig.getName());
		
		Assert.assertEquals(20, appConfig.getAge());
	}
}



The junit test passes, indicating that the properties in properties have been correctly injected through @Value

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705286&siteId=291194637
Recommended