自动装配字段保持为空,从而导致WicketRuntime异常

时间:2014-08-27 16:00:09

标签: spring hibernate wicket autowired

这些问题有很多,但建议的解决方案似乎对我不起作用。 我正在使用wicket以及冬眠和春天。我正在尝试使用一个文本字段的简单表单但是wicket一直在抱怨来自自动装配字段的空闲NullPointerException

Wicket类

public class WebLoginPage extends WebPage {
    @SpringBean
    IUserService userService;

    private final User newUser = new User();

    RequiredTextField<String> username = new RequiredTextField<String>(
            "username", new PropertyModel<String>(this.newUser, "username"));

    public WebLoginPage() {
        final Form<?> form = new Form<User>("userForm") {

            @Override
            protected void onSubmit() {
                WebLoginPage.this.userService
                        .addUser(WebLoginPage.this.newUser);
            }

        };
        form.add(this.username);
        this.add(form);

    }

UserServiceImpl

package com.ziker.schedule.service;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.ziker.schedule.dao.IUserDao;
import com.ziker.schedule.pojo.User;

@Controller
public class UserServiceImpl implements IUserService, Serializable {

    @Autowired
    IUserDao userDao;

    @Override
    @Transactional
    public void addUser(User user) {
        this.userDao.saveUser(user);

    }

    @Override
    @Transactional
    public void deleteUser(User user) {
        this.userDao.deleteUser(user);

    }
}

在UserDAOImpl

package com.ziker.schedule.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ziker.schedule.pojo.User;

@Repository
public class UserDaoImpl implements IUserDao {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public void saveUser(User user) {
        this.sessionFactory.getCurrentSession().saveOrUpdate(user);

    }

    @Override
    public void deleteUser(User user) {
        this.sessionFactory.getCurrentSession().delete(user);
    }

    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

的applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

     <context:annotation-config />

    <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/schedule"/>
        <property name="username" value="root"/>
        <property name="password" value="rootroot"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.ziker.schedule.pojo.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="userDao" class="com.ziker.schedule.dao.UserDaoImpl">
    <property name="sessionFactory" ref="mySessionFactory" />
    </bean>

    <bean id="userService" class="com.ziker.schedule.service.UserServiceImpl">  

    </bean>



</beans>

Wicket网页

  

包com.ziker.schedule.schedule;

     

import org.apache.wicket.Page;进口   org.apache.wicket.protocol.http.WebApplication;进口   org.apache.wicket.spring.injection.annot.SpringComponentInjector;

     

import com.ziker.schedule.login.WebLoginPage;

     

公共类ScheduleApplication扩展了WebApplication

     

{

     

@Override public class getHomePage(){return   WebLoginPage.class; }

     

}

任何帮助将不胜感激我已尝试配置没有注释没有运气。对我来说,似乎我错过了一些小事,但在一天半后我不得不问

编辑:问题字段在WebLogin页面中为IUserService userService;

栈跟踪

[qtp14429558-16] ERROR org.apache.wicket.DefaultExceptionMapper - Unexpected error occurred
org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at [Form [Component id = userForm]] on component [Form [Component id = userForm]] threw an exception
    at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:268)
    at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:216)
    at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:241)
    at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:234)
    at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:837)
    at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
    at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:265)
    at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:222)
    at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:293)
    at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:261)
    at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:203)
    at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:284)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1476)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:258)
    ... 36 more
Caused by: java.lang.NullPointerException
    at com.ziker.schedule.login.WebLoginPage$1.onSubmit(WebLoginPage.java:30)
    at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1262)
    at org.apache.wicket.markup.html.form.Form.process(Form.java:934)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:766)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:700)
    ... 41 more

1 个答案:

答案 0 :(得分:2)

你需要通过覆盖Wickets应用程序init()方法来注入spring bean来添加SpringComponentInjector。