2012-10-03 40 views
4

只是一个一般性的问题,当你定义一个基于Java的配置Web应用程序。即有一个类:ApplicationContext和一个WebApplicationInitializer类。如何基于Spring 3.1的Java配置工作

如何春季知道它加载豆,因为没有XML配置文件存在..请问tomcat的知道web应用程序没有任何一个web.xml

它是一个新手的问​​题..我对此表示赞赏。 :)

+0

Spring基于Java的配置与'web.xml'无关......所以有一个。 – Xaerxess

+0

好吧..我只是想比较..如果我正在创建一个web.xml我会创建一个调度器,让servlet知道应用程序上下文xml位置的位置。但在基于Java的配置..它会如何工作? – user1555190

回答

3

this blog post from SpringSource blog,约web.xml重要组成部分,有一个例子,基本上你指向JavaConfigWebApplicationContext,而不是默认XmlWebApplicationContextDispatcherServlet<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> 
+0

优秀的,你让我半路上有...这个问题回答另一半.. http://stackoverflow.com/questions/9939896/spring-3-with-java-based-configuration-and-resources-access-issue您可以创建基于java的配置来完成web.xml在http://www.java-allandsundry中的大部分功能。com/2012/05/spring-webapplicationinitializer-and.html但我只是不知道它是如何挂在一起... – user1555190

+1

这是完美的.. http://blog.springsource.org/2011/06/10/spring-3 -1-m2-configuration-enhancements/..回答所有......当你的类路径中存在spring-web模块JAR时,将检测到你的WebApplicationInitializer实现。 – user1555190

+0

+1了解更多;) – Xaerxess

1

我有一个非常好的方法来帮助您学习春天MVC如果你有Maven启动和运行。

如果是:去你的命令行(Cygwin的),我用...

  1. MVN原型:产生
  2. 它会要求一个 '原型号'。对于你...类型16
  3. 输入只是主包的组ID。
  4. 输入您的项目名称的工件编号。
  5. SNAP-SHOT ---只需按回车,并与版本相同。
  6. 包 - 与您的组ID名称相同。 EX:com.spring
  7. 通过输入字母'y'并按回车确认。

做到以上的你在你的工作空间目录之后。这样它就创建在那里。
你可以使用“mvn eclipse:eclipse”在Eclipse中加载它,或者你可以导入它。我更喜欢旧式导入现有项目。

一切都将在所有配置(基于Java的),这是对你有好处的条件“已经”为你设置。它将拥有您需要的所有Maven依赖关系,并且已经在您的pom.xml中。如果你愿意,你可以添加或取消它。

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

祝你好运!

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> 
+0

非常有帮助!我试图用'''org.springframework.config.java.JavaConfigWebApplicationContext''从现在不存在的[spring-javaconfig依赖项](https://mvnrepository.com/artifact/org.springframework.javaconfig/spring -javaconfig),因为找到[这个很好的参考](https://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/developing-web-applications.html)。你帮我理清了。 :) –

相关问题