I want to create bean using BeanFactory, but I am getting an exeception: java.io.FileNotFoundException: \\WEB-INF\businesscaliber-servlet.xml.
Resource res = new FileSystemResource("//WEB-INF//businesscaliber-servlet.xml");
BeanFactory factory = new XmlBeanFactory(res);
if (factory != null && beanId != null) {
obj = factory.getBean(beanId);
}
he its working using this
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:/WEB-INF/businesscaliber-servlet.xml");
I believe you need to specify an absolute path and not a Web application relative path to FileSystemResource.
Try using ServletContextResource instead.
Resource implementation for
ServletContext resources,
interpreting relative paths within the
web application root directory.
The only issue is you need the ServletContext so:
ServletContext servletContext = ...
Resource res = new ServletContextResource(servletContext,
"/WEB-INF/businesscaliber-servlet.xml");
BeanFactory factory = new XmlBeanFactory(res);
if (factory != null && beanId != null) {
obj = factory.getBean(beanId);
}
It's worth noting that ideally you would retrieve this from an ApplicationContext. From 4.4 Resource Loader of the Spring Reference:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt);
What would be returned would be a
ClassPathResource; if the same
method was executed against a
FileSystemXmlApplicationContext
instance, you'd get back a
FileSystemResource. For a
WebApplicationContext, you'd get
back a ServletContextResource, and
so on.
As such, you can load resources in a
fashion appropriate to the particular
application context.
So this is the preferred method of retrieving resources.
Alternatively since /WEB-INF/ is technically in the classpath you can use the classpath: prefix (as per your comment) or use ClassPathXmlApplicationContext (which will automatically return classpath resources).
Also theres no need to put double forward slashes in. Not sure why you're doing this. Perhaps a holdover from double backslashes, which are necessary?
Related
I have a default package called com.voja.springtest and another one called com.voja.springtest.beans where I have an beans.xml file.
I can get it like so using FileSystemXmlApplicationContext :
ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/Voja/Desktop/_/vj/springtest/src/main/java/com/voja/springtest/beans/beans.xml");
But ClassPathXmlApplicationContext can't find it like so (and it should per the tutorial I am doing):
ApplicationContext context = new ClassPathXmlApplicationContext("com/voja/springtest/beans/beans.xml");
Why?
you use wrong parh , in your case it should be like :
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:beans.xml");
4.7.2.2 The classpath*: prefix
When constructing an XML-based application context, a location string
may use the special classpath*: prefix:
ApplicationContext ctx =
new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml"); This
special prefix specifies that all classpath resources that match the
given name must be obtained (internally, this essentially happens via
a ClassLoader.getResources(...) call), and then merged to form the
final application context definition.
The Classpath*: portability classpath*: prefix
FileSystemXmlApplicationContext picks the XML file from absolute path by appending keyword "file" and also can fetch from classpath by appending keyword "classpath".
You can access the file as below
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-app.xml");
I am absolutely confused with application context in spring. If i use spring (simple spring) create a beans.xml and then invoke Application context from (for example) main() method.
ApplicationContext context = new FileSystemXmlApplicationContext
("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
all works well. But I don't understand if i move file on directory above or in another directory(for example) it will be ok?
in spring-mvc there is context for each DispatcherServlet which i create and where i specify some beans, there is common context for all servlets, how to specify this? in web.xml?
in general, please explain me this moment (I read spring in action, i undesrstand almost all, but these tricky moment isn't shown there.
From FileSystemXmlApplicationContext java doc:
Standalone XML application context, taking the context definition files from the file system or from URLs, interpreting plain paths as relative file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses as well as for standalone environments.
The key words here are context definition files, so you can pass paths to as many xml-files, as you want. Besides that, you can create an application context and pass it to the new one as a parent:
FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent)
Thus you can easily create the needed hierarchy of contexts.
ApplicationContext parentContext = new FileSystemXmlApplicationContext
("C:/some/path/ParentBeans.xml");
ApplicationContext childContext = new FileSystemXmlApplicationContext
(new String[]{"C:/some/path/ChildBeans1.xml", "C:/some/path/ChildBeans2.xml"}, parentContext);
if i move file on directory above all in another directory(for example) it will be ok?
As long as your path to file is correct and reachable - it's Ok.
I want to get servletContext in a Java class to read a file from WEB-INF directory. I extended my class with HttpServlet and tried to get the context as in the below code, but the servlet config is returned as null. I don't use any jsp or controller. My intention is to read a file directly placed in the WEB-INF directory from a Java class. Please let me know how I can get not null servletConfig / servletContext in the class:
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/samplefile");
Trap for young players. If you override the
public void init(ServletConfig config)
method, you must call
super.init(config);
inside the method. Otherwise the superclass sees the context as null. It's mentioned in the Javadoc:
When overriding this form of the method, call super.init(config).
NB You can get the context directly via getServletContext(). There's no need to go via getServletConfig().
I had this same issue and it turned out the web.xml file was created in the wrong place and was not being loaded by the container.
It needs to be created in the root of the WEB-INF folder. Ideally let Eclipse do this for you when you create the project.
Is there a way to create an ApplicationContext (or whatever else in Spring you can use to do getBean("beanName") ) by passing in an XML file that is in memory? The only methods I've been able to find involve providing a file or directory.
You could give it a try:
import org.springframework.context.support.GenericXmlApplicationContext;
String xmlDef = "...";
ApplicationContext ctx = new GenericXmlApplicationContext(new InputStreamResource(new ByteArrayInputStream(xmlDef.getBytes("UTF-8"))))
I have java spring application, that uses in xml files placeholders values of them taken from app.properties. Is it possible to create test or something similar without starting application and getting up spring context that names all placeholders used in xml files (where beans are configured) are correct, nothing missing, nothing misspelled etc. ?
Thanks.
One idea would be to get a reference to the ApplicationContext and actually create every bean. This should throw an exception if something was not defined correctly.
String[] beanNames = getAppContext().getBeanDefinitionNames();
for (int i = 0; i < beanNames.length; i++)
{
BeanDefinition beanDefinition = getAppContext().getBeanFactory()
.getBeanDefinition(beanNames[i]);
if (!beanDefinition.isAbstract())
{
getAppContext().getBean(beanNames[i]);
}
}