当前位置: 首页 > 知识库问答 >
问题:

springboot autowiring@repository在@configuration中不起作用

骆磊
2023-03-14

我有一个简单的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]

共有1个答案

解浩渺
2023-03-14

使用@PostConstruct。

@Autowired PropertySourcesPlaceholderConfigurer property;


@Autowired OctopusPropertiesRepository repo;


   @PostConstruct
    public  void onInit() {

        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);

    }
 类似资料:
  • 更新: 因为有些答案提供了解决方案,我问的是解释而不是解决方案。以下内容甚至不需要对“测试”进行组件扫描就可以工作: 现在的问题是,当@repository不工作时,为什么我甚至需要在它上使用componentscan?为什么在文档中@repository是由componentscan扫描的,而@enablejparepostiories是enoguh?

  • 我有一个简单的类叫BeaconDao 然而,当我用@service或@component标记beaconDao时,一切都运行得非常好。有人能看出问题所在吗?

  • 问题内容: 我想制作一个DatabaseConfig类来设置与数据库相关的东西(EntityManager,DataSource,TransactionManager)并获取我在字段上使用的属性 喜欢 问题是,所有这些字符串都是NULL,而不是我的属性文件的值。 如果我将代码放入我的类(具有和中引用的代码),则值注入有效 简而言之,@Value在我的Application类中有效,但在我的自定义@

  • 我有一些 在量角器中,我们搜索并找到元素,检查文本是否符合我们的期望,然后对该元素调用。测试在Chrome中运行良好,但在IE中就好像没有点击发生一样。破坏了测试。 IE 11是否支持点击

  • 问题内容: 我注意到less.js在firefox中工作,但在Chrome中不工作,或者是因为我出错了吗? 即使我尝试在Chrome中仍然无法使用,我在某个地方犯了错误吗? 问题答案: 通过您提供的链接: 如果您使用的是Chrome,Less.js浏览器脚本当前将无法使用,并且由于已知的Chrome问题,网页的路径以“file:///”开头。

  • 问题内容: 不仅如此,其他代码也有相同的问题。只是不能使用ImageView。 环境:macOS,IntelliJ 造成原因:java.lang.IllegalArgumentException:无效的URL:无效的URL或找不到资源 问题答案: 该图像构造函数接受一个url作为参数。如果您未在其中添加协议,则它将假定该项目来自类路径。显然,不会出现在您的类路径中。 要从文件而不是类路径中读取,请