我正在做一个用java8
和SringBoot 2.2.6
编写的大项目。该项目使用了Envers
,构建架构的女孩对我说,她无法application.properties
Envers
配置。比她做如下:
@Configuration
public class JPAConfig {
@Autowired
private DataSource dataSource;
@Bean(name="entityManagerFactory")
public LocalSessionFactoryBean sessionFactory() throws IOException {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setHibernateProperties(getHibernateProperties());
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("it.xxxx.xxxxx.xxxxx.common.model");
return factoryBean;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", PostgreSQL82Dialect.class.getName());
properties.put("hibernate.default_schema", "test");
properties.put("hibernate.listeners.envers.autoRegister", true);
properties.put("org.hibernate.envers.revision_field_name", "rev");
properties.put("org.hibernate.envers.revision_type_field_name", "rev_type");
properties.put("org.hibernate.envers.audit_table_prefix", "aud_");
properties.put("org.hibernate.envers.store_data_at_delete", true);
properties.put("org.hibernate.envers.audit_table_suffix", "");
return properties;
}
}
问题是,如果没有dataSource class name
我就无法启动我的@SpringBootTest
类,并且我不知道如何在这样的场景中添加它(我的意思是不更改配置(。
我还尝试在application.properties
中添加此行:
spring.profiles.active=@spring.profile@
spring.datasource.driver-class-name=org.postgresql.Driver
#JPA
spring.datasource.jndi-name=jdbc/test
但根本不起作用..
如果我使用JUnit
运行应用程序,则会收到此错误:
org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/test'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
你可以帮我吗??
多谢
您需要在spring-boot
嵌入的 tomcat 中将Datasource
注册为 JNDI 资源。
可以将其添加为测试范围配置。
此答案显示了如何注册 JNDI 资源:https://stackoverflow.com/a/26005740/5230585