ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

描述

使用@Value映射properties文件属性到Java字段

重点

  • 使用@PropertySource 注解指定*.properties文件位置;
  • 使用@Value进行注入;

my.properties

book.author=ssslinppp
book.name=spring boot


Java类

package com.sssppp;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@PropertySource("classpath:my.properties")
public class Ch522 {

	@Value("${book.author}")
	private String bookAuthor;
	
	@Value("${book.name}")
	private String bookName;

	@RequestMapping("/aa")
	String index() {

		return "book name is:" + bookName + " and book author is:" + bookAuthor;
	}

}

posted on 2017-06-08 17:17  ssslinppp  阅读(518)  评论(0编辑  收藏  举报