Spring中@PropertySource用法说明

书欣 Spring 发布时间:2023-02-01 21:50:34 阅读数:16773 1
下文笔者讲述spring中@PropertySource示例简介说明,如下所示

@PropertySource功能简介

@PropertySource注解的功能:
      用于加载配置文件
	  在具体的类中,使用@value注解即可显示值

@PropertySource和@Value

 
config.properties

mysql.url=8.8.8.8
mysql.db=test


package com.java265.config;
 
import org.springframework.beans.factory.annotation.Value;
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.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@ComponentScan(basePackages = { "com.java265.*" })
@PropertySource("classpath:config.properties")
public class AppConfigmysql {
 
	//8.8.8.8
	@Value("${mysql.url}")
	private String mysqlUrl;
 
	//test
	@Value("${mysql.db}")
	private String defaultDb;
 
	@Bean
	public MongoTemplate mongoTemplate() throws Exception {
		MongoClientOptions mongoOptions = 
			new MongoClientOptions.Builder().maxWaitTime(1000 * 60 * 5).build();
		MongoClient mongo = new MongoClient(mysqlUrl, mongoOptions);
		mysqlFactory mysqlFactory = new SimplemysqlFactory(mongo, defaultDb);
		return new MongoTemplate(mysqlFactory);
	}
 
	//To resolve ${} in @Value
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
		return new PropertySourcesPlaceholderConfigurer();
	}
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202302/5616.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者