Neu im Springboot (und im Frühjahr).
Ich habe dieses einfache Spring-Boot-Projekt und versuche, eine Verbindung zu MongoDB auf einem Remote-Server herzustellen. Ich habe die folgende Meldung, wenn ich die Anwendung ausführe.
Description:
Parameter 0 of constructor in com.mongotest.demo.Seeder required a bean of type 'com.mongotest.repositories.StudentRepository' that could not be found.
Action:
Consider defining a bean of type 'com.mongotest.repositories.StudentRepository' in your configuration.
Die Projektstruktur.
Und hier sind meine Klassen
@Document(collection = "Students")
public class Student {
@Id
private String number;
private String name;
@Indexed(direction = IndexDirection.ASCENDING)
private int classNo;
//Constructor and getters and setters.
}
================================
@Repository
public interface StudentRepository extends MongoRepository<Student, String>{
}
================================
@Component
@ComponentScan({"com.mongotest.repositories"})
public class Seeder implements CommandLineRunner{
private StudentRepository studentRepo;
public Seeder(StudentRepository studentRepo) {
super();
this.studentRepo = studentRepo;
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
Student s1 = new Student("1","Tom",1);
Student s2 = new Student("2","Jerry",1);
Student s3 = new Student("3","Kat",2);
studentRepo.deleteAll();
studentRepo.save(Arrays.asList(s1,s2,s3));
}
}
================================
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Ich glaube, ich folge diesem Tutorial genau
https://www.youtube.com/watch?v=Hu-cyytqfp8
Bitte helfen Sie mir dabei ... Vielen Dank im Voraus für Ihre Hilfe!
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>com.mongotest</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<Java.version>1.8</Java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-Java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Bitte fügen Sie die folgenden Anmerkungen in DemoApplication
ein.
@SpringBootApplication
@ComponentScan("com.mongotest") //to scan packages mentioned
@EnableMongoRepositories("com.mongotest") //to activate MongoDB repositories
public class DemoApplication { ... }
Wenn Sie das Schreiben von Anmerkungen vermeiden möchten, können Sie die Pakete com.mongotest.entities in com.mongotest.demo.entities und com.mongotest.repositories in com.mongotest.demo.repositories ändern
Spring Boot-Architektur kümmert sich um die Ruhe. Tatsächlich sollten sich andere Dateien und Pakete entweder auf derselben Ebene oder unterhalb Ihrer DemoApplication.Java befinden.
In meinem Fall erhielt ich denselben Fehler bei der Verwendung von mysql db
gelöst mit@EnableJpaRepositories
@SpringBootApplication
@ComponentScan("com.example.repositories")//to scan repository files
@EntityScan("com.example.entities")
@EnableJpaRepositories("com.example.repositories")
public class EmployeeApplication implements CommandLineRunner{ ..}
Ich habe 3 Hilfsklassen RedisManager, JWTTokenCreator & JWTTokenReader, die ich im Konstruktor als Abhängigkeit vom SessionService-Frühlingsdienst übergeben musste.
@SpringBootApplication
@Configuration
public class AuthenticationServiceApplication {
@Bean
public SessionService sessionService(RedisManager redisManager, JWTTokenCreator tokenCreator, JWTTokenReader tokenReader) {
return new SessionService(redisManager,tokenCreator,tokenReader);
}
@Bean
public RedisManager redisManager() {
return new RedisManager();
}
@Bean
public JWTTokenCreator tokenCreator() {
return new JWTTokenCreator();
}
@Bean
public JWTTokenReader JWTTokenReader() {
return new JWTTokenReader();
}
public static void main(String[] args) {
SpringApplication.run(AuthenticationServiceApplication.class, args);
}
}
Die Serviceklasse lautet wie folgt
@Service
@Component
public class SessionService {
@Autowired
public SessionService(RedisManager redisManager, JWTTokenCreator
tokenCreator, JWTTokenReader tokenReader) {
this.redisManager = redisManager;
this.tokenCreator = tokenCreator;
this.jwtTokenReader = tokenReader;
}
}