I'm new to Spring and got a situation that single project with multiple modules including one web module. Web module uses Spring MVC, but I was wondering if I can have main Spring configuration at project level that takes care of whole project, so that I can take full advantages of Spring framework.
我是Spring的新手,有一个项目有多个模块,包括一个web模块。Web模块使用Spring MVC,但是我想知道我是否可以在项目级别上使用主Spring配置来管理整个项目,以便充分利用Spring框架。
main
-module1
-module2
-web
+spring3.1, spring-security
What would be the best setting for this case?
对于这种情况,最好的设置是什么?
2 个解决方案
#1
6
The build layout and runtime CLASSPATH are two different things. Even if you define separate applicationContext.xml
files or @Configuration
classes in different modules, they might get merged into a single CLASSPATH.
构建布局和运行时类路径是两个不同的东西。即使您定义了单独的applicationContext。不同模块中的xml文件或@Configuration类,可能会合并到一个类路径中。
That being said module1
and module2
might declare their own contexts, but since the CLASSPATH is merged at runtime, only a single main context will be created. Also if you choose to use CLASSPATH scanning in one module, it might pick-up classes (beans) annotated with @Service
in other modules.
也就是说,module1和module2可能声明它们自己的上下文,但是由于类路径在运行时被合并,因此只创建一个主上下文。此外,如果您选择在一个模块中使用类路径扫描,它可能会在其他模块中使用带有@Service注释的类(bean)。
In web
module, which should also have dependencies on Spring core libraries, will also depend on spring-web
, MVC and spring-security
. This module will create child web
context, that has access to main context but not the other way around.
在web模块中,也应该依赖于Spring核心库,也将依赖于Spring -web、MVC和Spring -security。这个模块将创建子web上下文,它可以访问主上下文,但不能反过来。
Obviously you should have only a single copy of each library in your uber-JAR (ear
?)
显然,您应该在您的uber-JAR (ear?)中只有每个库的一个副本。
#2
8
We have this kind of architecture where I work. We decided to use the ContextLoaderListener (Spring Event Listener) in the web module.
我工作的地方就有这样的建筑。我们决定在web模块中使用ContextLoaderListener (Spring事件监听器)。
Then, in the serverApplicationContext.xml, we import all the modules context files :
然后,在serverApplicationContext。xml,我们导入所有的模块上下文文件:
<import resource="classpath*:module1ApplicationContext.xml" />
<import resource="classpath*:module2ApplicationContext.xml" />
...
Thus you leverage the spring context loading during the web application context initialization.
因此,您可以在web应用程序上下文初始化期间利用spring上下文加载。
#1
6
The build layout and runtime CLASSPATH are two different things. Even if you define separate applicationContext.xml
files or @Configuration
classes in different modules, they might get merged into a single CLASSPATH.
构建布局和运行时类路径是两个不同的东西。即使您定义了单独的applicationContext。不同模块中的xml文件或@Configuration类,可能会合并到一个类路径中。
That being said module1
and module2
might declare their own contexts, but since the CLASSPATH is merged at runtime, only a single main context will be created. Also if you choose to use CLASSPATH scanning in one module, it might pick-up classes (beans) annotated with @Service
in other modules.
也就是说,module1和module2可能声明它们自己的上下文,但是由于类路径在运行时被合并,因此只创建一个主上下文。此外,如果您选择在一个模块中使用类路径扫描,它可能会在其他模块中使用带有@Service注释的类(bean)。
In web
module, which should also have dependencies on Spring core libraries, will also depend on spring-web
, MVC and spring-security
. This module will create child web
context, that has access to main context but not the other way around.
在web模块中,也应该依赖于Spring核心库,也将依赖于Spring -web、MVC和Spring -security。这个模块将创建子web上下文,它可以访问主上下文,但不能反过来。
Obviously you should have only a single copy of each library in your uber-JAR (ear
?)
显然,您应该在您的uber-JAR (ear?)中只有每个库的一个副本。
#2
8
We have this kind of architecture where I work. We decided to use the ContextLoaderListener (Spring Event Listener) in the web module.
我工作的地方就有这样的建筑。我们决定在web模块中使用ContextLoaderListener (Spring事件监听器)。
Then, in the serverApplicationContext.xml, we import all the modules context files :
然后,在serverApplicationContext。xml,我们导入所有的模块上下文文件:
<import resource="classpath*:module1ApplicationContext.xml" />
<import resource="classpath*:module2ApplicationContext.xml" />
...
Thus you leverage the spring context loading during the web application context initialization.
因此,您可以在web应用程序上下文初始化期间利用spring上下文加载。