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:排除自动化配置,添加自定义配置文件,如下




猜你喜欢

转载自blog.csdn.net/huang_550/article/details/73872930