我正在创建一个使用Spring beans的应用程序,我需要从xml
初始化ApplicationContext
。由于它不是服务器应用程序,因此它没有WEB-INF
文件夹。那么,我应该在哪里放置xml
文件?Spring applicationContext.xml
1
A
回答
1
检查这个例子Spring Setter Injection
如果XML是在应用程序类路径,然后使用类似这样的
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
否则,如果XML是在文件系统,然后使用
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
3
弹簧首先,这个主题的文档有点难以理解。在应用程序上下文配置的spring文档中,方便的起点是here
A file system xml application context可能是最简单的开始。所以:
ApplicationContext appCtx =
new FileSystemXmlApplicationContext("/path/to/springconfig.xml");
使用ClassPathApplicationContext
,如果你在你的应用程序类路径中的XML配置。在这个例子中,它可能是项目目录src/config/springconfig.xml下的springconfig.xml。
ApplicationContext appCtx =
new ClassPathXmlApplicationContext("config/springconfig.xml");
如果您不确定您的springconfig.xml最终您已经构建了应用程序后,您可以使用下面的命令列出JAR的内容:
jar -tvf myapp.jar
对于默认蚀java项目,project-home/bin目录是类路径的开始。
一个相关的问题被问here
3
使用ClassPathXmlApplicationContext
:
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
或者考虑迁移到@Configuration
:需要
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
其中AppConfig
标注有@Configuration
,没有XML。
相关问题
- 1. Spring MVC 3,applicationContext.xml
- 2. Spring MVC的java.io.FileNotFoundException - applicationContext.xml的
- 3. 从Spring引导调用Applicationcontext.xml
- 4. Spring ContextLoader无法找到applicationContext.xml
- 5. Spring - applicationContext.xml中的路径
- 6. Spring - 使用applicationContext.xml和XXXXX-servlet.xml
- 7. spring mvc applicationcontext.xml和spring servlet.xml的区别
- 8. 首先加载的Web.xml(tomcat)vs ApplicationContext.xml(spring)?
- 9. Spring Application Component Scanner not application from applicationContext.xml
- 10. 如何编写Spring ApplicationContext.xml文件?
- 11. 如何验证spring applicationContext.xml文件
- 12. spring 4.3中的applicationContext.xml验证错误?
- 13. applicationContext.xml问题,设置Hibernate,Spring MVC和MySql
- 14. 的applicationContext.xml如何解释在Struts和Spring
- 15. 有没有办法为Spring生成applicationContext.xml?
- 16. spring @Autowired和applicationContext.xml中的bean声明
- 17. hibernate.cfg.xml和applicationContext.xml
- 18. 关于/WEB-INF/applicationContext.xml
- 19. [/WEB-INF/applicationContext.xml]:java.lang.VerifyError:class org.hibernate.type.WrappedMaterialized
- 20. Eclipse RCP找不到applicationContext.xml
- 21. 在applicationContext.xml中
- 22. filenotfoundexception运行jerseyTest - 缺少applicationContext.xml
- 23. 如何从applicationContext.xml启动ActiveMQ
- 24. 如何在Spring的applicationContext.xml中注册Hibernate空拦截器
- 25. Spring DI applicationContext.xml xsi:schemaLocation究竟是如何使用的?
- 26. 如何阅读Spring应用程序中的JVM参数applicationContext.xml
- 27. 在Spring应用程序中处理* -context.xml和applicationContext.xml文件
- 28. 如何在spring中编写高效的applicationContext.xml
- 29. applicationcontext.xml来自另一个jar的spring服务
- 30. 无法在applicationContext.xml中找到Spring配置资源
我做了同样的事情,并将应用程序上下文xml放在src目录中。这是行不通的。我正在开发Eclipse RCP应用程序。 – itun
更新为包含一些类路径调试想法。我使用src/config/springconfig.xml对Eclipse进行了测试,它适用于我。 – pd40