David Gueta
David Gueta

Reputation: 89

Spring Boot and MongoDB configuration

i'm new with spring.

I'm using maven to build my webapp. I have the following structs:

  1. pom.xml
  2. src/main/[java/resources]

there is no *.xml file, *.conf or *.properties... nothing.

Application.java

@Autowired
private UserRepository repository;

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

@Override
public void run(String... args) throws Exception {
    repository.deleteAll();
    repository.save(new User("test", "123"));
    List<User> users = repository.findByLogin("test");
    ...
}

Also, User.java and UserRepository.java

public interface UserRepository extends MongoRepository<User, String> {
public List<User> findByLogin(String login);

}

and it works!!!

my question is: How I change the configuration of mongo? database, password??

thanks!

Upvotes: 0

Views: 6168

Answers (1)

Danilo Picolotto
Danilo Picolotto

Reputation: 56

Create the file: src\main\resources\application.properties

and in this file, put:


    # MONGODB (MongoProperties)
    spring.data.mongodb.host= # the db host
    spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
    spring.data.mongodb.uri=mongodb://localhost/test # connection URL
    spring.data.mongodb.database=
    spring.data.mongodb.authentication-database=
    spring.data.mongodb.grid-fs-database=
    spring.data.mongodb.username=
    spring.data.mongodb.password=
    spring.data.mongodb.repositories.enabled=true # if spring data repository support is enabled

Upvotes: 4

Related Questions