Spring ClassPathXmlApplicationContext cannot load xml - spring

I want to load applicationContext xml by ClassPathXmlApplicationContext.
I use this method can load
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
but After I create a folder, Spring can't load applicationContext
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("venom/spring/study/spring-context.xml");
this is my module struct

I dont think you need to specify the path as long as those folders are inside resources folder, you just need to specify the XML file name like this:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

Related

How to add beans loaded via FileSystemXmlApplicationContext to an existing context loaded via ClassPathXmlApplicationContext?

I have an old application that has all its spring batch beans in class path; these beans get loaded in application context using ClassPathXmlApplicationContext. Now, there is a need where I can put these spring batch beans outside my war/classpath.
If you could please help me here? I can not change existing code and if I load these new definitions using FileSystemXmlApplicationContext, how could I merge them with existing context?
Thanks !
You could try using #ImportResource in your new application to load the bean definitions from an XML resource:
#Configuration
#ImportResource({
"classpath:your-old-applicationContext.xml",
"classpath:another-old-applicationContext.xml"
})
public class NewApplicationConfig {
}

Can't get Spring bean

I have a Spring project in which I want to get a specific Spring bean defined in my Spring beans XML File. My Spring bean XML file is located at /WEB-INF/spring/root-context.xml.
Here is my code in the Service class:
ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/spring/root-context.xml");
Here is the error I get when I compile :
parsing XML document from class path resource [WEB-INF/spring/root-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-context.xml] cannot be opened because it does not exist
Probably WEB-INF is not inside your classpath. I suggest to move the xml file in the classpath (for example src/main/resources/spring/root-context.xml). Then you can access it with: ApplicationContext context = new ClassPathXmlApplicationContext("/spring/root-context.xml"); If you're inside web application, Spring Mvc looks for th context in WEB-INF/<the name of the dispatcher servlet>-servlet.xml
If you want to access the context from WEB-INF you can load it using
GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new FileSystemResource(path));
A better way is using WebApplicationContextUtils or implement ApplicationContextAware. Not sure what your use case is...

Spring applicationContext not loading in a POJO

My applicationContext is placed at path:src\main\webapp\WEB-INF\conf\applicationContext.xml
When I build this web application.I can see that conf folder is placed at {projectname.war}\WEB-INF\conf\applicationContext.xml.And Application works fine.
Now I happened to write a class with public static void main where I am trying to load applicationCOntext using below:
ApplicationContext context = new ClassPathXmlApplicationContext("../conf/applicationContext.xml");
ClassPathXmlApplicationContext("/WEB-INF/conf/applicationContext.xml");
ClassPathXmlApplicationContext("applicationContext.xml");
none of above seems to be working.
error is listed below:
Caused by: java.io.FileNotFoundException: class path resource [../conf/applicationContext.xml] cannot be opened because it does not exist.
Can someone point what wrong I am doing ?

Avoid applicationContext.xml in my Action and Service Classes

I am writing a web application using Struts, Spring framework.
In the Struts Action I am Injecting Service Classes as follows
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
loginService = (LoginService)context.getBean("loginService");
How can I avoid mentioning applicationContext.xml in my Action classes I just have to use
loginService = (LoginService)context.getBean("loginService"); without specifying the .xml file in my class.
I came across to use below while googling
private LoginService loginService = (LoginService)ApplicationContextProvider.getContext().getBean("loginService");
But I do not want to use static method getContext().
Spring support Struts integration in the package org.springframework.web.struts.
Take a look at this article for an example.

Spring, using new ClassPathXmlApplicationContext and getting error being unable to find applicationContext.xml and others?

I am trying to follow this tutorial: http://www.vogella.de/articles/SpringDependencyInjection/article.html to use annotation dependency injection in my application. I set up the bean, etc like in the tutorial and then am trying to get an instance of the bean within my MainController class (a controller class that handles generating a specific page for my spring web mvc app).. I keep getting
SEVERE: Servlet.service() for servlet spring threw exception
java.io.FileNotFoundException: class path resource [WEB-INF/applicationContext.xml] cannot be opened because it does not exist
I am doing this in my MainController:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory factory = context;
BeanIRPlus beanirPlus = (BeanIRPlus) factory
.getBean("BeanIRPlus");
IRPlusInterface irPlus = beanirPlus.getIRPlus();
I have searched and searched on this and yet to find an answer that fixes my problem. My applicationContext in in webapp/WEB-INF/ and my spring app seems to be working otherwise as it was handling requests, etc before this. I have tried putting the applicationContext.xml in WEB-INF classes but still nothing. Is there any workaround to make this not search the path this way as I think its doing a relative path search. Thanks for any advice
Not a direct answer, but here goes.
The tutorial you have referred is for dependency injection in a standalone application and not a web application. In case of web application, spring automatically loads the context files and initializes the beans. So you would not need any of the lines specified in the MainController.
Instead, you could do something like this to use beanIRPlus bean in your controller.
#Autowired
private BeanIRPlus beanIRPlus;

Resources