【发布时间】:2016-08-03 11:48:05
【问题描述】:
我有一个简单的 Spring Boot 应用程序,其中我有一个 jpa 存储库对象,我想在 @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 视图:
【问题讨论】:
-
包含扫描组件的类。还请在列表中包含包裹。可能是主要课程?
-
主类在根包中,其他类在同一个包中。我认为它与时间有点相关,因为如果我不使用这个配置,spring 会生成这个 bean。
-
如果你添加一个简单的成员,如下所示,在配置类中 Spring 会找到它吗? @Autowired OctopusPropertiesRepository octopusPropertiesRepository
-
不,我试过了,但没有改变:(
-
@luboskrnac 我已经添加了屏幕截图
标签: java spring spring-boot spring-data-jpa