Groovy Spring Bean Classpath - spring

I am attempting to run a groovy script on startup for a web application. I am first trying to follow an example from the book Spring Recipes.
I have a SimpleInterestCalculator in the following package - com.apress.springrecipes.interest
The beans package is in src/test/resources in a package com.wer.com, and that's where the bean is.
My entry in that is
<lang:groovy id="interestCalculator" script-source="classpath:com/apress/springrecipes/interest/SimpleInterestCalculator.groovy">
<lang:property name="rateCalculator" value="rateCalculator" />
</lang:groovy>
I am getting the following error.
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'simpleInterestCalculator':
Could not determine scripted object type for GroovyScriptFactory:
script source locator [classpath:src/com/apress/springrecipes/interest/SimpleInterestCalculator.groovy];
nested exception is java.io.FileNotFoundException:
class path resource [src/com/apress/springrecipes/interest/SimpleInterestCalculator.groovy] cannot be opened because it does not exist

The error you are getting is a FileNotFoundException. This means it is not finding the script file itself. If the file is present, then it may not be on the classpath at runtime.
Check your bin/out directory in your IDE to see if the .groovy file is present there. Sometimes if you have a .groovy in a source directory, your IDE assumes this is 'source' code and compiles it down to bytecode. The result is you would end up with a .class file on the classpath instead of the groovy script itself. This will vary with settings and what IDE you are using. If this seems to be the problem there may be an option for this or you can use an alternate extension that will not be picked up by the compiler.

Related

Spring Boot is unable to locate ftl files in executable jar from cmd

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot find template location(s): [ServletContext resource [/templates]] (please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)
I kept ftl files in templates folder in resources.
In application.properties I also added these lines
spring.freemarker.template-loader-path= /templates
You have overridden the default Spring Boot property value of:
spring.freemarker.template-loader-path=classpath:/templates
and now you are pointing to a file system directory /templates. Remove your custom property to load the FTL files from the default classpath location.

Spring Boot 'bootJar' task does not build fat jar, instead excludes .original

bubbleshadow% ls -l build/libs
total 83544
-rw-r--r-- 1 dlynch staff 42772394 May 14 08:37 bubbleshadow-0.0.1-SNAPSHOT.jar
When running the above .jar file (which is supposed to be a fat jar), I get the following error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path res
ource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
I have attempted to follow instructions from:
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/
In 8.1 it says Reacting (sic) to the Java Plugin:
And then:
Creates a BootJar task named bootJar that will create an executable (sic), fat jar for the project.
My build runs within IntelliJ IDEA but not when executing on the command line.
According to Josh Long's Book Learning Spring Boot 2.0 - Second Edition, see it says:
If we peek at the output, we'll find the original JAR file (non-FAT) along with the rebundled one containing our application code as well as the third-party dependencies, as shown here: $ ls build/libs
learning-spring-boot-0.0.1-SNAPSHOT.jar
learning-spring-boot-0.0.1-SNAPSHOT.jar.original
Turnquist, Greg L.. Learning Spring Boot 2.0 - Second Edition: Simplify the development of lightning fast applications based on microservices and reactive programming (Kindle Locations 664-667 (sic)). Packt Publishing. Kindle Edition.
I don't understand why the .original jar is not being produced and the jar that it is producing for me is un-runnable.

Grails 3 (Gradle) dependency without parent directory

Can I not use the following Gradle approach to dependencies in Grails? I do not have nor want a parent directory;
https://stackoverflow.com/a/19303545/2288004
When I try it, I get the the following error;
Caused by: java.lang.IllegalStateException: Expected method not found:
java.lang.NoSuchMethodException:
org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.addApplicationListener(org.apache.catalina.deploy.ApplicationListener)
It works when I use a parent directory for the settings.gradle, but unfortunately it’s not how I want to structure the project.
The following was indeed the solution I was looking for,
include ":myplugin"
project(':myplugin').projectDir = new File(settingsDir, '../myplugin')
The error was down to how I was managing my tomcat dependencies between the two projects.
Tomcat was already being pulled in via the plugin but while I still needed to reference tomcat at compile time in the application, I also needed to make sure it was the same version, and so added the following just above "dependencies" to target the version I required;
ext['tomcat.version'] = '7.0.70'

Spring configuration error WEB-INF/servlet-context.xml FileNotFoundException

I have a very simple spring test app. But I get exception even though everything seems to be on order. I might be missing something. Please check the pic to see the project structure and web.xml file contains as well as exception:-
efinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/servlet-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/servlet-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
There are two kinds of resources in a servlet environment:
servlet resources - files uner the root of the web application (loaded via ServletContext)
classpath resources - resources on web application's classpath (loaded via ClassLoader)
When Spring is supposed to load its configuration it needs to know which mechanism to use.
classpath:foo/bar.xml - will load as classpath resource
checking WEB-INF/classes, contents of WEB-INF/lib/*.jar and other shared servlet container's classpath locations
when using maven and its project structure, all files from src/main/resources will be placed on classpath
foo/bar.xml - will load as servlet resource
when using maven and its project structure the src/main/webapp folder is the root of your application
TL;DR As I wrote in the comment, either remove classpath: prefix when referencing XML file or move your XML file to src/main/resources and remove the WEB-INF part.

Unable to invoke 'hadoop jar' command for class, which contains Spring persistance unit

The problem is that, the jar file uses Spring ORM for loading the persistance configurations, and based on these configurations, files are moved to suitable folders in HDFS. Now If i use, 'java -cp' instead of 'hadoop jar', it fails to copy to HDFS, with FileSystem error.
While invoking the jar with hadoop jar command (having spring orm injected) the exception is as:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'
defined in class path resource [applicationContext.xml
Error creating bean with name 'entityManagerFactory' defined in class
path resource [applicationContext.xml]: Invocation of init method
failed; nested exception is java.lang.IllegalStateException:
Conflicting persistence unit definitions for name 'Persistance':
file:/home/user/Desktop/ABC/apnJar.jar,
file:/tmp/hadoop-ABC/hadoop-unjar2841422106164401019/
Caused by: java.lang.IllegalStateException: Conflicting persistence
unit definitions for name 'Persistance'
Seems like Hadoop is unpacking the jar file to some tmp folder, is this really required?
Can we skip this step by any configuration change?
Any thoughts on this are welcome.
If you use "hadoop jar", hadoop will run org.apache.hadoop.util.RunJar. RunJar will unpackage your jar into a temp folder(In your case is /tmp/hadoop-ABC/hadoop-unjar2841422106164401019/) and load it in the current class loader. At last, it will invoke your main class to run your MapReduce application.
Did you add your jar in the CLASSPATH? If so, you will have your jar and the unpackaged folder in the class loader. I think that's why spring complains it.
As a work around, I extracted the configuration xmls from the jar, and placed them in the working directory.
This worked, however looking for the proper solution.
Hence If you are facing the similar issue, remove all configuration xmls and place the jar with only the compiled class files.

Resources