java 将 Spring applicationContext.xml 放在非 Web 应用程序中的何处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10572191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:37:16  来源:igfitidea点击:

Where to put Spring applicationContext.xml in a non-web application?

javaspring

提问by itun

I am creating an application which uses Spring beans and I need to initialize ApplicationContextfrom xml. As it is not a server application, it doesn't have WEB-INFfolder.So, where I should put the xmlfile?

我正在创建一个使用 Spring bean 的应用程序,我需要ApplicationContextxml. 因为它不是服务器应用程序,所以它没有文件夹。WEB-INF所以,我应该把xml文件放在哪里?

回答by pd40

The spring documentation for this topic is a little overwhelming at first. A handy starting point in the spring documentation for application context config is here

这个主题的 spring 文档起初有点让人不知所措。应用程序上下文配置的 spring 文档中的一个方便的起点是here

A file system xml application contextis probably the easiest to start with. So:

一个文件系统的XML应用程序环境可能是最容易下手。所以:

ApplicationContext appCtx = 
    new FileSystemXmlApplicationContext("/path/to/springconfig.xml");

Use ClassPathApplicationContextif you have the xml configuration in your application class path. In this example it might be a springconfig.xml under a project directory src/config/springconfig.xml.

使用ClassPathApplicationContext,如果你在你的应用程序类路径中的XML配置。在此示例中,它可能是项目目录 src/config/springconfig.xml 下的 springconfig.xml。

ApplicationContext appCtx = 
    new ClassPathXmlApplicationContext("config/springconfig.xml");

If you are unsure where your springconfig.xml ends up after you have built your application, you can use the following command to list the contents of your jar:

如果在构建应用程序后不确定 springconfig.xml 的最终位置,可以使用以下命令列出 jar 的内容:

jar -tvf myapp.jar

For a default eclipse java project, the project-home/bindirectory is the start of the classpath.

对于默认的 eclipse java 项目,project-home/bin目录是类路径的开始。

A related question was asked here

一个相关的问题被问到这里

回答by Tomasz Nurkiewicz

Use ClassPathXmlApplicationContext:

使用ClassPathXmlApplicationContext

ApplicationContext ctx = 
  new ClassPathXmlApplicationContext("applicationContext.xml");

Or consider migrating to @Configuration:

或者考虑迁移到@Configuration

ApplicationContext ctx = 
  new AnnotationConfigApplicationContext(AppConfig.class);

where AppConfigis annotated with @Configurationand no XML is needed.

whereAppConfig被注释@Configuration并且不需要 XML。

回答by Srikanth Venkatesh

Check this example Spring Setter Injection

检查此示例Spring Setter Injection

If the XML is in the applications classpath then use like this

如果 XML 在应用程序类路径中,则像这样使用

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Otherwise if the XML is in File system then use

否则,如果 XML 在文件系统中,则使用

     ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");