Springboot war deployment to tomcat 8.5 - java

I have built a spring boot war file using maven, When I try to deploy the war file to an external tomcat server in command prompt. I get an exception.
Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' 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/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 java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.OracleDriver
I am using two application.properties files one in src/main/resources that has the actual datasource config to Oracle and another in src/test/resources that has a datasource for a H2 in memory database.
Please let me know if I missed anything, Like placing the property file in config under tomcat folder etc.

I suppose you have set oracle.jdbc.OracleDriver as driver-class-name on your application.properties under src/main/java, right? Did you remember to include this dependency on pom.xml similar to what follows?
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
Edit
Try adding this to tomcat's context.xml:
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="xxx" password="xxx" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#localhost:1521:xe"/>

Okay, The root cause of this is there is no oracle6 jar in my war file - Even though I added the dependency in my pom.xml.
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>6</version>
<scope>system</scope>
<systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>
Therefore I've added the jar explicitly into my Tomcat lib and it worked.

I added the Datasource Configuration using the programmatic way as below and it worked, not sure why couldn't it be picked from application.properties.
#Configuration
public class DatasourceConfig {
#Bean
public DataSource datasource() {
return DataSourceBuilder.create()
.driverClassName("com.mysql.cj.jdbc.Driver")
.url("jdbc:mysql://localhost:3306/test_db")
.username("username")
.password("password")
.build();
}
}
Credits to Baeldung article here

Related

Spring Boot JPA with Camel JPA throws Failed to instantiate [com.zaxxer.hikari.HikariDataSource]

When created a Spring Boot with JPA and Camel JPA, and throws below "Failed to instantiate [com.zaxxer.hikari.HikariDataSource] ... " error. If exclude the hikary depency for spring-boot-data-jpa start inside the pom.xml, this goes away. Finding difficulties in including a db connection with spring boot jpa and camel-jpa.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.22.jar:5.3.22]
... 107 common frames omitted
It is missing mysql reference.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Assuming you're using mysql, the driver must be on the classpath. If you're using maven, that means your pom must declare the mysql driver as a dependency.

Spring boot spark application

I am using Spring Boot to implement a spark application. I build an uber jar, and use spark-submit to execute the application. But I got the following exception:
18/06/05 15:32:37 ERROR SpringApplication: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonBuilder' defined in class path resource
[org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [com.google.gson.GsonBuilder]: Factory method
'gsonBuilder' threw exception; nested exception is
java.lang.BootstrapMethod
Error: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1254)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103)
.....
The exception is still there even if I put the following dependency in my pom.xml:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
How can I resolve this dependency issue?
Maybe you can use #SpringBootApplication(exclude = {GsonAutoConfiguration.class}) to disable gson autoconfiguration.

Spring Boot JNDI not found - can't compile WAR with Maven

