我是Spring的新手,并且遇到了包含一个Web模块的多个模块的单个项目。 Web模块使用Spring MVC,但我想知道我是否可以在项目级别拥有主要的Spring配置来处理整个项目,这样我就可以充分利用Spring框架。
main
-module1
-module2
-web
+spring3.1, spring-security
这种情况的最佳设置是什么?
我是Spring的新手,并且遇到了包含一个Web模块的多个模块的单个项目。 Web模块使用Spring MVC,但我想知道我是否可以在项目级别拥有主要的Spring配置来处理整个项目,这样我就可以充分利用Spring框架。
main
-module1
-module2
-web
+spring3.1, spring-security
这种情况的最佳设置是什么?
构建布局和运行时CLASSPATH是两回事。即使你单独定义 applicationContext.xml
文件或 @Configuration
他们在不同模块中的课程 威力 合并到一个CLASSPATH中。
话虽如此 module1
和 module2
可能会声明自己的上下文,但由于CLASSPATH在运行时合并,因此只会创建一个主上下文。此外,如果您选择在一个模块中使用CLASSPATH扫描,它可能会提取带注释的类(bean) @Service
在其他模块中。
在 web
模块,也应该依赖于Spring核心库,也将依赖于 spring-web
,MVC和 spring-security
。这个模块将创建孩子 web
上下文,可以访问主要上下文,但不能反过来。
显然,你的uber-JAR中每个库只应该只有一个副本(ear
?)
我工作的这种架构。我们决定在Web模块中使用ContextLoaderListener(Spring Event Listener)。
然后,在serverApplicationContext.xml中,我们导入所有模块上下文文件:
<import resource="classpath*:module1ApplicationContext.xml" />
<import resource="classpath*:module2ApplicationContext.xml" />
...
因此,您在Web应用程序上下文初始化期间利用spring上下文加载。