美文网首页
spring boot 读取新增配置文件信息

spring boot 读取新增配置文件信息

作者: 周六不算加班 | 来源:发表于2018-12-04 14:13 被阅读19次

    1、在resources文件下创建配置文件database.properties

    mongodb.uri=xxxxxxxxxx
    mongodb.username=ceshi
    mongodb.password=123456
    mongodb.schema=xxxxx
    

    2、在mogodb配置方法中读取配置文件信息

    @Configuration  //等价于XML中配置bean
    @PropertySource(value = "classpath:database.properties",ignoreResourceNotFound = true)
    public class MongoDBConfig {
        @Value("${mongodb.schema}")
        private String databaseName;
    
        @Value("${mongodb.uri}")
        private String uri;
    
        @Value("${mongodb.username}")
        private String userName;
    
        @Value("${mongodb.password}")
        private String password;
    
        @Bean
        public MongoDbFactory mongoDbFactory() throws UnknownHostException {
            String uriStr="mongodb://"+userName+":"+password+"@"+uri+"/"+databaseName;
            System.out.println(uriStr);
            MongoClientURI mongoClientURI=new MongoClientURI(uriStr);
            MongoDbFactory mongoDbFactory=new SimpleMongoDbFactory(mongoClientURI);
            return mongoDbFactory;
        }
    }

    相关文章

      网友评论

          本文标题:spring boot 读取新增配置文件信息

          本文链接:https://www.haomeiwen.com/subject/hrvmcqtx.html