1

我是这个框架的新手。我从一些示例/教程开始,但我现在是堆栈。我相信错误在 application-Context.xml 中,在配置 spring 但我不知道在哪里。

我收到此错误:

03-ago-2013 17:29:33 org.apache.catalina.core.StandardContext listenerStart
GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de clase org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ListingEventos' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'serviceEvento' of bean class [com.app.struts.actions.eventos.ListingEventos]: Bean property 'serviceEvento' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1423)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:536)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1462)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:835)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:771)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1445)
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:860)
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:357)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'serviceEvento' of bean class [com.app.struts.actions.eventos.ListingEventos]: Bean property 'serviceEvento' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1042)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1420)
    ... 56 more
03-ago-2013 17:29:33 org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext

application-Context.xml 只有 60 行。有人可以帮助我吗??非常感谢!

STRUTS.XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.devMode" value="true" /> <!-- set to false for prod -->

    <constant name="struts.configuration.xml.reload" value="true" />

    <package name="eventos"  namespace="/eventos" extends="struts-default">
        <action name="*" class="com.app.struts.actions.eventos.{1}">
            <result name="success">/vistas/eventos/{1}.jsp</result>
            <result name="redirect" type="redirect">${redirectUrl}</result>
        </action>
    </package>

    <package name="artistas" namespace="/artistas" extends="struts-default">
        <action name="*" class="com.app.struts.actions.artistas.{1}">
            <result>/vistas/artistas/{1}.jsp</result>
            <result name="redirect" type="redirect">${redirectUrl}</result>
        </action>
    </package>

    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />


</struts>

应用程序上下文.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <!-- The singleton hibernate session factory -->
    <bean id="sessionFactory" scope="singleton"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean> 

    <!-- Spring's hibernate transaction manager, in charge of making hibernate sessions/txns -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 


    <bean id="eventoDao" class="com.app.dao.EventoDAOImpl">
        <constructor-arg ref="sessionFactory"/>
    </bean>

    <bean id="eventoService" class="com.app.service.EventoServiceImpl">
        <constructor-arg   ref="eventoDao"/>
    </bean>


<bean id="ListingEventos" class="com.app.struts.actions.eventos.ListingEventos">
    <property name="serviceEvento"  ref="eventoService" />   
</bean>

    </beans>

ListingEventos.java

package com.app.struts.actions.eventos;


import com.app.struts.actions.base.BaseAction;
import com.app.model.Evento;
import com.app.service.IEventoService;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;



public class ListingEventos extends ActionSupport {

 protected IEventoService serviceEvento;    
@Override
    public String execute() throws Exception
    {
        eventos = serviceEvento.getEvents();
        return "success";
    }


    List<Evento> eventos;
    public List<Evento> getEvents() { 
        return eventos; 
    }

    public void setServices(IEventoService value) {
        serviceEvento=value;
    }
}

EventoServiceImpl.java

package com.app.service;

import com.app.model.Evento;
import com.app.dao.IEventoDAO;
import java.util.List;


public class EventoServiceImpl implements IEventoService{



    private IEventoDAO dao;

    public EventoServiceImpl(IEventoDAO dao) {
        this.dao = dao;
    }

    public Evento getEventById(long id){
        return dao.getEventById(id);
    }

    public boolean deleteEventById(long id) {
        return dao.deleteEventById(id);
    }

    public void createEvent(String name) {
        dao.createEvent(name);
    }

    public List<Evento> getEvents(){
        return dao.getEvents();
    }

    public void removePersonFromEvent(int personId, int eventId){
        dao.removePersonFromEvent(personId, eventId);
    }

    public void addPersonToEvent(int personId, int eventId){
        dao.addPersonToEvent(personId, eventId);
    }
public void setEvents(Evento evento){
        dao.setEvents(evento);
    }
}

EventoDAOImpl.java

package com.app.dao;

import com.app.model.Evento;
import com.app.service.IArtistaService;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;

public class EventoDAOImpl implements IEventoDAO {

    private SessionFactory sessionFactory;

    public EventoDAOImpl(SessionFactory sessionFactory){

        this.sessionFactory = sessionFactory;
    }

    public Session sess() {
        return sessionFactory.getCurrentSession();
    }

    public Evento getEventById(long id){

        Query query = sess().createQuery("from com.app.model.evento e where e.id = :ID ");
        //query.setString("ID",Evento.getId());
        Evento EventoOut    = (Evento)query.uniqueResult();
        return EventoOut;
        //return (Evento) sess().load(Evento.class, id);
    }

    public boolean deleteEventById(long id) 
            throws DataAccessException {
        sess().delete(getEventById(id));
        return true;
    }

    public void createEvent(String name) {
        Evento theEvent = new Evento();
        theEvent.setName(name);
        sess().save(theEvent);
    }

    @SuppressWarnings("unchecked")
    public List<Evento> getEvents() {
        return sess().createQuery("from com.app.model.evento").list();
    }

    private IArtistaService iArtistaService;

    public void removePersonFromEvent(int personId, int eventId){
        getEventById(eventId).getPeople().remove(iArtistaService.getPersonById(personId));
    }

    public void addPersonToEvent(int personId, int eventId) {
        getEventById(eventId).getPeople().add(iArtistaService.getPersonById(personId));
    }
public void setEvents(Evento evento){
    long id=evento.getId();
    Query query = sess().createQuery("from com.app.model.evento e where e.id = :ID ");
    Evento EventoOut    = (Evento)query.uniqueResult();
    EventoOut.setName(evento.getName());
    EventoOut.setPeople(evento.getPeople());
}
}

Evento.java

package com.app.model;

import java.util.Set;
import com.app.model.Artista;


public class Evento {


    Long id;

    public Long getId() 
    {return id;}

    public void setId(Long id) 
    {this.id = id;}

    String name;

    public String getName() 
    {return name;}

    public void setName(String value) 
    {name = value;}


    Set<Artista> people;
    public void setPeople(Set<Artista> people) 
    {this.people = people;}

    public Set<Artista> getPeople() 
    {return people;}

}

Evento.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
    <class name="com.app.model.Evento" table="events">
        <id name="id" column="Id">
            <generator class="increment">
            </generator>
        </id>
        <!--property name="dt" /-->
        <property name="name" />
    </class>
</hibernate-mapping>

结构:http: //img198.imageshack.us/img198/4681/qedu.jpg

4

2 回答 2

1

您在 bean xml 配置中提到了构造函数参数:

   <bean id="ListingEventos" class="com.app.struts.actions.eventos.ListingEventos">
        <constructor-arg name="eventoService"  ref="eventoService" />   
    </bean>

但是您的 ListingEventos 类文件中没有这样的构造函数。您需要添加相应的构造函数。

于 2013-08-03T14:08:26.747 回答
1

好吧,您没有任何构造函数来ListingEventos获取 - 类型的参数IEventoService。实际上,由于您没有定义任何构造函数,因此您只有一个默认的无参数构造函数。

因此,您不能<constructor-arg>在该 bean 中使用注入 bean。

看来您应该使用<property>标签来注入使用属性。

以下 bean 声明:

<bean id="ListingEventos" class="com.app.struts.actions.eventos.ListingEventos">
    <constructor-arg name="eventoService"  ref="eventoService" />   
</bean>

应该:

<bean id="ListingEventos" class="com.app.struts.actions.eventos.ListingEventos">
    <property name="serviceEvento"  ref="eventoService" />   
</bean>
于 2013-08-03T14:08:51.820 回答