I've spent hours on this. I can't get my variables interpolated correctly with my properties file. I have a properties file and cannot get the values interpolated correctly. I've tried specifying the file explicitly, using an external config and the config inside the jar to no avail.
Usage
#Component
public class TeradataConnector {
#Value("${jdbcurl}")
private String teradataConnection;
#Value("${jdbctable}")
private String teradataTable;
Error
Jan 05, 2021 6:10:15 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jan 05, 2021 6:10:15 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jan 05, 2021 6:10:15 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.34]
Jan 05, 2021 6:10:15 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teradataConnector': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbcurl' in value "${jdbcurl}"
Jan 05, 2021 6:10:16 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teradataConnector': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbcurl' in value "${jdbcurl}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at com.company.nap.cyclic.App.main(App.java:11) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbcurl' in value "${jdbcurl}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:912) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
... 17 more
application.properties
#### Personalized Batch Configuration
batch.max_records=1
batch.max_wait_seconds=15
jdbcurl=wefwef
jdbcuser=wefwef
jdbcpass=fweijfwef
teradata.table=tester
Make sure you are imported class for #Value annotation is import org.springframework.beans.factory.annotation.Value
application.properties file should inside src\main\resources folder.
You must add key & value for jdbctable in application.properties file which is missing.
Find the package containing your main Spring Boot Application file. It contains the static main method and that has the #SpringBootApplication annotation. It should look something like this:
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
Make sure your TeradataConnector class resides in a package beneath the location of your Spring Application class found in step 1. So if your Spring Application class is in: com.companyxyz.invoice
then you TeradataConnector class must be: com.companyxyz.invoice.someotherpackage
Note: It can be further down. In other words, it does not have to be immediately below, just underneath somewhere.
Reason: a Spring Boot Application will at startup by default will only scan and autowire components in the packages underneath the main Spring Application package. Note: You could also add a #ComponentScan annotation and tell Spring where to find the components if they are not underneath you directory. However, I would recommend just moving them underneath unless there is a strong compelling reason.
Related
Feb 02, 2022 12:58:03 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#71318ec4: startup date [Wed Feb 02 12:58:03 IST 2022]; root of context hierarchy
Feb 02, 2022 12:58:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Feb 02, 2022 12:58:04 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Unexpected AOP exception; nested exception is java.lang.ExceptionInInitializerError
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Unexpected AOP exception; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.vishal.main.DrawingMain.main(DrawingMain.java:10)
Caused by: org.springframework.aop.framework.AopConfigException: Unexpected AOP exception; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:215)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:473)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:355)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:304)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1714)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
... 11 more
Caused by: java.lang.ExceptionInInitializerError
at org.springframework.aop.framework.CglibAopProxy.createEnhancer(CglibAopProxy.java:232)
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:181)
... 18 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module #8519cb4
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:464)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221)
at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:174)
at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:153)
at org.springframework.cglib.proxy.Enhancer.<clinit>(Enhancer.java:73)
... 20 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module #8519cb4
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at org.springframework.cglib.core.ReflectUtils$1.run(ReflectUtils.java:61)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
at org.springframework.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:52)
at org.springframework.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:243)
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
... 32 more
My first guess is that you want to run an older Spring version on a JRE 16+. Probably that old Spring version depends on an equally dated CGLIB version which uses internal Java APIs now blocked due to JEP 396, which was implemented for Java 16. Maybe you can simply upgrade CGLIB and continue to use your old Spring version, maybe you need to upgrade both. Without more detailed information from you, it is hard to say. A minimal example Maven or Gradle project on GitHub, reproducing the problem, would be great.
On a second thought, maybe for now you can get away with adding
--add-opens java.base/java.lang=ALL-UNNAMED
to your JVM command line. But even if it would work, it would not be a particularly sustainable solution, only a workaround.
I was able to fix this today (with JDK 17) by upgrading to SpringBoot 2.6.4 (specifically, you need spring framework 5.3.16, so if you're using spring dependency management plugin, you only need to set the springboot version to 2.6.4).
Seems like something was fixed in spring framework 5.3.16 that resolved the issue without having to add the --add-opens JVM option.
For reference, the error for me was happening because I had autowired a Supplier bean (not for Spring cloud function).
I have a Spring-Boot multimodule Project.
Main Module is a Vaadin Web-App (at the Moment very simple) and a second Module called Login with Spring Entitys, Repository and Service bean.
In the Login Module I have written Testcode that runs against a H2 Database. Tests are green, Entity was created, repository exists and so on. (Everything fine)
When I try to start the WebApp I allways get the Message
A component required a bean named 'entityManagerFactory' that could not be found.
My Main Applicattion Class
#SpringBootApplication(scanBasePackages = "eu.dwich.btc", exclude = ErrorMvcAutoConfiguration.class)
#Import(BtcBackendLoginConfig.class)
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Then my Config Class in the module
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan(basePackageClasses = BtcBackendLoginConfig.class)
#EntityScan(basePackageClasses = BtcBackendLoginConfig.class)
#EnableJpaRepositories(basePackageClasses = BtcBackendLoginConfig.class)
public class BtcBackendLoginConfig {
}
and the properties file
database.driver-class-name=org.postgresql.Driver
database.database-url=jdbc:postgresql://localhost:5433/btc_dev
database.database-user=btc_dev
database.database-user-password=xxxxx
# --------------------------------------
# special DB settings
database.hibernater-format-s-q-l=true
database.hibernater-gen-statistics=true
database.hibernate-dialect=org.hibernate.dialect.PostgreSQL9Dialect
# possible: create-drop, none, validate, update, create
database.hibernate-ddl-auto=create-drop
database.create-db-script=true
First (maybe simple) Question, where should I put the application.properties to configure the database, in the web Project or in the Login module?
Second Question what I'm doing wrong? is it not possible to get the spring boot autoconfiguration to work? (I don't whant to configure datasource and entitymanager)
I have tryed a lot of different configrurations nothing worked until now.
Thanks in advance
Update 1:
there is only one line with a warning, no other stacktrace
2020-09-24 08:07:34.788 INFO 19699 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/h2-console/**'], []
2020-09-24 08:07:34.802 INFO 19699 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#b246c96, org.springframework.security.web.context.SecurityContextPersistenceFilter#f632a44, org.springframework.security.web.header.HeaderWriterFilter#3bd390ec, org.springframework.security.web.authentication.logout.LogoutFilter#44e09d5a, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#30d21b64, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#1eed7157, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#1d1c15ef, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#15a14e1e, org.springframework.security.web.session.SessionManagementFilter#6a47a0c5, org.springframework.security.web.access.ExceptionTranslationFilter#5492fd4, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#4f5c34f5]
2020-09-24 08:07:34.825 WARN 19699 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userGroupRepository': Cannot create inner bean '(inner bean)#542c45c7' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#542c45c7': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2020-09-24 08:07:34.827 INFO 19699 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
At the end ther is only this message, is it possible to get more information?
2020-09-24 08:07:34.887 ERROR 19699 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
Process finished with exit code 0
Update:
This output will be generated when turning debug on:
Exclusions:
-----------
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:680) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:498) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:374) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:134) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1451) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:860) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at eu.dwich.btc.Application.main(Application.java:24) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.0.RELEASE.jar:2.2.0.RELEASE]
why does spring exclude the whole spring Data stack??
You use the wrong variable keys so Spring cannot configure the objects required.
Instead, use the following
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5433/btc_dev
spring.datasource.username=btc_dev
spring.datasource.password=xxxxx
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format-sql=true
spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.database=postgresql
spring.jpa.hibernate.dll-auto=create-drop
Also, make sure you have all the required libraries on the classpath.
You need at least javax.persistence-api and hibernate.
I would also advice you NOT to use basePackageClasses unless you know what it does. If you place your Application configuration in his own package instead of the root, Spring won't be able to find any eligible classes.
At least for now, use basePackages = "eu.dwich.btc" instead.
Here is a list of common Spring properties.
Next to the # DATASOURCE section you see the reference to DataSourceAutoConfiguration.
Check your pom file , you must be missing some dependencies. I was also same problem and resolved after carefully gone through pom file , posgress driver jar was missing and spring-data depenndency was missing. Its giving very generic error you should enable stack trace
"server.error.include-stacktrace=never"
to get more detailed error.
I think I found the error
Spring applikation simple without imports
#SpringBootApplication(scanBasePackages = "eu.dwich.btc", exclude = ErrorMvcAutoConfiguration.class)
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
the backend config needs some modifikation
#Configuration
#EnableTransactionManagement
#EntityScan(basePackages = "eu.dwich.btc")
#EnableJpaRepositories(basePackages = "eu.dwich.btc")
#Import({
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class })
#Profile("!test")
public class BtcBackendLoginConfig {
}
the second #SpringBootApplication was an error and is not needet
without the Autoconfig imports it won't work
and be carefull with the properties, tada.....
#spring.jpa.properties.hibernate.ddl-auto=create-drop is wrong
#spring.jpa.hibernate.hbm2ddl.auto=create-drop is also wrong
# the right one (as mentioned by Nico Van Belle) is:
spring.jpa.hibernate.ddl-auto=create-drop
and if your run an real database (not H2) the default ddl-auto= is none
In my opinion the import of the autoconfig classes is not the best way, but it works.
i start spring boot with undertow container.
this is errer message
10:17:49.123 [main] ERROR org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroy method on bean with name 'org.springframework.context.annotation.internalScheduledAnnotationProcessor' threw an exception
10:17:49.128 [main] ERROR org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroy method on bean with name 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#71ba6d4e: startup date [Tue Dec 06 10:17:46 CST 2016]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:954) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:961) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033) [spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:555) [spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at com.test.Application.main(Application.java:34) [main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:?]
10:17:49.128 [main] ERROR org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroy method on bean with name 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#71ba6d4e: startup date [Tue Dec 06 10:17:46 CST 2016]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
10:17:49.129 [main] ERROR org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#71ba6d4e: startup date [Tue Dec 06 10:17:46 CST 2016]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
10:17:49.133 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedServletContainerCustomizerBeanPostProcessor': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cachingConfig' defined in file [D:\workspace\test\build\classes\main\com\test\config\CachingConfig.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:478) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cachingConfig' defined in file [D:\workspace\test\build\classes\main\com\test\config\CachingConfig.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:671) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
build.gradle
configurations {
compile.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
compile.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
task wrapper(type: Wrapper) {
gradleVersion = '3.1'
}
dependencies {
providedRuntime(
'org.springframework.boot:spring-boot-starter-undertow:1.4.2.RELEASE'
)
testCompile(
'junit:junit:4.11',
"org.springframework.boot:spring-boot-starter-test:1.4.2.RELEASE",
'org.spockframework:spock-core:1.1-groovy-2.4-rc-1',
'org.spockframework:spock-spring:1.1-groovy-2.4-rc-1',
//微基准测试
'org.openjdk.jmh:jmh-core:1.13',
'org.openjdk.jmh:jmh-generator-annprocess:1.13'
)
compile(
'org.springframework.boot:spring-boot-starter:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE',
// 'org.springframework.boot:spring-boot-starter-jersey:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-validation:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-actuator:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-jdbc:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-log4j2:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-configuration-processor:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-data-mongodb:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-data-redis:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-mail:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-aop:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-data-rest:1.4.2.RELEASE',
'org.springframework.boot:spring-boot-starter-freemarker:1.4.2.RELEASE',
'org.codehaus.groovy:groovy-all:2.4.7',
'cglib:cglib-nodep:3.2.4',
'org.objenesis:objenesis:2.4',
//异步日志支持
'com.lmax:disruptor:3.3.4',
//mybatis
'org.mybatis:mybatis:3.3.1',
'org.mybatis:mybatis-spring:1.2.4',
'com.github.pagehelper:pagehelper:4.1.3',
//mysql
'mysql:mysql-connector-java:5.1.38',
//数据库连接池
'com.alibaba:druid:1.0.18',
//java工具类
'com.google.guava:guava:19.0',
// apache的commons工具类
'commons-io:commons-io:2.4',
'org.apache.commons:commons-lang3:3.4',
'commons-codec:commons-codec:1.10',
'commons-fileupload:commons-fileupload:1.3.1',
'commons-beanutils:commons-beanutils:1.9.2',
//java guava工具类
'com.google.guava:guava:19.0',
//java collections 工具类
'org.eclipse.collections:eclipse-collections:8.0.0-M1',
'org.eclipse.collections:eclipse-collections-testutils:8.0.0-M1',
//操作redis
'redis.clients:jedis:2.8.1',
//json
'com.fasterxml.jackson.core:jackson-databind:2.7.3',
'com.fasterxml.jackson.core:jackson-core:2.7.3',
'com.fasterxml.jackson.core:jackson-annotations:2.7.3',
'com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.7.3',
//pojo copy
'net.sf.dozer:dozer:5.5.1',
//image util
//metadata-extractor is a straightforward Java library for reading metadata from image files.
'com.drewnoakes:metadata-extractor:2.8.1',
//Thumbnailator is a thumbnail generation library for Java.
'net.coobird:thumbnailator:0.4.8',
//二维码
'com.google.zxing:core:3.2.1',
'com.google.zxing:javase:3.2.1',
//shiro
'org.apache.shiro:shiro-core:1.2.4',
'org.apache.shiro:shiro-web:1.2.4',
'org.apache.shiro:shiro-ehcache:1.2.4',
'org.apache.shiro:shiro-spring:1.2.4',
'org.apache.shiro:shiro-cas:1.2.4',
//ant
'org.apache.ant:ant:1.9.6',
//servlet
'javax.servlet:javax.servlet-api:4.0.0-b01',
'javax.servlet.jsp:jsp-api:2.2.1-b03',
//管理项目中API接口
'com.mangofactory:swagger-springmvc:1.0.2',
'io.springfox:springfox-swagger2:2.5.0',
//http 请求工具
'com.sun.jersey:jersey-core:1.19.2',
'com.sun.jersey:jersey-client:1.19.2',
'com.sun.jersey.contribs:jersey-multipart:1.19.2'
)
}
but my project incloud validator jar.
if i add this code
// #Bean
// public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
// UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
// factory.addBuilderCustomizers(builder -> builder.addHttpListener(80, "0.0.0.0")
// .setServerOption(UndertowOptions.ENABLE_HTTP2, true));
// return factory;
error :java.lang.ClassNotFoundException: io.undertow.Undertow$Builder
if i use tocmat or jetty , it's ok
I'm not sure if it's a Spring Boot or Undertow issue.
this's my lib list
this's my lib list
I have resolved that error:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl
adding this missing dependency:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.2.0.Final</version>
</dependency>
I know this question has been asked many times but I've been stuck on this for a few days and cannot figure out why this is happening.
Here is my JUnit test suite, and the issue happens when I try to #Autowire RequestService:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:**/*.xml"})
public class RequestServiceTest {
#Autowired
private RequestService requestService;
#Test
public void add() throws Exception {
requestService.add(new Request(1, "hello"));
requestService.add(new Request(2, "hi"));
Request requestResponseFromDatabase = (Request) requestService.findById(1);
assertEquals("hello", requestResponseFromDatabase.getRestRequestParameters());
}
}
Below is my code for my model, DAO, and service classes.
Model (leaving out the constructor and other fields for brevity)
#Entity
public class Request implements Serializable {
#Id
private Integer restRequestId;
private Timestamp restRequestTimestamp;
private String restRequestParameters;
}
DAO
#Component
public class RequestDAO {
#Autowired
private RequestService requestService;
#PersistenceContext
private EntityManager entityManager;
public void persist(Request request) {
entityManager.persist(request);
}
public void update(Request request) {
entityManager.getTransaction().begin();
entityManager.merge(request);
entityManager.getTransaction().commit();
}
public Object findById(Serializable id) {
Request request = entityManager.find(Request.class, id);
if (request == null) {
throw new EntityNotFoundException("Cannot find Request for ID " + id);
}
return request;
}
public void delete(Request request) {
entityManager.getTransaction().begin();
entityManager.remove(request);
entityManager.getTransaction().commit();
}
public void deleteById(Serializable id) {
Request request = entityManager.find(Request.class, id);
if (request == null) {
throw new EntityNotFoundException("Cannot find Request for ID " + id);
} else if (request != null) {
entityManager.getTransaction().begin();
entityManager.remove(request);
entityManager.getTransaction().commit();
}
}
public List<Request> findAll() {
Query query = entityManager.createQuery("SELECT e from Request e");
List<Request> requests = query.getResultList();
return requests;
}
public void deleteAll() {
List<Request> requests = findAll();
for (Request request : requests) {
delete(request);
}
}
}
Service
#Component
public class RequestService {
#Autowired
private RequestDAO requestDAO;
#Transactional
public void add(Request request) {
requestDAO.persist(request);
}
#Transactional
public void update(Request request) {
requestDAO.update(request);
}
#Transactional
public Object findById(Serializable id) {
return requestDAO.findById(id);
}
#Transactional
public void delete(Request request) {
requestDAO.delete(request);
}
#Transactional
public void deleteById(Request request) {
requestDAO.delete(request);
}
#Transactional
public void deleteAll() {
requestDAO.deleteAll();
}
#Transactional
public void addAll(Collection<Request> requestCollection) {
for (Request request : requestCollection) {
requestDAO.persist(request);
}
}
#Transactional(readOnly = true)
public List<Request> findAll() {
return requestDAO.findAll();
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans -->
<context:component-scan base-package="com.company.mypackage"/>
<!-- Activates scanning of #Autowired -->
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem://requestDb"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="com.company.mypackage"
p:dataSource-ref="dataSource">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"/>
<property name="showSql" value="true"/>
</bean>
</property>
</bean>
<bean id="transactionManger" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManger"/>
</beans>
Full stacktrace:
May 23, 2016 1:57:38 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
May 23, 2016 1:57:38 PM org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
May 23, 2016 1:57:38 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener#3498ed, org.springframework.test.context.support.DependencyInjectionTestExecutionListener#1a407d53, org.springframework.test.context.support.DirtiesContextTestExecutionListener#3d8c7aca, org.springframework.test.context.transaction.TransactionalTestExecutionListener#5ebec15, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener#21bcffb5]
May 23, 2016 1:57:38 PM org.springframework.context.support.GenericApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext#548b7f67: startup date [Mon May 23 13:57:38 EDT 2016]; root of context hierarchy
May 23, 2016 1:57:38 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener#1a407d53] to prepare test instance [com.company.mypackage.request.service.RequestServiceTest#57175e74]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.company.mypackage.request.service.RequestServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.company.mypackage.request.service.RequestService com.company.mypackage.request.service.RequestServiceTest.requestService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.mypackage.request.service.RequestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.company.mypackage.request.service.RequestService com.company.mypackage.request.service.RequestServiceTest.requestService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.mypackage.request.service.RequestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 30 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.mypackage.request.service.RequestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 32 more
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.company.mypackage.request.service.RequestServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.company.mypackage.request.service.RequestService com.company.mypackage.request.service.RequestServiceTest.requestService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.mypackage.request.service.RequestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.company.mypackage.request.service.RequestService com.company.mypackage.request.service.RequestServiceTest.requestService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.mypackage.request.service.RequestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 30 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.mypackage.request.service.RequestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 32 more
Process finished with exit code -1
Additional Details
Spring version: 4.2.5.RELEASE
Hibernate version: 5.1.0.Final
My spring.xml is located within src/main/resources (yes I am aware that it is not the best practice but wanted to make sure my tests work first. I did try to keep it in my src/test/resources folder but kept getting issues with my ApplicationContext not being able to load)
Resources followed:
#Autowired with JUnit tests
#AUTOWIRED : BeanCreationException
#Autowired fails with JUnit & Spring
Spring not autowiring in unit tests with JUnit
Spring ORM example with JPA, Hibernate, Transactions
Any ideas on how to resolve this? I have asked similar questions to this in the past and do apologize - having a hard time understanding Spring along with all of it's documentation. I want to make sure I fully understand this to avoid making the same mistake in the future.
EDIT: Here is my folder structure:
Stack trace when using #ContextConfiguration(locations = {"classpath:spring.xml"}):
May 23, 2016 3:20:39 PM org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
May 23, 2016 3:20:39 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener#60f82f98, org.springframework.test.context.support.DependencyInjectionTestExecutionListener#35f983a6, org.springframework.test.context.support.DirtiesContextTestExecutionListener#7f690630, org.springframework.test.context.transaction.TransactionalTestExecutionListener#edf4efb, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener#2f7a2457]
May 23, 2016 3:20:39 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
May 23, 2016 3:20:39 PM org.springframework.context.support.GenericApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext#174d20a: startup date [Mon May 23 15:20:39 EDT 2016]; root of context hierarchy
May 23, 2016 3:20:39 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.hsqldb.jdbcDriver
May 23, 2016 3:20:39 PM org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
INFO: Building JPA container EntityManagerFactory for persistence unit 'default'
May 23, 2016 3:20:39 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: default
...]
May 23, 2016 3:20:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
May 23, 2016 3:20:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 23, 2016 3:20:39 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 23, 2016 3:20:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
May 23, 2016 3:20:40 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : User not found: USER
May 23, 2016 3:20:40 PM org.springframework.context.support.GenericApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
May 23, 2016 3:20:40 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener#35f983a6] to prepare test instance [com.company.mypackage.request.service.RequestServiceTest#481ba2cf]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
java.lang.IllegalStateException: Failed to load ApplicationContext
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Process finished with exit code -1
The error msg is very clear, you need to config your hibernate properly.
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
The possible reason could be:
You haven't setup your hibernate properly.
or there is no DB running during the test
The problem happening over here is it is not able to load your spring bean xml file. can you put your spring xml file directly at the root level in the classpath.
After deploy successfully jhipster 1.4.0 into heroku using yo jhipster:heroku, I make changes (no matters wich changes) to my code then I execute: grunt deployHeroku, and push to heroku master.
But Jhipster uses local datasource to trying to connect:
2014-10-14T19:38:59.648294+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true -Djava.rmi.server.useCodebaseOnly=true
2014-10-14T19:39:02.704297+00:00 app[web.1]: [INFO] es.japanathome.Application - Starting Application on 347e60b4-335d-489e-96c1-036737b62d48 with PID 2 (started by u6297 in /app)
2014-10-14T19:39:02.704442+00:00 app[web.1]: [DEBUG] es.japanathome.Application - Running with Spring Boot v1.1.7.RELEASE, Spring v4.0.7.RELEASE
2014-10-14T19:39:06.306581+00:00 app[web.1]: [DEBUG] org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
2014-10-14T19:39:06.553454+00:00 app[web.1]: [DEBUG] es.japanathome.config.AsyncConfiguration - Creating Async Task Executor
2014-10-14T19:39:06.915540+00:00 app[web.1]: [DEBUG] es.japanathome.config.DatabaseConfiguration - Configuring Datasource
2014-10-14T19:39:07.912527+00:00 app[web.1]: [DEBUG] es.japanathome.config.DatabaseConfiguration - Configuring Liquibase
2014-10-14T19:39:38.492503+00:00 app[web.1]: at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
2014-10-14T19:39:38.492538+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
2014-10-14T19:39:38.492577+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
2014-10-14T19:39:38.492649+00:00 app[web.1]: at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:370)
2014-10-14T19:39:38.492726+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
2014-10-14T19:39:38.492763+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
2014-10-14T19:39:38.492804+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
2014-10-14T19:39:38.492861+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
2014-10-14T19:39:38.492894+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
2014-10-14T19:39:38.492935+00:00 app[web.1]: at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
2014-10-14T19:39:38.492971+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
2014-10-14T19:39:38.493037+00:00 app[web.1]: at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
2014-10-14T19:39:38.493078+00:00 app[web.1]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
2014-10-14T19:39:38.493108+00:00 app[web.1]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:916)
2014-10-14T19:39:38.493148+00:00 app[web.1]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
2014-10-14T19:39:38.493186+00:00 app[web.1]: at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)
2014-10-14T19:39:38.493211+00:00 app[web.1]: ... 94 more
2014-10-14T19:39:38.493291+00:00 app[web.1]: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [es/japanathome/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.DatabaseException: java.sql.SQLException: Timeout of 30000ms encountered waiting for connection.
As you can see instead of use HerokuDatabaseConfiguration it uses DatabaseConfiguration, so this is the problem. But I don't know why, any ideas?
The solution is to remove the ".class" ending in the DatabaseConfiguration for the ConditionalOnMissingClass.
#ConditionalOnMissingClass(name = "my.app.HerokuDatabaseConfiguration.class") changed to #ConditionalOnMissingClass(name = "HerokuDatabaseConfiguration") helped me.
I think that ConditionalOnMissing is just looking for the ClassName and implies the .class suffix. With the deprecated "value" it was possible to add the class.