我已经将我的应用程序从zk 5.0升级到zk 6.5并且在重新运行应用程序后spring bean为null,请帮助,下面是代码:
的web.xml
Cannot implicitly convert type 'object' to 'TestProjectForProject5.Student'.
弹簧-config.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>
@Component定义如下 CategoryServiceImpl.java:
<?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>
下面是我使用categoryService和nullpointer从categoryService.getCategories()抛出的地方 SearchController.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);
}
}
我发现如果我使用它会有效 CategoryService categoryService =(CategoryService)SpringUtil.getBean(“categoryService”); 而不是@Resource CategoryService categoryService;
但是我想保留我的旧代码,有人可以告诉我旧代码出了什么问题,它是否在zk 5.0中运行?看来zk升级后会忽略spring bean。
答案 0 :(得分: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
。
旁注:请仅将此作为快速解决方法使用,并尝试弄清楚自动装配背后的实际问题是什么,而不是在一些业余时间工作。
我这样说是因为正常的@WireVariable
是连接春豆的唯一最佳方式。
寻找解决方案:
尝试将@Component
更改为@Service
或@Repository
,看看是否仍然获得了nullpointer,或者删除了给它的名称。
我知道文档清楚地谈到了@Component
,但错误也是可能的。