`

Spring 利用@Value注入properties文件属性

阅读更多
本编文章是对Spring利用@Value来直接注入properties文件中属性的简单记录

1.整体目录结构如下



2.pom文件

<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类
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 文件

name=chengf
age=20


5.测试类
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());
	}
}



junit 测试通过,表明已经通过@Value正确注入了properties中的属性
  • 大小: 10.2 KB
分享到:
评论

相关推荐

    Spring 自定义注解注入properties文件的值jar包

    Spring 自定义注解注入properties文件的值jar包,下面为使用方法 在xml配置文件中,这样加载properties文件 ...

    spring杂谈 作者zhang KaiTao

    1.19 @Value注入Properties 错误的使用案例 1.20 @Value注入Properties 使用错误的案例 1.21 扩展SpringMVC以支持更精准的数据绑定 1.22 扩展SpringMVC以支持更精准的数据绑定1 1.23 扩展SpringMVC以支持绑定JSON...

    api-algafood:涵盖了Spring Rest专家课程(进行中)技术。 Spring和依赖注入使用针对REST API的主要Spring生态系统项目,例如Spring Framework,Spring MVC,Spring Boot,Spring Data JPA,Spring Security OAuth和Spring HATEOAS。 还了解依赖注入如何与Spring,IoC容器,具有@ Component,@ Configuration和@Bean的bean定义,注入点,与@ Primary

    api-藻类食品 涵盖了Spring Rest专家课程(进行中)... 使用Spring概要文件,按概要文件配置application.properties,属性@Value和@ConfigurationProperties。 带有OpenAPI的JPA,Hibernate和Flyway域驱动设计(DDD)文

    SpringBoot:springboot基础服务

    2.2其他的属性文件需要使用@PropertySource(“ classpath:/config/cs.properties”)来指定文件的位置(只要存在该注解即可),然后再使用@Value注入; 2.3将属性文件装配成一个bean,@ ConfigurationProperties...

    spring applicationContext 配置文件

    &lt;description&gt;Spring公共配置文件 &lt;!-- mes 的數據庫 --&gt; &lt;property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/&gt; &lt;property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:...

    Spring.net框架

    我们的Factory就是利用这种方式根据配置文件动态加载程序集,动态创建对象并设置属性的。有了这个Factory,MainApp中的内容就很简单了: using System; namespace IocInCSharp { public class MainApp { public ...

    一文总结Spring 注解及作用详解

    文章目录开启注解扫描配置@RequestMapping声明bean的注解注入bean的注解spring配置文件中读取properties配置文件j配置类相关注解切面(AOP)相关注解@Bean的属性支持@Value注解环境切换异步相关定时任务相关@Enable*...

    springmybatis

    2. 设置mybatis 配置文件:Configuration.xml, 在src_user目录下建立此文件,内容如下: 程序代码 程序代码 &lt;!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ...

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    &lt;value&gt;WEB-INF/jdbc.properties&lt;/value&gt; class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"&gt; &lt;value&gt;${datasource.driverClassName}...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    ssh(structs,spring,hibernate)框架中的上传下载

    第11~25行定义了Hibernate的会话工厂,会话工厂类用Spring提供的LocalSessionFactoryBean维护,它注入了数据源和资源映射文件,此外还通过一些键值对设置了Hibernate所需的属性。  其中第16行通过类路径的映射方式...

    spring3.2+strut2+hibernate4

    &lt;value&gt;classpath*:jdbc.properties&lt;/value&gt; &lt;!-- 数据源配置,主要用于开发测试环境 --&gt; &lt;!-- &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; --&gt; ...

    carcar.sql

    org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值. 3.编写DEMO 1.控制器 @Controller public class HelloController { private ...

    report 开源的访问层中间件,基于java平台

    a)自定义配置文件需采用指定资源地址:report/report-config.properties b)REPORT_SWITCH:true 则开启,反之关闭 默认开启 c)FILETER:自定义过滤器,多个以逗号隔开 d)JSON_BOAY:json数据请求方式key名称,默认为...

    SpringMVC+Hibernate全注解整合

    &lt;value&gt;classpath:properties/jdbc.properties&lt;/value&gt; &lt;!-- 配置數據源 --&gt; destroy-method="close"&gt; &lt;property name="driverClassName" value="${jdbc.driver}" /&gt; &lt;property name="url" value=...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public class ...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    现在,Java EE 后端与 Flex 前端的接口已经定义好了,要完成 Java EE 后端的接口实现类非常容易,利用 Spring 强大的依赖注入功能,可以通过几行简单的代码完成: 清单 2. FlexServiceImpl class public ...

    Activiti6.0教程例子下载

    然后,按上面说的,把database里的create文件夹里的数据库文件导入你自己的数据库(如果没有修改db.properties,就不用导入了)。 Demo的具体解释与数据库配置的具体信息详见官方手册,手册已经说的很清楚了。这里...

    Sosoo 1.0网络爬虫程序.doc

    通过上述对功能的定制,我们可以看到在应用中我们对sosoo的编程接口并不多,而且目前系统都是基于set的方式注入aop注入对象,这样很容易和spring等基于set方式的依赖注入(IOC)框架集成。 1.Roboter类,spider...

    社区协作配置标准Tamaya.zip

    属性通常会以key = value的形式进行指定,这些属性会以文件的形式来提供并且会加载到一个Java Properties对象中。令人遗憾的是,OSGi、Spring、Java EE、SE以及其他在Java中运行的框架和解决方案都提供了自己的配置...

Global site tag (gtag.js) - Google Analytics