gpt4 book ai didi

java - 'Application' 中方法 init 的参数 0 需要类型为 'package' 的 bean,但无法找到

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:52 24 4
gpt4 key购买 nike

我正在尝试将 MorphiaJava 连接起来,因此尝试实现一个项目。但是,我收到以下错误

***************************
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());
};
}
}

最佳答案

由于您的存储库不在Application的包层次结构中,请考虑添加组件扫描指令,例如:

@SpringBootApplication(exclude = EmbeddedMongoAutoConfiguration.class)
@ComponentScan({"code.nithin.repository"})
public class Application {

您还可以指定多个包,或者如果您想扫描整个应用程序的包树,则只需扫描“code.nithin”

关于java - 'Application' 中方法 init 的参数 0 需要类型为 'package' 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52111477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com