Spring中@PropertySource用法说明
下文笔者讲述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(); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。