我對Spring 2.5開發和java有一些瞭解,但我想了解更多關於現代Spring的知識,包括Boot和Data。我在看項目:如何覆蓋由SPRING BOOT提供的默認值自動配置
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());
}
}
}
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb – geoand 2014-10-08 13:45:30
正如我在下面所說的,我已經改變了application.properties文件中的屬性。是否有相應的更改爲所需的Java? – kayakpim 2014-10-08 13:59:22