我在将 JpaRepository 正确注入(inject)数据提供程序时遇到问题。
我在我的 EpisodieApiApplication.class 中添加了 @EnableJpaRepositories 注释位于包中:
package pl.hypeapp.episodie;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "pl.hypeapp.episodie.configuration")
@EnableAdminServer
@EnableJpaRepositories
public class EpisodieApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(EpisodieApiApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EpisodieApiApplication.class);
}
}
DatabaseDataProviderConfiguration.class位于包装
package pl.hypeapp.episodie.configuration.dataprovider;
@Configuration
public class DatabaseDataProviderConfiguration {
@Bean
public TvShowDatabaseProvider tvShowDatabaseProvider(TvShowRepository tvShowRepository) {
return new TvShowDatabaseProvider(tvShowRepository);
}
}
package pl.hypeapp.episodie.dataproviders.database;
public interface TvShowRepository extends JpaRepository<TvShow, String> {
}
我的数据库提供者在模块 application/dataproviders/database DatabaseDataProvider.class
public class TvShowDatabaseProvider implements DoesTvShowExist, GetTvShowFromDatabase, InsertTvShowToDatabase {
private final TvShowRepository tvShowRepository;
public TvShowDatabaseProvider(TvShowRepository tvShowRepository) {
this.tvShowRepository = tvShowRepository;
}
}
TvShow.class位于包中:
package pl.hypeapp.core.entity.tvmaze;
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Entity
public class TvShow {
@JsonProperty("id")
public Integer id;
}
但是我得到了这个错误:
Error creating bean with name 'tvShowDatabaseProvider' defined in class path resource [pl/hypeapp/episodie/configuration/dataprovider/DatabaseDataProviderConfiguration.class]: Unsatisfied dependency expressed through method 'tvShowDatabaseProvider' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tvShowRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class pl.hypeapp.core.entity.tvmaze.TvShow
完整代码位于 github但它不完整,可能由于缺少类而无法工作
最佳答案
您正在使用构造函数注入(inject)来注入(inject) TvShowRepository
bean 并且 @Autowired
缺少 TvShowDatabaseProvider
的构造函数,因此您需要添加如下所示:
public class TvShowDatabaseProvider implements
DoesTvShowExist, GetTvShowFromDatabase,
InsertTvShowToDatabase {
private final TvShowRepository tvShowRepository;
@Autowired //add this
public TvShowDatabaseProvider(TvShowRepository tvShowRepository) {
this.tvShowRepository = tvShowRepository;
}
}
关于java - 注入(inject) JpaRepository : Error creating bean with name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43237852/