多模块项目中的Spring配置 [英] Spring configuration in multi-module project

查看:281
本文介绍了多模块项目中的Spring配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

main
   -module1
   -module2
   -web
      +spring3.1, spring-security

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

What would be the best setting for this case?

推荐答案

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

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.

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

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.

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

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.

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

Obviously you should have only a single copy of each library in your uber-JAR (ear?)

这篇关于多模块项目中的Spring配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