只是一个一般性问题,当您定义基于 Java 的配置 Web 应用程序时。即有一个类:ApplicationContext 和一个 WebApplicationInitializer 类。
Spring如何知道它必须加载bean,因为不存在xml配置文件..tomcat如何知道没有web.xml的webapp
这是一个新手问题..我很感激。:)
只是一个一般性问题,当您定义基于 Java 的配置 Web 应用程序时。即有一个类:ApplicationContext 和一个 WebApplicationInitializer 类。
Spring如何知道它必须加载bean,因为不存在xml配置文件..tomcat如何知道没有web.xml的webapp
这是一个新手问题..我很感激。:)
请参阅SpringSource 博客中的这篇博文,关于的重要部分有一个示例,基本上你web.xml
指向JavaConfigWebApplicationContext
而不是 default XmlWebApplicationContext
in 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>
如果您启动并运行了 Maven,我有一个非常好的方法可以帮助您学习 Spring MVC。
如果是这样:转到我使用的命令行(Cygwin)...
在您的工作区目录中执行上述所有操作。这样它就在那里创建。
您可以执行“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>