ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource - java

I was trying to do some basic database operations. I'm using STS 3.2.0, Apache Tomcat 7.
My dispatcher-servlet.xml includes:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
Apache Tomcat fails miserably with thousands of exceptions, starting with this one :
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.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
EDIT 1: I also downloaded 3.0.3.RELEASE version of the jar, and its still the same.
FINAL EDIT It seems, you should add your external jar files manually to your deployment directory as well (well if you don't use any extension that'd do that for you)

Try to add to POM.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

I think you should import selected jdbc jar into web app library.

if none of the above solution doesn't work, then manually add spring-jdbc jar file to webapp/WEB-INF/lib.

Related

Multiple Persistent Units JPA

I have a Spring application, which is configured for multiple Persistent units.
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list value-type="java.lang.String">
<value>classpath*:META-INF/*-persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="dataSource" value-ref="dataSource" />
</map>
</property>
<property name="defaultDataSource" ref="dataSource" />
<property name="defaultPersistenceUnitName" value="pm-model" />
</bean>
However, I'm getting an error on startup saying that one of the persistent units is not able to load. I also called out in the code above to list all the persistence.xml files, but still getting the same error.
ERROR 2017-08-10 15:33:44,882 [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bService': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com-model' is defined
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)
If anyone has any suggestions, I'm all ears. Thanks in advance.

Spring Connectivity with Postgres DB

Trying to connect to Postgres DB, context params are as below:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/****" />
<property name="username" value="****" />
<property name="password" value="****" />
</bean>
Getting exception on context loading:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in ServletContext
resource [/WEB-INF/ApplicationContext.xml]: Error setting property
values; nested exception is
org.springframework.beans.PropertyBatchUpdateException; nested
PropertyAccessExceptions (1) are: PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property
'driverClassName' threw exception; nested exception is
java.lang.IllegalStateException: Could not load JDBC driver class
[org.postgresql.Driver]
I have the postgres driver in the LIB of the project, as I can check the class file org.postgresql.Driver. The Class.forName also gives a positive result for the driver class.
I have tried all versions of postgres drivers, but still this isn't going through.
Make sure the lib gets copied to the packaging correctly and that that directory is in the classpath.
I was able to resolve the issue , When I added the Postgres DB Jar to the WEBINF/Lib the bean was getting created , Some how it was not picking the jar from the JavaResource/Lib as i was doing this before .
The problem is a particular case which is not answered in most of the places well and just checking the classpath wont be a correct answer to this query.

Spring URL protocol doesn't like LDAPS?

I have configured the following beans in my application context:
<bean id="bindConnectionPool" parent="abstractConnectionPool"
p:connectionFactory-ref="bindConnectionFactory" />
<bean id="bindConnectionFactory"
class="org.ldaptive.DefaultConnectionFactory"
p:connectionConfig-ref="bindConnectionConfig" />
<bean id="bindConnectionConfig" parent="abstractConnectionConfig" />
<bean id="abstractConnectionConfig" abstract="true"
class="org.ldaptive.ConnectionConfig"
p:ldapUrl="ldaps://myldap.example.com:636"
p:connectTimeout="3000"
p:useStartTLS="true"
p:sslConfig-ref="sslConfig" />
<!-- EDIT/UPDATE: -->
<bean id="sslConfig" class="org.ldaptive.ssl.SslConfig">
<property name="credentialConfig">
<bean class="org.ldaptive.ssl.X509CredentialConfig"
p:trustCertificates="mycert" />
</property>
</bean>
When I deploy the WAR to Tomcat I get the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name ‘bindConnectionPool’ defined in ServletContext resource
[/WEB-INF/deployerConfigContext.xml]: Invocation of init method failed; nested exception
is java.lang.IllegalArgumentException: path must start with either classpath: or file:
What do I need to change the LDAPS-based URL to? Neither file: nor classpath: make sense here...thoughts?
Or, is ldapUrl a red herring, and is there some other path defined on this bindConnectionPool bean that isn't configured right? Oh, and obviously, can't post the LDAP server's actual name for security reasons (myldap.example.com is a different value).

javax.persistence.PersistenceException : [PersistenceUnit: vodPersistenceUnit] class or package not found

I got the following error :
Error creating bean with name
'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'
defined in class path resource [jpaDaoContext.xml]: Initialization of
bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'vodEntityManagerFactory' defined in class
path resource [jpaDaoContext.xml]: Invocation of init method failed;
nested exception is javax.persistence.PersistenceException:
[PersistenceUnit: vodPersistenceUnit] class or package not found
I had a look on Google and I was told to choose transaction-type=RESOURCE_LOCAL but the settings were already that way. What is wrong with these settings :
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- transaction-type is RESOURCE_LOCAL or JTA -->
<persistence-unit name="vodPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<class>mypackage.persistent.HistoriqueAction</class>
<class>mypackage.persistent.ParametresTechniques</class>
<class>mypackage.persistent.TicketType</class>
<class>mypackage.persistent.TransactionType</class>
<class>mypackage.persistent.StatutSession</class>
<class>mypackage.persistent.Statistique</class>
<class>mypackage.persistent.StatUser</class>
<!-- Avoid to scan *.class and *.hbm.xml -->
<exclude-unlisted-classes />
</persistence-unit>
</persistence>
Regards
I fixed the issue. I had to comment these three lines in the file "persistence.xml" :
<!--class>mypackage.persistent.TicketType</class>
<class>mypackage.persistent.TransactionType</class>
<class>mypackage.persistent.StatutSession</class-->
For the moment i have no idea why it fixes the issue. It is really hard to debug this spring file.
If you had to comment out the "class" elements, it is likely that one of those classes is either not defined, or is not available in the classpath.
I faced the same exact error, and it was resolved once the fully qualified names were all correct. Ideally, Hibernate should tell you what class is not found, but sadly it does not do it in this case.
You you haven't done that, put <property name="persistenceUnitName" value="vodPersistenceUnit" /> in your jpaDaoContext.xml as property of your entityManagerFactory bean definition like:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="vodPersistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">...</property>
</bean>
I was running into this exception when trying to run a Spring Boot application in WebLogic 12.1.3 In the dependency tree I found out spring-tx was being included from one of the common project libraries. Our particular app only calls web service so there is no need for database access. So in the library dependency I added:
<exclusions><exclusion> <groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></exclusion></exclusions>

Context Initialization Failure -- BeanCreationNotAllowedException

The Spring Context of my application is failing to initialize. Can anyone help me understand why it is failing and how to fix it?
Below are the warning & error messages I'm getting:
[WARN] Invocation of destroy method 'shutdown' failed on bean with
name 'cxf'
org.springframework.beans.factory.BeanCreationNotAllowedException:
Error creating bean with name 'entityManagerFactory': Singleton bean
creation not allowed while the singletons of this factory are in
destruction (Do not request a bean from a BeanFactory in a destroy
method implementation!)
org.springframework.web.context.ContextLoader
[ERROR] Context initialization failed
<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" destroy-method="shutdown"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="${persistence.unit}"/>
<property name="dataSource" ref="pooledDs"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<property name="showSql" value="false"/>
<property name="generateDdl" value="false"/>
</bean>
</property>
</bean>
The CXF website doesn't include the destroy-method call in its example configurations, so it seems like this is a misconfiguration. See this page for details: http://cxf.apache.org/docs/interceptors.html.
I also found a bug tracker for this issue: https://issues.apache.org/jira/browse/CXF-2164. It appears that the destroy method was not implicitly being called in earlier versions of CXF, but that has been fixed in v2.2.11.
So, my suggestion would be to get up to at least that version and just have <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" /> in your config.
In my case I had same problem with version 2.5.0, but that was my fault.
I had wrong bean in context.
In detail: I had Spring MVC Controller (named OrderController) annotated with #Controller without defined name (annotation driven). On the other CXF requires xml configuration AFAIK, so I named bean using java configuration (using #Bean) as orderController and somehow when cxf was initialized Spring used this wrong MVC controller and it failed on error listed above.
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'cxf': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

Categories