将JpaRepository正确注入数据提供程序时遇到问题。
我将@EnableJpaRepositories注释放在包中的EpisodieApiApplication.class中:
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);
}
}
位于pacakage的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> {
}
模块应用程序/ dataproviders / database中的我的databaseprovider DatabaseDataProvider.class
public class TvShowDatabaseProvider implements DoesTvShowExist, GetTvShowFromDatabase, InsertTvShowToDatabase {
private final TvShowRepository tvShowRepository;
public TvShowDatabaseProvider(TvShowRepository tvShowRepository) {
this.tvShowRepository = tvShowRepository;
}
}
位于包中的
package pl.hypeapp.core.entity.tvmaze;
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Entity
public class TvShow {
@JsonProperty("id")
public Integer id;
}
但我收到了这个错误:
在类路径资源[pl / hypeapp / episodie / configuration / dataprovider / DatabaseDataProviderConfiguration.class]中定义名称为'tvShowDatabaseProvider'的bean时出错:通过方法'tvShowDatabaseProvider'参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'tvShowRepository'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:不是托管类型:class pl.hypeapp.core.entity.tvmaze.TvShow
github处的整个代码,但其不完整,可能由于缺少类而无效
答案 0 :(得分:2)
您正在使用构造函数注入来注入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;
}
}
答案 1 :(得分:1)
您没有扫描您的实体。您可以使用"pl.hypeapp"
作为@EnableJpaRepository
的basePackages或使用@EntityScan
:
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "pl.hypeapp.episodie.configuration")
@EnableAdminServer
@EnableJpaRepositories(basePackages = {"pl.hypeapp.episodie.dataproviders"})
@EntityScan(basePackages = {"pl.hypeapp.core.entity"})
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);
}
}