sc_ray
sc_ray

Reputation: 8043

Mixing *-servlet.xml with applicationContext.xml

I have an existing Spring application that does some server side processing. I am trying to create a webapp for this particular application and chose SpringMVC to serve my purpose.

I created a display controller as follows:

@Controller
@RequestMapping("/items")
public class ItemDisplayController {
    private static final Logger LOGGER = Logger.getLogger(ItemDisplayController.class);
    private static final String ITEMS_REDIRECT = "redirect:/item/items";

    @Autowired
    private ItemDisplay itemDisplay;

    @RequestMapping
    public String listItems(ModelMap model) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("List all items");
        }

        List<ItemDetail> itemDetails = itemDisplay.getAllItems();
        model.addAttribute("itemDetails",itemDetails);
        return "items";
    }
}

I already have an applicationContext file with the following definitions:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <array>
                <value>classpath:item.properties</value>
                <value>file:#{systemEnvironment['ITEM_HOME']}/item.properties</value>
                <value>file:#{systemProperties['ITEM_HOME']}/item.properties</value>
            </array>
        </property>
        <property name="ignoreResourceNotFound" value="true"/>
    </bean>


    <bean id="itemDisplay" class="com.acme.itemDisplayImpl">
           <property name="itemDisplayDAO" ref="jdbcItemDisplayDAO"/>
       </bean>

    <bean id="jdbcItemDisplayDAO" class="com.acme.database.dao.JdbcItemDisplayDAO">
        <property name="dataSource" ref = "dataSource"/>
    </bean>


    <bean id="realDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="#{props['itemds.jdbc.driver']}"/>
        <property name="url"><value><![CDATA[#{props['itemds.jdbc.url']}]]></value></property>
        <property name="username" value="#{props['itemds.username']}"/>
        <property name="password" value="#{props['itemds.password']}"/>
        <property name="testOnBorrow" value="#{props['itemds.test.on.borrow']}"/>
        <property name="testWhileIdle" value="#{props['itemds.test.while.idle']}"/>
        <property name="poolPreparedStatements" value="#{props['itemds.pool.prepared.statements']}"/>
        <property name="validationQuery" value="#{props['itemds.validation.query']}"/>
        <property name="validationQueryTimeout" value="#{props['itemds.validation.query.timeout']}"/>
        <property name="timeBetweenEvictionRunsMillis" value="#{props['itemds.time.between.eviction.runs.millis']}"/>
        <property name="maxActive" value="#{props['itemds.max.active']}"/>
        <property name="maxIdle" value="#{props['itemds.max.idle']}"/>
        <property name="initialSize" value="#{props['itemds.initial.size']}"/>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource" ref="realDataSource"/>
    </bean>

</beans>

In my *servlet.xml, I have defined the ViewResolver as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.acme.item"/>

    <bean id="primaryViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="order" value="1"/>
    </bean>
</beans>

And in the web.xml, I have the context-param defined:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>items</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>items</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

When I run my application, the itemDisplay is not wired up and on the debugger it is showing up with a null value.

Can somebody point out what I may be doing wrong here? In an ideal world, I would assume that annotating the itemDisplay with @Autowired in the controller would help resolve the implementation of the interface.

Upvotes: 2

Views: 1122

Answers (3)

Prasanna Talakanti
Prasanna Talakanti

Reputation: 2394

Only thing I can think of is, ContextLoaderListener does not complain if the applicationContext.xml is not found, Let's try this add classpath*:applicationContext.xml also make sure applicationContext.xml is run time class path of the server

Here is an excellent article that provides a good insight into spring classpath resource.

Upvotes: 1

Gaurav
Gaurav

Reputation: 1567

Not sure if this is a typo in your applicationContext file :

<bean id="itemDisplay" class="com.acme.itemDisplayImpl"/>

Shouldn't the class be com.acme.item.DisplayImpl ?. If the name of class is indeed itemDisplayImpl, then you need to change the component-scan element in *servlet.xml to include the correct package.

Upvotes: 1

maximdim
maximdim

Reputation: 8169

Your files looks up fine at quick glance. As long as there is only one bean with of ItemDisplay type in your context it should be autowired by type, otherwise you should get error in log file.

One small thing I noticed, which is probably unrelated to your particular issue, is that you map your 'items' controller to all files in web.xml. Usually you would want it to map to particular type of files only - say *.htm so it won't get called for resources such as images etc.

Upvotes: 1

Related Questions