Konfigurieren von Spring-Repository mongoTemplate definiert in xml-bean?
Ausführung des Codes in Ordnung, aber schafft Sammlungen in default mongo-Datenbank und Ort ich.e in test Datenbank @ localhost:27017. In der mongoTemplate bean WLAN durch das folgende xml -, ich bin mit mydb als Datenbank mit mongod-Instanz läuft auf localhost:27018. Allerdings werden die Daten noch bekommt persistenten Standard-Instanz und-Datenbank.
MongoDB XML-Bean definiert src/main/resources/mongo-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<mongo:mongo id="mongo" host="localhost" port="27018"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg value="mydb" />
</bean>
<mongo:repositories base-package="core.repository" mongo-template-ref="mongoTemplate"/>
</beans>
Playlist-repository:
package core.repository;
import core.dao.Playlist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.repository.Repository;
/**
* This repository provides CRUD operations for {@link core.dao.Playlist} objects.
*/
public interface PlaylistRepository extends Repository<Playlist, String> {
/**
* Finds the information of a single Playlist entry.
* @param id The id of the requested Playlist entry.
* @return The information of the found Playlist entry. If no Playlist entry
* is found, this method returns an empty {@link java.util.Optional} object.
*/
Optional<Playlist> findOne(String id);
/**
* Saves a new Playlist entry to the database.
* @param saved The information of the saved Playlist entry.
* @return The information of the saved Playlist entry.
*/
Playlist save(Playlist saved);
}
Playlist-service, der verwendet repository:
package core.service;
import core.dao.*;
import core.error.NotFoundException;
import core.repository.PlaylistRepository;
import core.simulator.PlaylistServiceSimulator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Executes the business logic promised by the {@link core.service.PlaylistService} interface.
*/
@Service
final class PlaylistServiceExecutor implements PlaylistService {
private static final Logger LOGGER = LoggerFactory.getLogger(PlaylistServiceExecutor.class);
private final PlaylistRepository repository;
private final PlaylistServiceSimulator simulator;
@Autowired
PlaylistServiceExecutor(PlaylistRepository repository, PlaylistServiceSimulator simulator) {
this.repository = repository;
this.simulator = simulator;
}
@Override
public PlaylistDTO create(PlaylistDTO playlist) {
LOGGER.debug("Creating a new Playlist entry with information: {}", playlist);
Playlist persisted = Playlist.build()
persisted = repository.save(persisted);
LOGGER.debug("Created a new Playlist entry with information: {}", persisted);
return persisted.toDTO();
}
@Override
public PlaylistDTO findById(String id) {
LOGGER.debug("Finding Playlist entry with id: {}", id);
Playlist found = findPlaylistById(id);
LOGGER.debug("Found Playlist entry: {}", found);
return found.toDTO();
}
private Playlist findPlaylistById(String id) {
Optional<Playlist> result = repository.findOne(id);
return result.orElseThrow(() -> new NotFoundException(id));
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myapp</groupId>
<artifactId>core</artifactId>
<version>0.1</version>
<properties>
<!-- Enable Java 8 -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Configure the main class of our Spring Boot application -->
<start-class>core.CoreApp</start-class>
</properties>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
</parent>
<dependencies>
<!-- Get the dependencies of a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Spring Data MongoDB-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Support -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Schließlich SpringApplication-boot-Klasse CoreApp:
package core;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* This configuration class has three responsibilities:
* <ol>
* <li>It enables the auto configuration of the Spring application context.</li>
* <li>
* It ensures that Spring looks for other components (controllers, services, and repositories) from the
* <code>core</code> package.
* </li>
* <li>It launches our application in the main() method.</li>
* </ol>
*/
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class CoreApp {
public static void main(String[] args) {
SpringApplication.run(CoreApp.class, args);
}
}
Haben Sie andere Konfigurationen? Dies scheint durch das Buch.
Nein. Ich versuche, herauszufinden, wie pass-Eigenschaften definiert, die von außen auf die spring-Anwendung und dies ist mein Erster Versuch nach dem Lesen eine Menge von Artikeln. Ich Frage mich, ob ich verlängern sollte PlaylistRepository von MongoRepository statt nur-Repository. Werde es mal ausprobieren.
Spring boot-Autokonfiguration ist wahrscheinlich die Abholung Ihres
Nein. Ich versuche, herauszufinden, wie pass-Eigenschaften definiert, die von außen auf die spring-Anwendung und dies ist mein Erster Versuch nach dem Lesen eine Menge von Artikeln. Ich Frage mich, ob ich verlängern sollte PlaylistRepository von MongoRepository statt nur-Repository. Werde es mal ausprobieren.
Spring boot-Autokonfiguration ist wahrscheinlich die Abholung Ihres
spring-data-mongodb
Abhängigkeit und die automatische Konfiguration Ihrer mongo-repository. Wie funktioniert Frühjahr wissen, über Ihre mongo-context.xml
? Ich sehe es nicht überall verwiesen wird
InformationsquelleAutor Srini K | 2015-02-19
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ich nehme an, Sie sind mit Spring boot für Sie die automatische Konfiguration Merkmale und rechthaberisch Standardwerte. Wenn dem so ist, sollten Sie lassen Sie den Frühling konfigurieren Sie Ihr Mongo-repository.
Entfernen Sie Ihre mongo-Konfiguration in
src/main/resources/mongo-context.xml
Ersetzen Sie die mongo Abhängigkeit
mit den folgenden Frühjahr-boot-Abhängigkeit zu
pom.xml
Stellen Sie sicher, dass Ihre repositories erweiternMongoRepository
Müssen mindestens die folgenden Eigenschaften in Ihrem
application.properties
. Siehe Spring-Boot-Anhang für Eigenschaften ReferenzHier sind einige weitere Handbücher:
Cool, ich werde aktualisieren, die Antwort.
InformationsquelleAutor ikumen
Können Sie
applicationContext.xml
- Datei für dieses. Es sieht wie folgt aus:InformationsquelleAutor AknKplnoglu