我在尝试将存储库自动装入主类时遇到了一些问题。我收到以下错误: -
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.talkable_bm234.App required a bean of type 'com.talkable_bm234.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.talkable_bm234.repository.UserRepository' in your configuration.
以下是我的文件,之前我已经能够成功完成这项工作但是很难用Mongo进行设置:
App.java
package com.talkable_bm234;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import com.talkable_bm234.domain.User;
import com.talkable_bm234.repository.UserRepository;
import reactor.core.publisher.Flux;
@SpringBootApplication
@EnableReactiveMongoRepositories({"com.talkable_bm234.repository"})
@ComponentScan(basePackages = {"com.talkable_bm234"})
public class App {
@Autowired
UserRepository userRepository;
final static Logger logger = Logger.getLogger(App.class.getName());
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public CommandLineRunner administrators(ApplicationContext ctx) {
return args -> {
// Add administrator account type on boot
logger.info("preparing to add admin accounts...");
final User administratorA = new User("John", "Doe", "adminA@admins.co.uk", "12345", "ADMIN");
final User administratorB = new User("Wendy", "Doe", "adminB@admins.co.uk", "12345", "ADMIN");
userRepository.saveAll(Flux.just(administratorA, administratorB)).subscribe();
};
}
}
UserRepository.java
package com.talkable_bm234.repository;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import com.talkable_bm234.domain.User;
import reactor.core.publisher.Mono;
/**
* Reactive Repository class for handling users
* @author Ben
*/
@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String> {
//Find by Email Address
Mono<User> findByEmailAddress(String emailAddress);
}
MongoConfig.java
package com.talkable_bm234.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClients;
/**
* Reactive MongoDB Configuration for Cloud Database
* @author Ben
*
*/
@Configuration
@EnableReactiveMongoRepositories
@PropertySource("classpath:application.properties")
public abstract class MongoConfig extends AbstractReactiveMongoConfiguration {
//Values taken from 'application.properties'
//------------------------------------------
@Value("${spring.data.mongodb.username}")
private String userName;
@Value("${spring.data.mongodb.password}")
private String password;
@Value("${spring.data.mongodb.database}")
private String database;
@Value("${spring.data.mongodb.host}")
private String host;
@Value("${spring.data.mongodb.port}")
private Integer port;
//----------------------------------------
@Override
protected String getDatabaseName() {
return database;
}
@Bean
@DependsOn("embeddedMongoServer")
public MongoClient mongoClient() {
return (MongoClient) MongoClients.create(String.format("mongodb://%s:%d", host, port));
}
protected String getMappingBasePackage() {
return "com.talkable_bm234.domain";
}
}
User.java - Lombok Getters&amp;塞特斯
package com.talkable_bm234.domain;
import javax.persistence.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
/**
* Domain Class for a registered User.
* User Types: "USER", "PRESENTER", "ADMIN"
* @author Ben
* MongoDB & Lombok Annotations
*/
//Name of collection, user objects are mapped to! ->
@Document(collection="users")
public class User {
@Id
@Getter @Setter
private long ObjID;
@Getter @Setter @NonNull
private String firstName;
@Getter @Setter @NonNull
private String lastName;
@Getter @Setter @NonNull
@Indexed(name="emailAddress", unique=true)
private String emailAddress;
@Getter @Setter @NonNull
private String password;
@Getter @Setter @NonNull
private String type;
@PersistenceConstructor
public User(String firstName, String lastName,
String emailAddress, String password, String type) {
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
this.password = password;
this.type = type;
}
public User() {}
}
有什么想法吗? 问候, 本