简体   繁体   English

放置在应用程序上下文中的Spring bean无效

[英]Spring bean placed inside application context is not working

I have worked with Spring annotations a bit, but never with spring xml. 我曾经使用Spring注释,但从未使用过spring xml。 I have to create an application where all the beans are kept in applciationContext.xml file and not in xxx-dispather.xml. 我必须创建一个应用程序,其中所有bean都保存在applciationContext.xml文件中,而不是保存在xxx-dispather.xml中。 But when i place the beans inside xxx-dispatcher.xml i am able to hit the controller class and everything works fine, But when i place the same beans inside applciationContext.xml it does not work at all. 但是当我将bean放在xxx-dispatcher.xml中时,我能够访问控制器类并且一切正常,但是当我将相同的bean放在applciationContext.xml中时它根本不起作用。 All i get is 404 exception. 我得到的只是404例外。

Below is my web.xml file 下面是我的web.xml文件

<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring Web MVC Application</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-applicationContext.xml</param-value>
 </context-param>

 <listener>
 <listener-class>
  org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
 </web-app>

Below is my mvc-dispatcher-servlet.xml 下面是我的mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="caseSensitive" value="true" />
    <property name="pathPrefix" value="/customer" />
</bean>




<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

</beans>

Below is my web-applicationContext.xml 下面是我的web-applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean class="com.mkyong.common.controller.WelcomeController" />
<bean class="com.mkyong.common.controller.HelloGuestController" />

</beans>

Can anyone please correct me where i have gone wrong. 任何人都可以在我出错的地方纠正我。

Add the following: 添加以下内容:

<mvc:annotation-driven/>
<context:component-scan base-package=""/>//the package where your controller are located

This will let you just use the controller annotation to locate you controllers. 这将让您只需使用控制器注释来定位控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM