【spring boot】使用@Value映射properties文件属性

2022-12-07,,,,

描述

使用@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;
} }

【spring boot】使用@Value映射properties文件属性的相关教程结束。

《【spring boot】使用@Value映射properties文件属性.doc》

下载本文的Word格式文档,以方便收藏与打印。