Spring singletons is instantiated before JNDI DataSourceBinding occured - java

I am a newbie to Java EE, I have some issue where spring singletons which have property of a JndiObjectFactoryBean is instantiated before the JNDI itself is bound. It cause an error on deployment and make the application deployment failed.
Is there anyway to configure the priority of the initialization? Such as make the JNDI DataSourceBinding instantiated first before the spring singletons?
I am using jboss-5.1.0.GA as my application server.
Here is the snippet of the server.log:
2015-05-25 16:47:14,410 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (main) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#1f873f7: defining beans [daoAuthenticationProvider,authenticationManager,settingbean,authenticationFailureHandler,authenticationSuccessHandler,sessionRegistry,sessionAuthenticationStrategy,authenticationFilter,sessionManageProcessingFilter,httpSessionContextIntegrationFilter,filterChainProxy,securityContextPersistenceFilter,filterSecurityInterceptor,httpRequestAccessDecisionManager,roleVoter,cxcdataDS,datasourceByJNDI,JTATransactionManager,jdbcTXManager,jdbcTemplate,jdbcTemplateext]; root of factory hierarchy
2015-05-25 16:47:14,593 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (main) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#1f873f7: defining beans [daoAuthenticationProvider,authenticationManager,settingbean,authenticationFailureHandler,authenticationSuccessHandler,sessionRegistry,sessionAuthenticationStrategy,authenticationFilter,sessionManageProcessingFilter,httpSessionContextIntegrationFilter,filterChainProxy,securityContextPersistenceFilter,filterSecurityInterceptor,httpRequestAccessDecisionManager,roleVoter,cxcdataDS,datasourceByJNDI,JTATransactionManager,jdbcTXManager,jdbcTemplate,jdbcTemplateext]; root of factory hierarchy
2015-05-25 16:47:14,593 ERROR [org.springframework.web.context.ContextLoader] (main) Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoAuthenticationProvider' defined in ServletContext resource [/WEB-INF/classes/CXC_Security.xml]: Cannot create inner bean 'org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl#1437781' of type [org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl] while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl#1437781' defined in ServletContext resource [/WEB-INF/classes/CXC_Security.xml]: Cannot resolve reference to bean 'cxcdataDS' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cxcdataDS' defined in ServletContext resource [/WEB-INF/classes/CXC_DataSource.xml]: Cannot resolve reference to bean 'datasourceByJNDI' while setting bean property 'targetDataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasourceByJNDI' defined in ServletContext resource [/WEB-INF/classes/CXC_DataSource.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: cxcdataDS not bound
...
and only after long stack trace of error the required JNDI binding is instantiated
2015-05-25 16:47:16,759 INFO [org.jboss.resource.connectionmanager.ConnectionFactoryBindingService] (main) Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=cxcdataDS' to JNDI name 'java:cxcdataDS'
I would really appreciate if somebody could help me for this.
Thanks

In the end the singletons is instantiated before JNDI is bound due to issue during deployment, for some reason the application is deployed to inside of a .sar folder belong to the JBoss. Because of that the application is loaded before the JNDI is instantiated.
After I move my application to the correct folder, it is running okay as before.

Related

Failed to look up JNDI DataSource

This problem may seems redundant but I can't find a suitable solution for my specific problem.
So, I have been using basic JDBC template to make connection to the DB tables in my spring boot application. I have few more features to add and I wanted to follow JPA route. I added spring.datasource.jndi-name and spring.jpa.database-platform to the application.properties file correctly(same as once that are working in my other applications).
Although I'm able to run the current application(with both JPA and JDBC connection) locally, I'm getting following error while building it on weblogic remote server.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc.myDataSource'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Can anyone help me with this ?

JNDI #Resource in Spring

I have a Java EE application with WildFly, and I use:
#Resource(lookup = "java:/jms/queue/MyQueue")
but now I am migrating part of this application to Spring one, and I have no idea how to look up for my JNDI resource.
In application.yaml I added:
spring:
datasource:
jndi-name: "java:jms/queue/MyQueue"
but it throws an error during startup:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRoute': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'java:/jms/queue/MyQueue' defined in JNDI environment: JNDI lookup failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial
MyRoute.java:
#Resource(lookup = "java:/jms/queue/MyQueue")
private Queue queue1;
Is it possible to easily inject the resource in Spring?
I wish it can work like #Autowired does. Inject the Queue with lookup into JNDI.

