使用 ClassPathXmlApplicationContext 的 applicationContext.xml 的正确路径
我在 WEB-INF/config/applicationContext.xml 中使用 applicationContext.xml 工作 Web 应用程序。现在我需要将一些测试工具实现为独立应用程序,它可以使用相同的 applicationContext.xml,但 ClassPathXmlApplicationContext 类的配置路径存在问题。
我知道当我将 applicationContext.xml 复制到默认包(.java 所在的位置)时,我可以使用 ClassPathXmlApplicationContext("applicationContext.xml"),但这有必要吗?
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AdminTool {
private Logger log = Logger.getLogger(getClass());
/** all below doesn't work - FileNotFoundException) **/
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/config/applicationContext.xml");
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("../config/applicationContext.xml");
public static void main(String[] args) {
new AdminTool();
}
public AdminTool() {
log.debug(ac);
}
}
I have working web-application with applicationContext.xml in WEB-INF/config/applicationContext.xml. Now i need to implement some testing tool as standalone application, that can use the same applicationContext.xml, but have problem with path to config for ClassPathXmlApplicationContext class.
I know that when i copy applicationContext.xml to default package (where .java resides) i can use ClassPathXmlApplicationContext("applicationContext.xml"), but is this necessary ?
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AdminTool {
private Logger log = Logger.getLogger(getClass());
/** all below doesn't work - FileNotFoundException) **/
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/config/applicationContext.xml");
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("../config/applicationContext.xml");
public static void main(String[] args) {
new AdminTool();
}
public AdminTool() {
log.debug(ac);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请转到属性>Java构建路径>项目源检查类路径并将目录添加到类路径中的WEB-INF之前。绝对是工作
please go to property>Java Build Path>Source of your project check the classpath and add the directory before WEB-INF in the classpath. definitely it's work
这就是为什么我更喜欢将所有配置文件放在 classes 文件夹中的原因。事实上,使用 Maven,您可以将所有这些文件放在 src/main/resources 中。然后,在您的测试文件中,您可以通过“classpath:applicationContext.xml”进行访问。
This is the reason why I prefer to put all configuration files in classes folder. In fact, using Maven, you can put all these files in src/main/resources. Then, in your test files you can access as 'classpath:applicationContext.xml'.
在本地测试场景中,您不在 Jar 内,因此不需要基于类路径的访问。使用
FileSystemXmlApplicationContext
相反。可能是这样的:(
路径是相对于执行目录的)
In a local test scenario, you are not inside a Jar, so you don't need Classpath-based access. Use
FileSystemXmlApplicationContext
instead.Probably something like this:
(paths are relative from the execution directory)