我正在尝试将Morphia
与Java
连接起来,因此尝试实现一个项目。但是,出现以下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method init in code.nithin.application.Application required a bean of type 'code.nithin.repository.AssetRepository' that could not be found.
Action:
Consider defining a bean of type 'code.nithin.repository.AssetRepository' in your configuration.
Process finished with exit code 1
调用函数是
@Bean
public CommandLineRunner init(AssetRepository assetRepository){
return (args) -> {
Asset test = new Asset(1, "https://example.com");
System.out.println(assetRepository.save(test).getId());
};
}
AssetController
是
@Repository
public class AssetController implements AssetRepository{
@Autowired
private Datastore datastore;
@Override
public Key<Asset> save(Asset asset){
return datastore.save(asset);
}
}
而AssetRepository
是
public interface AssetRepository {
public Key<Asset> save(Asset asset);
}
Asset
类是
@Data
@Entity("asset")
@NoArgsConstructor
public class Asset {
@Id
private ObjectId id;
private String URL;
private long timeStamp;
private int version;
public Asset(int version, String URL) {
this.version = version;
this.URL = URL;
this.timeStamp = System.currentTimeMillis();
}
public ObjectId getId() {
return id;
}
public String getURL() {
return URL;
}
public long getTimeStamp() {
return timeStamp;
}
public int getVersion() {
return version;
}
}
添加了整个Application
类
package code.nithin.application;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import code.nithin.entity.Asset;
import code.nithin.repository.AssetRepository;
@SpringBootApplication(exclude = EmbeddedMongoAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner init(AssetRepository assetRepository){
return (args) -> {
Asset test = new Asset(1, "https://example.com");
System.out.println(assetRepository.save(test).getId());
};
}
}
答案 0 :(得分:0)
由于您的存储库不在Application
的包层次结构中,请考虑添加组件扫描指令,例如:
@SpringBootApplication(exclude = EmbeddedMongoAutoConfiguration.class)
@ComponentScan({"code.nithin.repository"})
public class Application {
您还可以指定多个软件包,或者如果要扫描整个应用程序的软件包树,则只需扫描"code.nithin"
。