2017-06-09 20 views
0

にnullポインタが、私は6.5をZKするZK 5.0から自分のアプリケーションをアップグレードしたとのSpring Beanを再実行適用後のnullの場合、以下のコードがあり、助けてくださいZK +春豆、@Resource

<?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> 

春-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> 

そして@Componentは CategoryServiceImpl.java以下のように定義されています

0123私はcategoryService.getCategoriesから投げるにNullPointer categoryServiceとを使用する場合について
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); 
    } 
} 

がある() 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)); 
     } 
    } 

と私は、私が使用している場合、それが動作することがわかっ CategoryService categoryService =(CategoryService)SpringUtil.getBean ( "categoryService"); @Resource CategoryService categoryServiceの代わりに ;

私は古いコードを残したいと思います。誰かが古いコードで何がうまくいかなかったか教えてください。それはzk 5.0で動作していましたか? zkがアップグレードされた後、春のbeanは無視されるようです。

+1

stacktraceを貼り付けてください – emotionlessbananas

+0

あなたのzk-springバージョンは何ですか? –

+0

私はzk-springを使いませんでした。私はspring.jarバージョン2.5.6をインポートしました – Ryan

答えて

0

Did you read the following manual

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) 

、その後:

@WireVariable 
CategoryService categoryService; 

まあ、汚いトリックも(これはベースVMですが、ためにも動作するはずがありあなたがして、クラスに注釈を付ける必要がある

コントローラ):

public AbstractVM() { 
    this.autowire(this); 
} 

protected final void autowire(Object object) { 
    this.getApplicationContext().getAutowireCapableBeanFactory() 
      .autowireBean(object); 
    this.getApplicationContext().getAutowireCapableBeanFactory() 
      .initializeBean(object, null); 
} 

さらに、@Injectを使用することもできます。

サイドノート:これは、クイックフィックスとしてのみ使用してください、いくつかの空き時間に動作していないautowiringの背後にある本当の問題を把握してください。
これは普通の@WireVariableが春の豆を繋ぐ唯一の最良の方法だからです。解決策を見つけ出すために


@Serviceまたは@Repository@Componentを変更すると、あなたはまだnullポインタを取得するかどうか、それに与えられた名前をドロップするようにしてください。
私は文書に明らかに@Componentについて語っていますが、バグも可能です。

+0

SearchControllerを以下のように更新しました。まだnullpointerの例外です。 @VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver。クラス) public class SearchController extends AbstractController @WireVariable( "categoryService")カテゴリサービスcategoryService; カテゴリサービスに更新した場合のみ動作します。categoryService =(CategoryService)SpringUtil.getBean( "categoryService"); 誰でも手伝いできますか? – Ryan

+0

リンクに記述されているように、web.xmlの2番目のリスナーを逃してしまいます。 – chillworld

+0

申し訳ありませんが、忘れて、私は両方のリスナーをweb.xmlに追加しました。しかし、まだnullpointer例外です。 – Ryan