Why Injection of Bean failed in Spring Mvc project - spring

I wanted to follow the tutorial on the following link: Tutorial Spring MVC, but at runtime I received the following error message.
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'applicantDAOImpl': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private org.hibernate.SessionFactory
org.o7planning.springmvcforms.dao.impl.ApplicantDAOImpl.sessionFactory;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'sessionFactory' defined in
org.o7planning.springmvcforms.config.ApplicationContextConfig: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.hibernate.SessionFactory]: Factory method
'getSessionFactory' threw exception; nested exception is
java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Does anyone have an idea of the source of this error?
Thank you

A long time ago I've followed that tutorial and it worked for me :)
Based on the exception I would look at the DB configs in your project because it looks like Spring can't create Hibernate-related Beans.
But first I would check some similar problems like your:
How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Good luck!

Well, i Had Similar Issues with an old project from around 2019 plus
<!-- https://mvnrepository.com/artifact/javax.xml.ws/jaxws-api -->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2</version>
</dependency>
i added this dependecy for javax, since it's the one responsible for the xml bindings that may be present in the project.
check here for more explanation https://www.jesperdj.com/2018/09/30/jaxb-on-java-9-10-11-and-beyond/

Related

Spring Boot + HikariCP - non-deterministic failure to auto-configure a DataSource

On my local machine, when I run one of my Spring Boot 2.0.0 applications from IntelliJ, the application periodically fails to start and the following message is displayed in the console:
Application Failed to Start
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable drive class
When I get this error, if I try to run the application again it will (sometimes) start. The behavior seems non-deterministic. Sometimes the application starts on the first try, sometimes it takes a few consecutive attempts.
Edit ----------------------------------------------------------------------------
The error appears to be caused by the application failing to determine what profile is active. When the application fails to start, the Spring logging output states "No active profile set, falling back to default profiles: default". When the application starts successfully, the logging output states "The following profiles are active: local,permitall", which are the profiles I set to active in application.properties.
Unfortunately, whether or not Spring determines what profile is active when starting is a roll of the dice for me.
----------------------------------------------------------------------------------
My application is configured to use spring-boot-starter-data-jpa and connect to a Postgres database. For connection pooling I use HikariCP 2.7.8.
The console error (see below) implies that Hikari is failing to load a data source. Since this error is non-deterministic, maybe it depends on the order that Spring creates beans during startup?
The spring.datasource.url property is defined in my local application properties file. The application properties file sets the spring active profile property to local.
This is my application.properties file
spring.profiles.active=local,permitall
management.endpoints.web.base-path=/
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
This is my application-local.properties
spring.datasource.url=jdbc:postgresql://localhost/vms?currentSchema=vms
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
I haven't found a pattern to the failures or the successes. Any ideas on solving or troubleshooting this problem are appreciated.
Console error message
2018-05-18 13:53:49.667 ERROR 16228 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource
[org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$61a24aeb]: Constructor threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception;
nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

Spring Boot application WAR deployment to Resin 4: is there a way to use app (WEB-INF/lib) classloader first?

I'm trying to deploy Spring Boot app as a WAR to Caucho Resin 4. I'm getting an error on hitting the app:
org.springframework.context.ApplicationContextException: Unable to
start embedded container; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name
'org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration'
: Bean instantiation via constructor failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration$
$EnhancerBySpringCGLIB$$58b91c9d]: Constructor threw exception; nested
exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'dashboardResource' defined in file
[/var/resin/webapps/itasng-0.8.5/WEB-INF/classes/gov/nih/cit/itasng/
rest/DashboardResource.class]: Unsatisfied dependency expressed through
constructor parameter 0; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'payPeriodRepository' defined in file
[/var/resin/webapps/itasng-0.8.5/WEB-INF/classes/gov/nih/cit/itasng/
persistence/PayPeriodRepository.class]: Unsatisfied dependency
expressed through constructor parameter 0; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name
'org.springframework.orm.jpa.SharedEntityManagerCreator#0': Cannot
resolve reference to bean 'entityManagerFactory' while setting
constructor argument; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/
HibernateJpaAutoConfiguration.class]: Invocation of init method failed;
nested exception is java.lang.NoSuchMethodError:
javax.persistence.Table.indexes()[Ljavax/persistence/Index; at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext
.onRefresh(EmbeddedWebApplicationContext.java:137)
~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
...
It's obvious that this is a typical class-loading issue: classes loaded by the parent class-loader from Resin lib (e.g. lib/javaee-16.jar) cause issues for the app built with newer versions (e.g. WEB-INF/lib/hibernate-jpa-2.1-api-1.0.0.Final.jar).
Most app servers that I used before have a setting somewhere in a vendor specific –web.xml descriptor to switch the classloader order so that WEB-INF/lib classes load first. However, the Resin docs don't really have an easy answer if such an option exists.
Is there a way to switch class loading order in Resin 4 or what would be an alternative solution?
In Resin 4.0 there is no way to use app classloader first.
Potential workarounds for related classloading issues could involve using
the <jvm-classpath> which has precedence over Resin's classpath.
An example use case is to use later versions of JPA instead of JPA 2.0, which is deployed out-of-the-box.
Documentation reference:
http://www.caucho.com/resin-4.0/admin/cluster-config.xtp#jvm-classpath

spring + Hibernate mvc

I am new to the spring mvc and i want to do validation in spring+hibernate using mvc annotations, i have added two jars for that (hibernate-validator-5.0.1.final.jar and hibernate-validator-5.0.1.final.jar).
I am getting exception as follow can anybody help please,
thanks in advance
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger

Spring Error : java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer

First 2 Parts Solved --- I still Got Another Error Below >>>>
I am having trouble w. this Struts - Spring - Hibernate Application.
I got the error while accessing the Action class on Struts. It failed during instantion of the ActionClass due to Spring failed to create a Hibernate object bean.
Then I used log4j to get the info. It complained about :
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [factory/bean.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [factory/bean.xml]: Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.NoSuchFieldError: INSTANCE
Here is the full log4j log on the console :pastebin/z6FFAUEE
Here is my bean.xml : pastebin.com/nREBZ99N
I am honestly not very sure what the error is about, because this is my first time using Spring Hibernate and also the AOP aspects.
I think I have all the libs needed, but I am not very sure too. I think it could be on the xml setting or some missing parameters.
Any helps / pointers is appreciated.
Thx
^^^^SOLVED -- Now I got another error
Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V
this is probably another library issue : Google Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(Z)V
but i m not again vr sure since, I am new using this framework, I am not vr familiar / knowledgable.
Any help poiting out the error/cause would sincerely be appreciated.
There is a ton of libs jars and I am only familiar with few of them, mostly just guessing the others. The libs is inherited from other application. Any links on good reference on libs also appreciated.
Here they are : http://pastebin.com/tgz8UFfs
^^^^SOLVED - Solution : I removed the text files from lib.
I had this error now, complaining that: java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer
It complains about error on creating serviceProxyTemplate .. etc cannot initiate ^ class
here is the full stack trace : http://pastebin.com/c7rqk5cR
What library is the error / what lib do I need ?
Thx for the help

Deployment of .war works locally but fails on distant server with Glassfish3

I have been working on this web application for more than a year and here is the configuration which did not bring any problem until now:
Java EE application using Spring, EclipseLink and maven for dependencies - PostgreSQL database
Locally I work in Eclipse where my Glassfish3 server and maven plugin are integrated and I use jdk1.7.0_03
On my Windows server, Glassfish3 is also installed - also jdk1.7.0_03 and jenkins for dealing with svn and deployment
Locally and on the distant server, the database is PostgreSQL9.1 and the database has the same name, same URL and same tables and data
Both glassfishs also have connection pools and JDBC resources set up
Now my problem, which occurs since last month, and I cannot figure out how to solve, is: while my application runs just fine locally (from eclipse or also just manually deployed into glassfish using maven for building the war and glassfish administration interface for the deployment), I get an error when I try to deploy the same war on glassfish on my distant server... Here is the error I get:
[ERROR] com.sun.enterprise.admin.cli.CommandException: remote failure: Exception while loading the app : java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'analysisDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of the FactoryBean's object failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.methodSecurityMetadataSourceAdvisor': Cannot resolve reference to bean 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource] while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.security.access.expression.method.ExpressionBasedAnnotationAttributeFactory] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'expressionHandler' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'expressionHandler' defined in ServletContext resource [/WEB-INF/spring/security-config.xml]: Cannot resolve reference to bean 'projectPermissionEvaluator' while setting bean property 'permissionEvaluator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectPermissionEvaluator': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public fr.inra.means.services.interfaces.IProjectService fr.inra.means.services.security.ProjectPermissionEvaluator.projectService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in ServletContext resource [/WEB-INF/spring/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.VerifyError: Expecting a stackmap frame at branch target 43 in method fr.inra.means.model.application.Projet.equals(Ljava/lang/Object;)Z at offset 4
I first asked this question about it, thinking that I must have had a maven configuration problem or so: java.lang.VerifyError when deploying the new version of my Java EE web application but using the same maven configuration locally works so it must be about glassfish configuration!
I even tried to switch to jdk1.6 (which was a pain because I don't like the idea to change that since I have developed with jdk1.7 for a year now)... It solved the deployment problem (the verifyError) but, same way, while it was working perfectly fine locally, when I ran the application on the server, a function crashed when it reached this line "CriteriaBuilder cb = em.getCriteriaBuilder();"...
(and no Exception could be found in my logs...)
Do you have any idea about what could cause this difference of behaviour between the 2 glassfish servers?
What is the configuration difference between my 2 glassfish servers which would explain this error?
I'm running out of ideas (and hope) so any help would be great!
Are you deploying the app in the 'Domain Admin Server' or a standalone instance?
If yes, create a standalone instance locally as well and try deployment there.
Applications should not really be running in 'Domain Admin Server' in production. Some frameworks behave differently there because 'Domain Admin Server' uses a different security manager by default IIRC.

Resources