开发者

Where should I keep the Spring applicationContext.xml in an EAR

开发者 https://www.devze.com 2023-04-01 14:03 出处:网络
I have service classes which are开发者_开发知识库 deployed in an EAR with no WAR or Web apps. In what folder structure should I ideally store the applicationContext.xml?

I have service classes which are开发者_开发知识库 deployed in an EAR with no WAR or Web apps. In what folder structure should I ideally store the applicationContext.xml?

Currently we load it as

Resource res = new ClassPathResource("META-INF/applicationContext.xml");


That's as good a place as any. It doesn't really matter, and I'm not aware of any convention for this.


If you are using Spring in a web application, you shouldn't ever load a ClasspathXmlApplicationContext manually. Your context will be a web application context, not a classpath application context, and will be loaded automatically if you put the following in your web.xml:

Code:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>

assuming your main Spring config file is called spring-config.xml and is located in the WEB-INF folder.

Loading classpath context programmatically is always wrong for web app, you should have one context loaded by listener and (optionally) one more context loaded by DispatcherServlet (if you use Spring web mvc as your mvc framework). I suggest you start by reading some documentation and try the Petclinic example to gain some insight in how to correctly use Spring in a web application.

Also, your ear packaging strategy is wrong; your Spring configuration is webapp specific so you should put your Spring config files inside the WAR, under the root of the web app (probably the best place is somewhere under WEB-INF, alongside web.xml and other descriptors). If you have resources like classes used by both ejb and webapp, you should package those classes inside a jar, put this jar under the lib directory in your ear and add a Class-Path entry for the jar in the MANIFEST.MF of webapp & each of the ejb jars. If you have config files with environment-dependant properties (like jdbc config and disk paths) you shouldn't package them inside your ear but instead let the application read it externally from the file system (PropertyPlaceholderConfigurer is your best shot).

For starters, I suggest you use Eclipse ear creation function to see how your web app should be packaged before you start using ant to build your project.

I also suggest you consider Maven as your compile - build - package strategy since this will automate and standardize the project and the web app maven archetype will let you create a correct pattern for j2ee web app in a blink.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号