我有一個簡單的彈簧啓動應用程序,其中我有一個jpa庫對象,我想在類似下面的@Configuration
類中自動裝配。springboot autowiring @Repository裏面的@Configuration不起作用
@Configuration
public class Appconfig {
@Bean
@Autowired
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(OctopusPropertiesRepository repo) {
PropertySourcesPlaceholderConfigurer property = new PropertySourcesPlaceholderConfigurer();
Map<String,Object> props = new ConcurrentHashMap<>();
List<OctopusProperties> loadedSettings = repo.findAll();
loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue()));
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("custom", props));
property.setPropertySources(mutablePropertySources);
return property;
}
}
這裏是@Repository
類
@Repository
public interface OctopusPropertiesRepository extends JpaRepository<OctopusProperties, Long> {
}
,我得到以下異常。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.app.OctopusPropertiesRepository]
如果我沒有使配置和啓動應用程序成功,我從執行器中看到bean是可用的。這裏有什麼問題?爲什麼我不能在@Configuration
內部連線@Repository
。
P.S.這兩個java文件位於同一文件夾下:com.example.app
P.P.S.我的項目 Eclipse視圖:
包括正在掃描組件的類。還請在列表中包含軟件包。可能是主班? – luboskrnac
主類在根包中,並且所有其他類都在同一個包中。我認爲它與時間有些相關,因爲如果我不使用這個配置,spring會生成這個bean。 – cacert
如果你在Spring配置類中添加一個簡單的成員,如下所示,Spring是否會找到它?@Autowired OctopusPropertiesRepository octopusPropertiesRepository –