前面文章提到了ApplicationContext对国际化的支持,下面看一下它对资源访问的支持。
由于ApplicationContext实现了ResourceLoader接口,我们就可以使用它提供的getResource();方法进行资源的访问。它提供了三种方式:
1,通过虚拟路径来访问,这种方式一般对于资源文件在CLASSPATH下的访问方式,看一下代码:
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
Resource reource = context.getResource("classpath:message.properties");
2,通过实际路径来访问:
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
Resource reource = context.getResource("file:c:/eclipse/workspace/myApp/WEB-INF/src/message.properties");
3,通过相对路径来访问:
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
Resource reource = context.getResource("WEB-INF/src/message.properties");
当然当取得了resource后,就可以利用它提供的各种方法对资源文件进行操作了。
由于ApplicationContext实现了ResourceLoader接口,我们就可以使用它提供的getResource();方法进行资源的访问。它提供了三种方式:
1,通过虚拟路径来访问,这种方式一般对于资源文件在CLASSPATH下的访问方式,看一下代码:
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
Resource reource = context.getResource("classpath:message.properties");
2,通过实际路径来访问:
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
Resource reource = context.getResource("file:c:/eclipse/workspace/myApp/WEB-INF/src/message.properties");
3,通过相对路径来访问:
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
Resource reource = context.getResource("WEB-INF/src/message.properties");
当然当取得了resource后,就可以利用它提供的各种方法对资源文件进行操作了。