【问题标题】:How to override the defaults provided by SPRING BOOT Autoconfiguration如何覆盖 SPRING BOOT 自动配置提供的默认值
【发布时间】:2014-12-03 04:14:00
【问题描述】:

我对 Spring 2.5 开发和 java 有一定的了解,但我正在尝试更多地了解现代 Spring,包括 Boot 和 Data。我在看项目:

https://github.com/spring-projects/spring-data-examples/tree/master/rest/multi-store

我的机器上有这个示例,但我想覆盖示例的某些方面。例如,我想指定我自己的 mongo 数据库服务器和要在项目中使用的名称。看起来要做的事情是创建一个指定 mongo 数据源的新 bean,但我不确定在这种情况下如何做。

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import example.person.Person;
import example.person.PersonRepository;
import example.treasure.Treasure;
import example.treasure.TreasureRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import javax.activation.DataSource;
import javax.annotation.PostConstruct;
import java.net.UnknownHostException;

/**
 * Application configuration file. Used for bootstrap and data setup.
 *
 * @author Greg Turnquist
 * @author Oliver Gierke
 */
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableMongoRepositories
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired PersonRepository personRepository;
    @Autowired TreasureRepository treasureRepository;

    @PostConstruct
    void checkitOut() {

        personRepository.save(new Person("Frodo", "Baggins"));
        personRepository.save(new Person("Bilbo", "Baggins"));

        for (Person person : personRepository.findAll()) {
            log.info("Hello " + person.toString());
        }

        treasureRepository.deleteAll();
        treasureRepository.save(new Treasure("Sting", "Made by the Elves"));
        treasureRepository.save(new Treasure("Sauron's ring", "One ring to rule them all"));

        for (Treasure treasure : treasureRepository.findAll()) {
            log.info("Found treasure " + treasure.toString());
        }
    }
}

【问题讨论】:

标签: java spring mongodb spring-data spring-boot


【解决方案1】:

参见 MongoRepositoriesAutoConfiguration,这是自动配置发生的地方。

你可以使用 Spring Boot 的属性机制来覆盖属性:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

在您的情况下,只需创建一个 application.properties 文件,其中包含您要覆盖的属性:例如。 spring.data.mongo.host=somehost

干杯, 马库斯

【讨论】:

  • 你能扩展一下吗。实际上,我的 src\main\resources 文件夹中已经有一个 application.properties 文件,我用它来更改服务器的默认端口。它包含: server.port=8765 spring.data.mongo.host=somehost 但未使用 mongo 变量。我应该在java中添加一些东西来启用它吗?我在哪里可以获得可以设置的属性列表?
  • 在回答我评论的第二部分时,我刚刚找到了这个很棒的列表,我仍然不确定为什么主机部分被忽略:docs.spring.io/spring-boot/docs/current/reference/html/…
  • 查看类:org.springframework.boot.autoconfigure.data.MongoProperties 了解可用属性。
  • 似乎前缀改变了,在我的版本中是 spring.data.mongo 而不是 spring.data.mongodb
  • 我仍然不确定如何更改创建的数据库名称 - 默认值似乎是“测试” - 也许这是 mongodb 中的默认值?
【解决方案2】:

实际属性在我链接到http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html的文档中

Markus 的建议中有一个错字 - 它是 spring.data.mongodb。*

我仍然不确定如何更改创建的数据库名称 - 默认值似乎是“测试” - 也许这是 mongodb 中的默认值?

【讨论】:

  • “test”名称似乎被指定为“spring.data.mongodb.uri”属性的一部分。因此,将其值更改为“mongodb://localhost/mydbname”之类的值,它应该可以工作。
猜你喜欢
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 2018-08-13
相关资源
最近更新 更多