Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!


使用springboot做jdbcTemplate的注入时候,启动报错!!

原因:springboot已经配置了datasource的自动化配置,因为做了jpa的自动化配置去除导致

@Configuration
@EnableAutoConfiguration(exclude = {
        MetricExportAutoConfiguration.class,
        SecurityAutoConfiguration.class,
        ManagementWebSecurityAutoConfiguration.class,
       DataSourceAutoConfiguration.class,
       HibernateJpaAutoConfiguration.class,
       DataSourceTransactionManagerAutoConfiguration.class,
        MetricExportAutoConfiguration.class
})
@ComponentScan
@EnableDiscoveryClient
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableFeignClients
@EnableScheduling
public class PackResolveServiceApp extends SpringBootServletInitializer {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        SpringApplication.run(PackResolveServiceApp.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(PackResolveServiceApp.class);
    }
}
解决方式1:注释掉  EnableAutoConfiguration 中datasource和hibernate相关的autoConfig,开启自动化配置

在代码中就能使用jdbcTemplate:

    @Autowired
    DataSource dataSource;

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        try {
            jdbcTemplate.setDataSource(this.dataSource);
            jdbcTemplate.setLazyInit(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jdbcTemplate;
    }

解决方式2:排除自动化配置,添加自定义配置文件,如下




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值