PDA

View Full Version : Config File access issue


rsrch
May 6th, 2008, 01:00 PM
I have a silly problem. For most of you it may be cake walk. Please help me to resolve this.

My project structure is

project
/WEB-INF
/classes/com/../..
/config
/jsp
/lib
/tld


My issue is I have a applicationContext.xml config file.
When I placed my applicationContext.xml config file in /WEB-INF/classes folder I am able to access it using the bean finder class with location simply "applicationContext.xml" and everything is working fine.

But when I placed applicationContext in any other folder my bean finder class
is throwing FileNotFound Exception.

When I placed it in config folder I am not able to access with relative path "/config/applicationContext.xml" or "config/applicationContext.xml".

And also I tried to access by placing directly under WEB-INF folder and
using path "/WEB-INF/applicationContext.xml" I failed access it from my bean.


public class BeanFinder {

/** Logger for this class and subclasses */
protected final Log log = LogFactory.getLog(getClass());

private static ApplicationContext ctx;

public Object getBean(ServletRequest request, String beanName) {
if (ctx == null) {
if (!(request instanceof HttpServletRequest)) {
throw new IllegalArgumentException("Can only process HttpServletRequest");
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
ctx = getContext(httpRequest);
}
Object obj = new Object();
return obj;

}

public Resource getResource(String location){
Resource template = new ClassPathXmlApplicationContext().getResource(locat ion);
return template;
}

public Object getBean(String beanName) throws BeansException {

// String[] configLocation = "file:../WEB-INF/config/applicationContext-hibernate.xml";
// ctx = new FileSystemXmlApplicationContext(configLocation);
// String[] configLocation = {"classpath:/config/applicationContext.xml"};
String[] configLocation = {"classpath:applicationContext.xml"};
ctx = new ClassPathXmlApplicationContext(configLocation);
log.info("Context: " + ctx);
Object obj = ctx.getBean(beanName);
return obj;

}

/**
* Allows test cases to override where application context obtained from.
*
* @param httpRequest which can be used to find the <code>ServletContext</code>
*
* @return the Spring application context
*/
public ApplicationContext getContext(HttpServletRequest request) {
return WebApplicationContextUtils.getRequiredWebApplicati onContext(
request.getSession().getServletContext());
}



I want to access my config file by placing it under config folder. How can I achieve this. Please help me.

Thanks in advance.

rsrch

noon
May 7th, 2008, 12:34 AM
Just to make sure...

Is your application a web application? In that case usually (!!) you don't have to pull any beans from the application context. Usually the beans are "pushed" by Spring IoC (Inversion of Control).

See the chapter in the reference manual
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html

See also the Spring examples which comes with the Spring distribution.

If you need to pull beans from application context e.g. in a Spring controller, you can request beans from the application context with


this.getApplicationContext().getBean("yourBeanId");


The application context files should be inserted directly under WEB-INF directory (by this they are also protected files by Servlet container and cannot be accessed from outside e.g. by a browser).

Again... study the Spring examples :)

rsrch
May 8th, 2008, 09:00 AM
Thanks noon.

As I told you if I place my config files in the same directory as class files everything is working fine for me. I will stick to that.