springMVC not able to locate the config directory - spring

my springMVC project not able to locate the config directory
My application directory structure is--
myapplication
+src/javaApplication
+webcontent
+web-inf
+jsp
+lib
+config
while accessing this configuration I am getting an error
SEVERE: Servlet.service() for servlet [springsocial] in context with path [/socialmedia] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
java.util.NoSuchElementException: 'babelnet.minEdgeWeight' doesn't map to an existing object
I am trying to use babelnet in springMVC application. where should i put the configuration details in spring please suggest.
thanks in advance.

In general, your file hierarchy should probably be something like this:
/src
/main
/java
/resources
/webapp
/WEB-INF
/test
/java
Configuration files that need be be on the classpath should go into the /src/main/resources directory. Config files that you reference directly can go pretty much anywhere, but probably belong either in the resources folder or the WEB-INF folder, depending on the context of their use. Spring MVC configuration files should go into /src/main/webapp/WEB-INF.

Related

giving Freemarker resource folder in deployed application for spring boot

I'm trying to give resource folder for Freemarker template below is my bean config
Configuration freeMarkerConfig() throws IOException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new ClassPathResource("ftl").getFile());
return cfg;
}
and I have ftl folder in the resources folder
-- java
-- resources
-- ftl
-- template.ftl
while deploying it works fine in my local machine but fails in my docker container with exception
java.io.FileNotFoundException: class path resource [ftl] cannot be resolved to absolute file
path because it does not reside in the file system: jar:file:!/BOOT-INF/classes!/ftl
I need to keep ftl in the resources folder and give the directory path to configuration. I don't know how to debug further.
Property spring.freemarker.template-loader-path is available.
spring.freemarker.template-loader-path=classpath:/ftl/
Or, use setTemplateLoaderPath("classpath:/ftl/") instead of setDirectoryForTemplateLoading().
I found the issue after repeated searching. Issue is because of this
Classpath resource not found when running as jar

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 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.

Spring - applicationContext.xml cannot be opened because it does not exist

I have a Spring MVC application and a problem with JUnit tests combined with the file applicationContext.xml.
In my JUnit test class I write:
final ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
service = (TestServiceImpl) context.getBean("testServiceImpl");
The error I get is that aplicationContect.xml can not be found:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
But it exists in the WEB-INF folder.
So, what's wrong here? Why does the file not exist for the JUnit test?
You should keep your Spring files in another folder, marked as "source" (just like "src" or "resources").
WEB-INF is not a source folder, therefore it will not be included in the classpath (i.e. JUnit will not look for anything there).
If you use maven, create a directory called resources in the main directory, and then copy your applicationContext.xml into it.
From your java code call:
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
I got the same error.
I solved it moving the file applicationContext.xmlin a
sub-folder of the srcfolder. e.g:
context = new ClassPathXmlApplicationContext("/com/ejemplo/dao/applicationContext.xml");
The ClassPathXmlApplicationContext isn't going to find the applicationContext.xml in your WEB-INF folder, it's not on the classpath. You could copy the application context into your classpath (could put it under src/test/resources and let Maven copy it over) when running the tests.
I also found this problem. What do did to solve this is to copy/paste this file everywhere and run, one file a time. Finally it compiled and ran successfully, and then delete the unnecessary ones. The correct place in my situation is:
This is under the /src/ path (I am using Intellij Idea as the IDE). The other java source files are under /src/com/package/ path
Hope it helpes.
This happens to me from time to time when using eclipse
For some reason (eclipse bug??) the "excluded" parameter gets a value *.* (build path for my resources folder)
Just change the exclusion to none (see red rectangle vs green rectangle)
I hope this helps someone in the future because it was very frustrating to find.
Click on the src/main/java folder and right click and create the xml file. If you create the application.xml file in a subpackage other than /, it will not work.
Know your structure is look like this
package/
| subpackage/
| Abc.java
| Test.java
/application.xml
enter image description here
I was struggling since a couple of hours for this issue because i was putting that file under resources folder but it didn't help me, finally i realized my mistake.
Put it directly under src/main/java.
For me, it worked by keeping file(applicationContext.xml) in the resources folder
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
I placed the applicationContext.xml in the src/main/java folder and it worked
I solved it moving the file spring-context.xml in a src folder.
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
I fixed it by adding applicationContext.xml into jar/target/test-classes for Maven project. And use
XmlBeanFactory bf = new XmlBeanFactory( new ClassPathResource(
"/applicationContext.xml", getClass() ) )
instead of
XmlBeanFactory bf = new XmlBeanFactory( new ClassPathResource(
"/WEB-INF/applicationContext.xml", getClass() ) )
My solution:
If you have no folder WEB-INF please put the file applicationContext.xml into the folder source(src).
Then Java Project can read file applicationContext.xml -> getBean -> perform your business.
enter image description hereThe solution is to place the xml file in resources folder(src->main-> resources) and use this object creation new ClassPathXmlApplicationContext("applicationContext.xml");
Create a Directory at the bottom of main directory named resources. That solved my issue.
In Spring all source files are inside src/main/java.
Similarly, the resources are generally kept inside src/main/resources.
So keep your spring configuration file inside resources folder.
Make sure you have the ClassPath entry for your files inside src/main/resources as well.
In .classpath check for the following 2 lines. If they are missing add them.
<classpathentry path="src/main/java" kind="src"/>
<classpathentry path="src/main/resources" kind="src" />
So, if you have everything in place the below code should work.
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring-Module.xml");
Please do This code - it worked
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
o/w: Delete main method class and recreate it while recreating please uncheck Inherited abstract method its worked
I'm using Netbeans, i solved my problem by putting the file in: Other Sources default package, then i called it in this way:
ApplicationContext context =new ClassPathXmlApplicationContext("bean.xml");
resources folder
While working with Maven got same issue then I put XML file into src/main/java path and it worked.
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
You actually need to understand the ApplicationContext. It is an interface and it will have different implementations based on configuration.
As you are using new ClassPathXmlApplicationContext("applicationContext.xml"); , kindly pay attention to initial right hand-side , it says ClassPathXmlApplicationContext , so the XML must be present in the class path.
So drag your applicationContext.xml wherever it is to the src folder.
Gist: new ClassPathXmlApplicationContext as the name
[ClassPathXml]will look for the xml file in the src folder of your
project, so drag your xml file there only.
 ClassPathXmlApplicationContext—Loads a context definition from an XML
file located in the classpath, treating context definition files as classpath
resources.
 FileSystemXmlApplicationContext—Loads a context definition from an XML
file in the file system.
 XmlWebApplicationContext—Loads context definitions from an XML file contained
within a web application.
just change the containing package of your applicationContext.xml file.
applicationContext.xml must be in src package not in your project package.
e.g.
src(main package)
com.yourPackageName(package within src)
classes etc.
applicationContext.xml(within src but outside of yourPackage or we can say
parallel to yourPackage name)
your-module/src/applicationContext.xml
val context = ClassPathXmlApplicationContext("applicationContext.xml")
Check the directory path, the default path is /src/ and not /.
GL
I got the same issue while working on a maven project, so I recreate the configuration file spring.xml in src/main/java and it worked for me.
I solved it by moving the file applicationContext.xml in an src folder and main folder.
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

Resources