4

只是一个一般性问题,当您定义基于 Java 的配置 Web 应用程序时。即有一个类:ApplicationContext 和一个 WebApplicationInitializer 类。

Spring如何知道它必须加载bean,因为不存在xml配置文件..tomcat如何知道没有web.xml的webapp

这是一个新手问题..我很感激。:)

4

2 回答 2

3

请参阅SpringSource 博客中的这篇博文,关于的重要部分有一个示例,基本上你web.xml指向JavaConfigWebApplicationContext而不是 default XmlWebApplicationContextin DispatcherServlet<init-param>

<web-app>
    <!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext
         instead of the default XmlWebApplicationContext -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
    </context-param>
    <!-- Configuration locations must consist of one or more comma- or space-delimited
         fully-qualified @Configuration classes -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>example.RootApplicationConfig</param-value>
    </context-param>
    <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Declare a Spring MVC DispatcherServlet as usual -->
    <servlet>
        <servlet-name>dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext
             instead of the default XmlWebApplicationContext -->
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
        </init-param>
        <!-- Again, config locations must consist of one or more comma- or space-delimited
             and fully-qualified @Configuration classes -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>example.web.WebBeansConfig</param-value>
        </init-param>
    </servlet>
</web-app>
于 2012-10-03T10:23:13.297 回答
1

如果您启动并运行了 Maven,我有一个非常好的方法可以帮助您学习 Spring MVC。

如果是这样:转到我使用的命令行(Cygwin)...

  1. mvn 原型:生成
  2. 它将要求一个“原型编号”。为你...键入 16
  3. 输入组 ID,它只是主包。
  4. 输入工件 ID,这是您的项目名称。
  5. SNAP-SHOT --- 只需按回车键,与版本相同。
  6. 包 - 与您的组 ID 名称相同。例如:com.spring
  7. 输入字母“y”确认,然后按回车键。

在您的工作区目录中执行上述所有操作。这样它就在那里创建。
您可以执行“mvn eclipse:eclipse”在 Eclipse 中加载它,或者您可以只导入它。我更喜欢老式的导入现有项目。

一切都将为您“已经”设置好所有配置(基于 Java),这对您有好处。它将在您的 pom.xml 中包含您需要的所有 Maven 依赖项。如果需要,您可以添加或从中获取。

这里的重点是你已经有一个正在运行的项目,你可以从那里开始使用它。我首先像这样创建我的所有项目并删除我不需要的并添加我所做的然后从那里开始。

祝你好运!!!

Anywho...将此添加到您的 web.xml。这将帮助您回答问题。研究如下:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
于 2014-11-02T21:36:48.530 回答