2012-10-03 41 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)。你幫我理清了。 :) –

相關問題