I am using multiple projects in a single .war and it successfully works in my tomcat.
When I try to load the spring context manually for testing, It gives exception while loading .xml from other projects.Below I am sharing the details of my project.
build tool : ANT
Context Loading method :
ApplicationContext context = new FileSystemXmlApplicationContext("file:resources/spring/root-context.xml");
Exception :
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [classpath:com/abc/service/config/test.xml]; nested exception is java.io.FileNotFoundException: com/abc/service/config/test.xml (No such file or directory)
test.xml is part of core project and Loading via config Class.
#Configuration
#ImportResource({
"classpath:com/abc/service/config/test.xml",
})
I could not understand why spring context loaded in tomcat and failed in manual loading. Please someone help to figure out the problem.
Related
My application has application.yml in the main and test resources folder however application (Spring boot 2.4 with Maven) is complaining that application.properties is not found.
Below is the error.
2021-07-08 04:37:33.381 ERROR --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener#bd4ee01] to prepare test instance [com.test.SomeTests#6a7aa675]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
...
Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:187)
I have tried to find solution online but could not able to find one. Please help.
This is not a sufficient description for your problem, where did you put the application.yml file ? if you are in a maven projet project, you should put it under src/main/resources folder.
Also I'm not sure if spring is able to interpret .yml file extension of your file, try to change it to application.yaml instead.
Note from spring.io :
You can explicitly specify your application properties location by setting the program arguments with as :
--spring.config.location=your_properties_path
Our application should use a .properties file which is created in deploy time on the host filesystem, thus it is not being bundled with the application .war in build time. It is opened and read by a loader class during the Spring application context creation
<bean id="propertiesLoader" class="com....PropertiesLoader">
<property name="propertiesFileLocation" value="${PROPERTIES_FILE_LOCATION}"/>
...
where the file path value of PROPERTIES_FILE_LOCATION is defined in theapplication.properties file in src\main\resources of the application's -web maven module
PROPERTIES_FILE_LOCATION=/home/.../propertyfilename.properties
and the type of the propertiesFileLocation field is String and it is being accessed via new FileInputStream(propertiesFileLocation) in PropertiesLoader.java class
With this setup the application context creation is successful. However, we also have some integration tests in our project which also setup a Spring context for their execution. We though to store a copy of propertyfilename.properties file into src\test\resources of the -it maven module and refer it from its it.properties like
PROPERTIES_FILE_LOCATION=classpath:somedirectory/propertyfilename.properties
however during the creation of the integration test context we receive the following exception
[ERROR] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'propertiesLoader' defined in URL
[ERROR] Caused by: java.io.FileNotFoundException: classpath:somedirectory\propertyfilename.properties
(The filename, directory name, or volume label syntax is incorrect)
Now if we modify the type of the propertiesFileLocation field to org.springframework.core.io.Resource and we access it via propertiesFileLocation.getInputStream() in PropertiesLoader.java, that makes the integration tests context creation succesful, but at the same the real application context creation fails with
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertiesLoader' defined in URL
[file:/.../tomcat/work/Catalina/localhost/_/loader/conf/spring/springmvc/springmvc-util.xml]: Invocation of init method failed;
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/home/.../propertyfilename.properties]
This seems to be a "deadlock" situation, as either the application or the integration test context creation fails. Is there a way with which both context can be succesfully created?
Resource loading is quite well documenten in the Spring References Guide. When you don’t prefix the resources with the location to load it from it will default to the default location of the ApplicationContext. For the web application that will be the root of the archive, basically what is in the WEB-INF directory. \
You want to load a file resource for this prefix the resource with file: and in your test use classpath: to have it loaded from the `classpath. This way it will work regardless of which default location the application context uses.
I am getting above exception when i try to run simple spring example
Following line of code try to read spring beans from beans1.xml
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
Following is the complete exception i am getting.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans1.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans1.xml] cannot be opened because it does not exist
I have put xml file in every possible location hoping that program will detect the file. But still this program can not find this bean configuration file. Also i am using maven as the build tool, but i am running the program using Eclipse IDE
put it in resources directory of maven project
src/main/resources/beans1.xml
check the name,your bean.xml file name should be same as what you have mentioned here ie., beans.xml ClassPathXmlApplicationContext("beans1.xml"):
i try to use iReport with an hibernate connection loaded using spring xml config file.
So, in netbeans wizard, i choose new datasource and i select "spring loaded Hibernate collection".
I set my xml spring file, but when i click test i have this exception:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [META-INF/spring.cfg.xml]; nested exception is org.springframework.beans.FatalBeanException: NamespaceHandler class [org.springframework.context.config.ContextNamespaceHandler] for namespace [http://www.springframework.org/schema/context] not found; nested exception is java.lang.ClassNotFoundException: org.springframework.context.config.ContextNamespaceHandler
The spring xml is ok beacouse al works great in application, the problem is when i try to use it for iRepor in netbeans plugin.
Documentation about jasper report using hibernate and spring are very very slightly.
EDIT
I added all spring-dependance jar in Tools->Options->iReport->classpath (i'm refering to netbeans, this is totaly undocumented...) and finally seem to load context, but now i have this excpetions:
WARNING [org.netbeans.ProxyClassLoader]: Will not load class org.springframework.beans.factory.xml.NamespaceHandlerSupport arbitrarily from one of ModuleCL#10eaeda[com.jaspersoft.ireport] and ModuleCL#19e3bdd[org.netbeans.libs.springframework] starting from SystemClassLoader[569 modules]; see http://wiki.netbeans.org/DevFaqModuleCCE
WARNING [org.netbeans.ProxyClassLoader]: Will not load class org.springframework.beans.factory.xml.NamespaceHandler arbitrarily from one of ModuleCL#10eaeda[com.jaspersoft.ireport] and ModuleCL#19e3bdd[org.netbeans.libs.springframework] starting from SystemClassLoader[569 modules]; see http://wiki.netbeans.org/DevFaqModuleCCE
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [META-INF/spring.cfg.xml]; nested exception is org.springframework.beans.FatalBeanException: Class [org.springframework.context.config.ContextNamespaceHandler] for namespace [http://www.springframework.org/schema/context] does not implement the [org.springframework.beans.factory.xml.NamespaceHandler] interface
I think this is a problem of the netbeans plugin only and i can do anything :(
Thanks.
Installing iReport standalone app all work good, so is a problem of iReport plugin in netbeans.
I've got problems starting my WAR application on a local JBoss. After two other EARs are deployed and the TomcatDeployer begins deploying the WAR, I'm getting the following error message:
2010-04-28 10:01:56,605 ERROR [org.jboss.ejb.plugins.LogInterceptor] [] [main] EJBException in method: public abstract [return type] methode throws javax.ejb.CreateException,java.rmi.RemoteException, causedBy:
javax.ejb.EJBException: org.springframework.beans.factory.access.BootstrapException: Unable to initialize group definition. Group resource name [classpath*:context.xml], factory key [contextService]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service' defined in URL [jar:file:/C:/jboss-4.2.3.GA/server/default/deploy/frontend.war/WEB-INF/lib/modules.jar!/aontext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [package/context.xml]; nested exception is java.io.FileNotFoundException: class path resource [package/context.xml] cannot be opened because it does not exist
But package/context.xml actually is placed inside a JAR in one of my EAR files which should be deployed before the WAR. And at least I get a message that the deployment of the EAR has been successful. I also looked into the JAR with my file archiver and the context.xml is indeed there at the right place.
Is there a way for me to get sure that the JAR, not the EAR as a whole, is really deployed to the JBoss? I'm already starting to lose my head about this issue.
Thank you.
Bernhard
Could be a classloader issue. You can check whether your JAR and its contents are loaded properly by configuring a logger for classloading events, as described here (example 3.8).
The log also gives you information about which classloader loaded what - this might explain why your web app (loaded by its own loader) does not see stuff within the EAR.
Another thing: is the JAR containing the XML file included in the classpath of the WAR?
I have deployed my war in another deploy directory called "deploy.last" in the JBoss directory, since it has to be -- as the name suggests -- deployed at last.
Now I've changed my JBoss configuration and put the war in the higher-ranking deploy directory (where the two EARs are too) and everything is working fine now. The war is still deployed at last.
I don't have an idea why this worked, but it worked. :D