如何在spring中编写高效的applicationContext.xml

时间:2012-10-12 08:38:20

标签: java spring javabeans

我刚开始学习春天。 使用spring在应用程序开发中编写ApplicationContext xml文件时应该避免什么,以便代码有效? (我的意思是在编写ApplicationContext xml文件时可以遵循的良好做法,请不要给我一个反对票,这是一个真正的怀疑)

1 个答案:

答案 0 :(得分:5)

以下几点需要考虑的事项

1)模块化 - 我们可以明智地划分applicationContext.xml模块并将其导入主applicationContext.xml中 示例:applicationContext.xml code snippest

<import resource="classpath:dbConfig.xml"/>
<import resource="classpath:userModuleContext.xml"/>
<import resource="classpath:customerModuleContext.xml"/>

2)数据库配置可以存储在单独的属性文件中,然后使用 PropertyPlaceholderConfigurer

在applicationContext.xml中使用

3)Bean范围应该适合原型,单例,请求,会话

singleton - 表示每个弹簧容器的单个实例。它可能会在集群环境中引起问题。

4)Bean依赖注入应该是合适的,这样它不应该导致循环依赖,如A ---&gt; B ---&gt; C ---&gt;甲

还有更多......