多模块项目中的Spring配置

Dan*_*Kim 11 java spring spring-mvc spring-security

我是Spring的新手,并且遇到了包含一个Web模块的多个模块的单个项目.Web模块使用Spring MVC,但我想知道我是否可以在项目级别拥有主要的Spring配置来处理整个项目,这样我就可以充分利用Spring框架.

main
   -module1
   -module2
   -web
      +spring3.1, spring-security
Run Code Online (Sandbox Code Playgroud)

这种情况的最佳设置是什么?

web*_*pat 8

我工作的这种架构.我们决定在Web模块中使用ContextLoaderListener(Spring Event Listener).

然后,在serverApplicationContext.xml中,我们导入所有模块上下文文件:

<import resource="classpath*:module1ApplicationContext.xml" />
<import resource="classpath*:module2ApplicationContext.xml" />
...
Run Code Online (Sandbox Code Playgroud)

因此,您在Web应用程序上下文初始化期间利用spring上下文加载.


Tom*_*icz 6

构建布局和运行时CLASSPATH是两个不同的东西.即使您在不同的模块中定义单独的applicationContext.xml文件或@Configuration类,它们也可能合并到一个CLASSPATH中.

这就是说module1module2可能声明它们自己的上下文,但由于CLASSPATH在运行时合并,因此只会创建一个主上下文.此外,如果您选择在一个模块中使用CLASSPATH扫描,它可能会提取@Service其他模块中注释的类(bean).

web模块中,它也应该依赖于Spring核心库,也将依赖于spring-webMVC和spring-security.该模块将创建子web上下文,它可以访问主上下文,但不能反过来.

显然你的uber-JAR中只有一个库的副本(ear?)