不同的spring配置文件角色

时间:2016-03-12 16:14:18

标签: java xml spring hibernate spring-security

我正在开发基于Spring的Web应用程序,使用 Spring Framework + Spring Security + Hibernate 进行数据访问(ORM)+ Maven 作为构建经理。所有数据访问操作必须由Hibernate提供。

我的目标是将Spring Security,Spring Framework和Hibernate集成在一起。我阅读了许多相应的教程,但令我困惑的是(在教程代码中)我的项目中缺少许多xml配置文件。 Here is the structure of my project

正如您所看到的,只有两个与Spring相关的xml文件和web.xml - 当我向我的项目添加Spring Framework和Spring Security支持时,它们都是Spring自动生成的。

但是在提到的教程中,还有名为" spring-database.xml"," spring-security.xml"的文件。我认为第一个是从数据库配置Spring toget数据,第二个是基本的Spring Security配置。文件。

问题:
1)这两个文件尚未自动生成,甚至是" spring-security.xml"。没问题 - 它是否必须如此?或者我的项目设置有问题?

2)如果我可以按照自己的意愿命名那些xml配置文件 - 那么Spring如何知道所有这些并区分它们?我还没有在官方的Spring refference文档中找到任何有关这些名称的配置文件。

回答问题2,如果有可能,请举一些例子。

3)我的applicationContext文件是空的 - 可以吗?如果它和dispatcher-servlet有区别呢?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

1)我一般只需要一个弹簧配置文件,它通常放在src/main/resources(或类路径的其他地方)和调用applicationContext.xml之后或类似的文件中。如果您使用多个Spring模块(例如Spring Data,Spring Security等),通常会为每个模块配置一个单独的配置文件,但不是必需的,您可以将所有配置放在同一个xml中。

2)命名并不重要,Spring在类路径中查找文件:https://softwareengineering.stackexchange.com/questions/145545/where-to-put-spring-configuration-file

3)通常,applicationContext.xml包含要扫描注释的bean和包的定义,应该放在src/main/resources中。我猜dispatcher-servlet.xml用于URI / servlet映射(spring mvc)

答案 1 :(得分:0)

您的项目结构没有问题。您没有必要拥有项目中的文件名。如果您想拥有自己的xml文件名,则必须在web.xml中提及xmls的名称。见下文

 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/dispatcher-servlet.xml
    /WEB-INF/spring-security.xml
  </param-value>
 </context-param>

来到第三个问题。 applicationContext.xml不应为空。您需要在applicationContext.xml中指定应用程序的bean(services,dao等)。在dispatcher-servlet.xml中,您需要指定与spring mvc相关的bean(处理程序映射,控制器,视图解析器等)。