Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Beans on spring-servlet.xml can't access beans in my applicationContext.xml

I have a question here, how beans defined in "applicationContext.xml" could be available to controllers defined in let's say "spring-servlet.xml", so i can skip this kind of errors i have.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/home' defined in ServletContext resource [/WEB-INF/mmapp-servlet.xml]: Cannot resolve reference to bean 'equipementService' while setting bean property 'equipementService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'equipementService' is defined

applicationContext.xml

<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC 
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean name="equipementService"
        class="mmapp.service.SimpleEquipementService" />
    <bean name="equipement1"
        class="mmapp.domain.Equipement" />

</beans>

mmapp-servlet.xml

<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean name="/home" class="mmapp.web.HelloController">
        <property name="equipementService" ref="equipementService" />
    </bean>
</beans>
like image 262
elaich Avatar asked Feb 21 '23 14:02

elaich


2 Answers

A Spring based web-application normally has multiple runtime Spring application contexts -

  1. A root application context(which is loaded up during the start-up of the servlet container), here is where you normally put your beans for your services - the root application context is loaded up by using a ContextLoaderListener, typically using these entries in your web.xml file:

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

  1. One or more web application contexts, which is considered a child of the root application context, holding the beans related to the UI - controllers etc. This is configured by using a Dispatcher Servlet, a special servlet provided by Spring that knows to delegate the calls appropriately to the Spring Container. The beans in the root application context are visible to the beans defined here, BUT not the other way around - this way providing a level of separation between the UI layer and the services layer . This is a typical configuration entry in web.xml file:

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

If you have defined the beans this way, the equipementService should be visible to your controller.

like image 155
Biju Kunjummen Avatar answered Apr 29 '23 04:04

Biju Kunjummen


I'm not an expert and I'm not sure if this might be a problem, but I have a suggestion. Could you post your web application descriptor (web.xml)? Does it contain the context-param?

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>
like image 33
BartoszMiller Avatar answered Apr 29 '23 05:04

BartoszMiller