Tomcat work directory empty

My Spring project was successfully deployed on another Linux server. But when I tried deploying it on this Linux server, it won't deploy. When I go to the homepage, I get the error "HTTP Status 404 - The requested resource is not available".
I compared the Tomcat directory of both servers and see only differences in the "work" directory. On this server (the one having problem), under "work\Catalina\localhost\MyProject" directory of Tomcat, I see that it's empty. Turns out that the files got put under "work\Catalina\localhost\_" instead. What's causing Tomcat to do this?
In my tomcat log file, I'm getting this:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SampleService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wdc.hydra.dao.DatatypeDAO com.wdc.hydra.service.DatatypeServiceImpl.datatypeDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SampleDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Sample' defined in class path resource [Sample-dao-context.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
Does anyone know why this is happening? Thanks.
How does the Spring Container defined? whether its by XmlWebApplicationContext / FileSystemXmlApplicationContext / ClasspathXmlApplicationContext and if its through FileSystemXmlApplicationContext then your directory structure in both the linux servers should be same. like wise others too.

Roo project import error in eclipse(sts)

I get an error when I want to import my project to sts(eclipse). I generate this project with spring roo and its work an other laptop so I just want to import and run on tomcat server somebody know what is this, I watch the dependecies and this class is there (I use Windows):
2012-10-16 23:32:42,540 [main] ERROR
org.springframework.web.context.ContextLoader - Context initialization
failed org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'transactionManager' defined in file
[D:\projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\roo-jquery-bootstrap\WEB-INF\classes\META-INF\spring\applicationContext.xml]:
Cannot resolve reference to bean 'entityManagerFactory' while setting
bean property 'entityManagerFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'entityManagerFactory' defined in file
[D:\projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\roo-jquery-bootstrap\WEB-INF\classes\META-INF\spring\applicationContext.xml]:
Invocation of init method failed; nested exception is
java.lang.IllegalArgumentException: Cannot find class
[org.hibernate.ejb.HibernatePersistence] at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at
... 41 more
I solved that it was an eclipse bug i restarted the eclipse and the application worked..

DispatcherServlet - Context initialization failed in Roo project

I develop a Roo project and deploy to a Tomcat 6 server.
Up until now everything was working fine.
I deleted and re-added an entity.
Now everytime when I start Tomcat I get this exception in the Spring Tool Suite:
SEVERE: StandardWrapper.Throwable
Throwable occurred: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'applicationConversionService' while setting constructor argument; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [myproject.web.ApplicationConversionServiceFactoryBean] for bean with name 'applicationConversionService' defined in ServletContext resource [/WEB-INF/spring/webmvc-config.xml]; nested exception is java.lang.ClassNotFoundException: myproject.web.ApplicationConversionServiceFactoryBean
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [myproject.web.ApplicationConversionServiceFactoryBean] for bean with name 'applicationConversionService' defined in ServletContext resource [/WEB-INF/spring/webmvc-config.xml]; nested exception is java.lang.ClassNotFoundException: myproject.web.ApplicationConversionServiceFactoryBean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
...
What do I need to do to make it work again?
spring is complainig about a bean not being available (probably the one you say you deleted).
exception is java.lang.ClassNotFoundException: myproject.web.ApplicationConversionServiceFactoryBean
try to search the config xml files & remove the reference made to this class, things should be back after that,
I had the same exact issue.
The problem was that I disabled the "Project > Build Automatically" feature, so I had to manually build the project.
Once I manually built the project everything went back to normal. :)
So, don't forget to build the project before deploying it, or just re-enable the "Project > Build Automatically" feature.
Hope it helps.
Your app deployed to Tomcat doesn't have a jar file in WEB-INF/lib that contains the class myproject.web.ApplicationConversionServiceFactoryBean. I don't know what that class is or where you'd find it, but it's missing.
my workaround (worked with eclipse and roo 1.1.5 as eclipse plugin)
open ApplicationConversionServiceFactoryBean.java class
delete class annotation #RooConversionService
wait for roo update
add back class annotation #RooConversionService
wait for roo update and try again
Inside of STS, click ->project->clean
It clean and discard all build problems and built states, The next time a build occurs the projects will be rebuilt from scratch.

Categories