FileSytemResources in Spring Framework

I am trying to get an XML file (containing a bean definition) in my Spring MVC project. If I have an xml file in the WEB-INF directory, then which path should I put in the FileSystemResource in my servlet to get xml?

i.e. BeanFactory factory = new XmlBeanFactory(new FileSystemResource("xml"));

thank

+3
source share
1 answer

Cannot be used FileSystemResource, you must use ServletContextResource:

new ServletContextResource(servletContext, "/myfile.xml");

Assuming, of course, that servletContext is available to you.

If you really want to use FileSystemResource, then you need to ask the container the directory is in and use it as a relative path, for example.

String filePath = servletContext.getRealPath("/myfile.xml");
new FileSystemResource(filePath);

Spring . , bean, Resource. , Spring , .

public class MyBean {

   private Resource myResource;

   public void setMyResource(Resource myResource) {
      this.myResource = myResource;
   }
}

beans:

<bean id="myBean" class="MyBean">
   <property name="myResource" value="/path/under/webapp/root/of/my/file.xml">
</bean>

Spring ServletContextResource bean.

+2

Source: https://habr.com/ru/post/1735407/


All Articles