i can't mvn clean install my project.
I need to use a remote JNDI in WAR(not jar), but having the name in application.properties throws error during mvn clean install(due to "datasource jndi name not found")
if I have to use jndi name from a remote server, that I have no control over, how do i compile my package in WAR without lookup fail, but let it look up during the deployment/installation of the app on the server? any hint, or direction is very much appreicated...
in app prop, i have
spring.datasource.jndi-name=jdbc/ff-app
I have tried: Spring boot JNDI datasource lookup failure - Name comp/env/jdbc not found in context "java:"
tried, http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-connecting-to-a-jndi-datasource
and pretty much every questions asked in stackoverflow about a remote jndi..
Some of the errors:
Caused by: 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/ff-app'; 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
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
... 69 common frames omitted
Caused by: org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/ff-app'; 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
at org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup.getDataSource(JndiDataSourceLookup.java:48) ~[spring-jdbc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration.dataSource(JndiDataSourceAutoConfiguration.java:62) ~[spring-boot-autoconfigure-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration$$EnhancerBySpringCGLIB$$9509801a.CGLIB$dataSource$0() ~ ........
My POM.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
</dependencies>
The point is that when you try to compile by using maven, maven tries to execute also Junit tests; in your case you have in src/resources/java some unit tests and so maven tries to execute them. Since you are using a JNDI datasource, it means that also the servlet container must be active for executing tests otherwise you can't get connection.
In this case when maven tries to execute tests maybe no servlet container (and, so, no JNDI datasource) is active and your test throws error.
To solve this (and to compile the maven artifact) you must:
or exclude tests execution: this is done by adding <maven.test.skip>true</maven.test.skip> in pom.xml. No test will be executed
or ignore tests error this is done by adding <maven.test.failure.ignore>true</maven.test.failure.ignore> in pom.xml. In this case tests will be executed but any test error will not stop maven compilation
The configuration I gave not only tells maven to not execute tests but it tells maven to not stop on any test error
I hope i have been clearer... my english is not the best

Why dependency order in POM impacts to bean visibility

I'm trying to wire up one component to existing system. To do that I included dependency to my component (DataFilter) into main project POM file.
Order 1)
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>DataFilter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>CommandHandler</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Such order (only dependencies in POM file; no import calls) cause that system during start pre-instantiate following beans:
from main app (blueprint.xml)
[activemq,parser,rawStorage,commandDAO,CommandService,payloadStorage,payloadRetrieval,rawReply,rawRetrieval,template,consumerTemplate,app-context:beanPostProcessor,app-context];
from component DataFilter
[filters,hdop_filter_pass_less_than_4,vdop_filter_pass_less_than_4,speed_filter_pass_less_than_high_speed,event_and_gps_time_diff_less_than_exp_time,fix_type_recomended_to_3dfix,always_pass_when_motion_or_stationary]
and don't see the bean iCommandService what cause that rawReply in main app can not be created
Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rawReply' defined in class path resource [OSGI-INF/blueprint/packaging-jar-camel-context-xml.txt]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.company.product.CommandHandler.InitialClass]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'iCommandService' is defined
When I switch the dependencies to order 2) (CommandHandler first)
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>CommandHandler</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>DataFilter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Following behaviour appear:
main app still pre-instantiate beans as above
[activemq,parser,rawStorage,commandDAO,CommandService,payloadStorage,payloadRetrieval,rawReply,rawRetrieval,template,consumerTemplate,app-context:beanPostProcessor,app-context]
component command handler (which beans wasn't visible in order 1)) pre-instantiate its beans
[iCommandService,commandDAO]
component data filter is not even mentioned during setting up the camel context; no pre-instantiating of data filter beans, no errors about missing beans.
camel context starts and built routes properly
Additionally
Command Handler and Data Filter keep its contexts in Beans.xml
Main app in blueprint.xml (or packaging-jar-camel-context-xml.txt when build as jar, not bundle)
Data Filer is compilable as OSGi bundle
No autowire is used
The questions are:
why the dependecies order influance on visibility or not creating beans
how to set it properly to achive the moment when all beans for all three components are created properly?

Java: Error creating bean with name 'loadTimeWeaver'?

I am currently working on a maven project but every time i try to deploy my war i get the exception below:
ERROR [DispatcherPortlet:276] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransforme r)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
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:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
at org.springframework.web.portlet.FrameworkPortlet.createPortletApplicationContext(FrameworkPortlet.java:356)
at org.springframework.web.portlet.FrameworkPortlet.initPortletApplicationContext(FrameworkPortlet.java:294)
at org.springframework.web.portlet.FrameworkPortlet.initPortletBean(FrameworkPortlet.java:268)
at org.springframework.web.portlet.GenericPortletBean.init(GenericPortletBean.java:120)
I simply don't understand when i have everything in place properly why this error. Please checkout my declaration in POM.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<forkMode>once</forkMode>
<argLine>
-javaagent:"path\spring-instrument-3.1.0.RELEASE.jar"
</argLine>
<useSystemClassloader>true</useSystemClassloader>
</configuration>
</plugin>
Please guide.
If you are using tomcat bundle and to enable load-time-weaving in Tomcat add Spring Instrument Tomcat to Tomcat's lib directory and the following in the context.xml inside the META-INF directory of your tomcat:
<Context path="/test">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
Refer to the Spring Docs on the tomcat's configuration.
Note: The configuration for tomcat differs based on the version.
Though this question was asked long time back, I am still answering it. Somebody else might find it useful.
If you are using Tomcat try adding the following in the catalina.bash for windows or catalina.sh for linux servers:
set CATALINA_OPTS=%CATALINA_OPTS% -javaagent:"C:\path\to\spring-instrument-3.1.1.RELEASE.jar"
Keep in mind it will work only in Tomcat versions less than 7.0.55. After that it wont take it. And you will need to add it in the context.xml as mentioned here load-time-weaver (aspectj-weaver) is not working in apache tomcat 7.0.55.

Categories

Resources