0

zk + spring bean, null pointer for @resource

asked 2017-06-09 03:47:46 +0800

ryantoakl gravatar image ryantoakl
1 1

updated 2017-06-09 03:49:32 +0800

I have upgraded my application from zk 5.0 to zk 6.5 and spring bean is null after re-run application, please help, below is the code:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http:// java.sun.com/xml/ns/javaee" 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">

<description>Application</description>
<display-name>Application</display-name>
<!-- Spring -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>

<!-- Spring configuration -->
<!-- Initialize spring context -->
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<!-- ZK -->
<listener>
    <description>ZK listener for cleanup when a session is destroyed</description>
    <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>

spring-config.xml

<?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"
   xsi:schemaLocation="http:// www.springframework.org/schema/beans 
   http:// www.springframework.org/schema/beans/spring-beans.xsd 
   http:// www.springframework.org/schema/context 
   http:// www.springframework.org/schema/context/spring-context.xsd 
   http:// www.zkoss.org/2008/zkspring/core 
   http:// www.zkoss.org/2008/zkspring/core/zkspring-core.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.dc.myapplication"/>
<context:property-placeholder location="classpath*:settings.properties"/>
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/myDB"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="jndiDataSource"/>
</bean>

<bean id="settings" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="index.path">${index.path}</prop>
            <prop key="max.hits">${max.hits}</prop>
            <prop key="search.resultsPerPage">${search.resultsPerPage}</prop>
        </props>
    </property>
</bean>

</beans>

And @Component is defined below CategoryServiceImpl.java:

package com.dc.myapplication.service.impl;

import javax.annotation.Resource;

import com.dc.myapplication.domain.RfiCategory;
import com.dc.myapplication.service.CategoryService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Resource JdbcTemplate dataSource;
    @Resource CategoryMapper categoryMapper;

    @Override
    public Iterable<? extends RfiCategory> getCategories() {
        return dataSource.query("SELECT * FROM category", categoryMapper);
    }
}

Below is where i use categoryService and nullpointer throw from categoryService.getCategories() SearchController.java

package com.dc.myapplication.controller;

import javax.annotation.Resource;

public class SearchController extends AbstractController implements PaginationListener, RfiCategoryState {

    private static final String STATE = "state";

    @Resource CategoryService categoryService;

    @Override
    protected void initialise() {

        populateCategories();
    }

    private void populateCategories() {
        for (RfiCategory category : categoryService.getCategories()) {
            categories.appendChild(rfiCategoryRenderer.renderRadioSelector(category, this));
        }
    }

And i found that it works if i use CategoryService categoryService = (CategoryService) SpringUtil.getBean("categoryService"); instead of @Resource CategoryService categoryService;

But i would like to keep my old code, Can someone tell me what went wrong in the old code, it was working in zk 5.0? It seems that the spring bean is ignored after zk is upgraded.

delete flag offensive retag edit
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2017-06-09 03:47:46 +0800

Seen: 6 times

Last updated: Jun 09 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More