仅当我运行 shadowJar 时,我在创建 entityManager bean 时收到以下错误。
bootJar 或 bootRun没有给出任何异常;它工作正常。我需要创建一个影子罐子。
例外
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elabsEntityManager' defined in class path resource [com/sample/orchestrator/config/ElabsDataSourceConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a parameterized type!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1159)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
at com.sample.orchestrator.OrchestratorApplication.main(OrchestratorApplication.java:27)
Caused by: java.lang.IllegalArgumentException: Not a parameterized type!
at org.jboss.jandex.Type.asParameterizedType(Type.java:228)
at org.jboss.jandex.Indexer.resolveTypePath(Indexer.java:768)
at org.jboss.jandex.Indexer.resolveTypePath(Indexer.java:780)
at org.jboss.jandex.Indexer.resolveTypePath(Indexer.java:775)
at org.jboss.jandex.Indexer.resolveTypeAnnotation(Indexer.java:718)
at org.jboss.jandex.Indexer.resolveTypeAnnotations(Indexer.java:613)
at org.jboss.jandex.Indexer.index(Indexer.java:1602)
at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassDescriptor(ClassFileArchiveEntryHandler.java:64)
at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.handleEntry(ClassFileArchiveEntryHandler.java:52)
at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.visitArchive(JarFileBasedArchiveDescriptor.java:147)
at org.hibernate.boot.archive.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:48)
at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:76)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:107)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:254)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:168)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:52)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1847)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784)
... 17 more
构建.gradle
dependencies {
// Comment these beam libraries to not see the error in shadow jar But i need this
implementation 'org.apache.beam:beam-sdks-java-core:2.27.0'
implementation 'org.apache.beam:beam-runners-direct-java:2.27.0'
implementation 'org.apache.beam:beam-runners-google-cloud-dataflow-java:2.27.0'
// ....other dependencies
}
jar {
manifest {
attributes "Main-Class": "com.sample.orchestrator.OrchestratorApplication"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
shadowJar {
zip64 = true
exclude "**/Log4j2Plugins.dat"
// Required for Spring
mergeServiceFiles()
append 'META-INF/spring.handlers'
append 'META-INF/spring.schemas'
append 'META-INF/spring.tooling'
transform(PropertiesFileTransformer) {
paths = ['META-INF/spring.factories' ]
mergeStrategy = "append"
}
}
这是与实体管理器相关的 bean。
@Configuration
@EnableJpaRepositories(basePackages = {
"com.sample.orchestrator.database.elabs.repository" }, entityManagerFactoryRef = "elabsEntityManager", transactionManagerRef = "elabsTransactionManager")
@PropertySource("classpath:application-${spring.profiles.active}.yml")
public class ElabsDataSourceConfig {
@Value("${spring.datasource-elabs.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource-elabs.url}")
private String url;
@Value("${spring.datasource-elabs.username}")
private String username;
@Value("${spring.datasource-elabs.password}")
private String password;
@Value("${spring.jpa.database-platform}")
private String dialect;
@Value("$(spring.jpa.hibernate.ddl-auto)")
private String ddlAuto;
@Value("$(spring.jpa.show-sql)")
private String showSql;
@Bean(name = "datasource-elabs")
public DataSource datasourceElabs() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean("elabsEntityManager")
public LocalContainerEntityManagerFactoryBean elabsEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(datasourceElabs());
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setPackagesToScan(new String[] { "com.sample.orchestrator.model.database.elabs" });
// em.setPersistenceXmlLocation("classpath:persistence.xml");
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", dialect);
properties.put("hibernate.ddl-auto", ddlAuto);
properties.put("hibernate.show-sql", showSql);
em.setJpaPropertyMap(properties);
return em;
}
@Bean("elabsTransactionManager")
public PlatformTransactionManager elabsTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(elabsEntityManager().getObject());
return transactionManager;
}
}
这是一个示例程序。
要运行,请包括
./gradlew bootRun -Pdev
./gradlew bootJar
并在运行时包含“-Dspring.profiles.active=dev”./gradlew shadowJar
并包括“-Dspring.profiles.active=dev”——错误步骤。
我不使用 bootJar 的原因是因为我正在使用应用程序创建一个谷歌数据流管道,依赖的 jar 文件不是从 bootJar 暂存的(Beam 需要依赖项的绝对路径来暂存相关文件)。但是我尝试过使用非 Spring Maven 阴影 jar,所有相关文件都已上演。希望用 spring shadowJar 实现同样的效果。