使用Flex客户端时Spring + Hibernate + Blazeds null错误

时间:2011-09-20 20:34:45

标签: flex hibernate spring blazeds amf

我的问题很简单,当我从独立应用程序中运行 EventService 中的方法时(通过在main()中使用ClassPathXmlApplicationContext),一切正常。

@Service
@Transactional(readOnly = true)
public class EventService {
    Logger log = Logger.getLogger(EventService.class);
    @Autowired private EventDao eventDao;

    public List<Event> getAll() {
        return eventDao.getAll();
    }

    public List<Event> getAllContainsResource(int resourceId) {
        return eventDao.getAllWithResource(resourceId);
    }

    public String getName() {
        return "simpleName";
    }
}

当我在tomcat(STS上的内置服务器)上运行时,容器正确启动但是当我向 getAll()方法或 getAllContainsResource(int)发送请求(通过AMF的Flex应用程序)时) * NullPointerException *出现。它显示 eventDao为空。我不知道是什么原因所以请帮忙。这是我的applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans>

    <!-- database connection -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/rapla" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                hibernate.c3p0.min_size=5
                hibernate.c3p0.max_size=20
                hibernate.c3p0.timeout=1800
                hibernate.c3p0.max_statements=50
                hibernate.show_sql=true
                hibernate.format_sql=true
                hibernate.use_sql_comments=true
        </value>
        </property>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
    </bean>


    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

    <context:annotation-config />
    <context:component-scan base-package="org.pwsz.dao.hibernate" />
    <context:component-scan base-package="org.pwsz.services" />

    <flex:message-broker />

</beans>

独立应用程序的XML是相同的,没有

<flex:message-broker />

EventDaoImpl:

@Repository
public class EventDaoImpl implements EventDao {

    Logger log = Logger.getLogger(EventDaoImpl.class);
    @Autowired private SessionFactory sessionFactory;

    public Event getById(int id) {
        return (Event) sessionFactory.getCurrentSession().get(Event.class, id);
    }

    public List<Event> getAll() {
        return sessionFactory.getCurrentSession().createCriteria(Event.class).list();
    }

    public List<Event> getAllWithResource(Integer id) {
        List<Event> resulu = sessionFactory.getCurrentSession()
            .createCriteria(Event.class)
            .createCriteria("appointments.teachers")
            .add(Restrictions.eq("resourceId", id))
            .list();

        return resulu;
    }
}

Spring-flex连接工作正常,我可以使用Flex Remote Object请求 getName ,但发送另外两个请求会产生null异常。

此致


我检查了WebApp中的问题(使用spring mvc),一切正常。我发现问题会产生Hibernate,当请求来自Flex(使用AMF协议)时,它会为DAO返回null sessionFactory(它们是@autowired)。

flex.messaging.MessageException: java.lang.NullPointerException : null
    at flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:444)
    at flex.messaging.services.RemotingService.serviceMessage(RemotingService.java:183)
    at flex.messaging.MessageBroker.routeMessageToService(MessageBroker.java:1400)
    at flex.messaging.endpoints.AbstractEndpoint.serviceMessage(AbstractEndpoint.java:1005)
    at flex.messaging.endpoints.AbstractEndpoint$$FastClassByCGLIB$$1a3ef066.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.flex.core.MessageInterceptionAdvice.invoke(MessageInterceptionAdvice.java:66)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:124)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$FixedChainStaticTargetInterceptor.intercept(Cglib2AopProxy.java:572)
    at flex.messaging.endpoints.AMFEndpoint$$EnhancerByCGLIB$$247a330d.serviceMessage(<generated>)
    at flex.messaging.endpoints.amf.MessageBrokerFilter.invoke(MessageBrokerFilter.java:103)
    at flex.messaging.endpoints.amf.LegacyFilter.invoke(LegacyFilter.java:158)
    at flex.messaging.endpoints.amf.SessionFilter.invoke(SessionFilter.java:44)
    at flex.messaging.endpoints.amf.BatchProcessFilter.invoke(BatchProcessFilter.java:67)
    at flex.messaging.endpoints.amf.SerializationFilter.invoke(SerializationFilter.java:166)
    at flex.messaging.endpoints.BaseHTTPEndpoint.service(BaseHTTPEndpoint.java:291)
    at flex.messaging.endpoints.AMFEndpoint$$EnhancerByCGLIB$$247a330d.service(<generated>)
    at org.springframework.flex.servlet.MessageBrokerHandlerAdapter.handle(MessageBrokerHandlerAdapter.java:109)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.traceNextValve(HttpRequestOperationCollectionValve.java:92)
    at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:409)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at org.pwsz.services.EventService.getAll(EventService.java:19)
    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 flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:418)
    ... 47 more

所以问题仍未解决,有人可以帮忙吗?提前谢谢


Finnaly我解决了问题,我更换了blazeds services-config.xml:

<services-config>
    <services>
        <service id="remoting-service" class="flex.messaging.services.RemotingService">
            <default-channels>
                <channel ref="my-amf" />
            </default-channels>

            <adapters>
                <adapter-definition id="java-object"
                    class="flex.messaging.services.remoting.adapters.JavaAdapter"
                    default="true" />
            </adapters>

            <destination id="myService" channels="my-amf">
                <properties>
                    <source>org.abc.services.EventService</source>
                </properties>
            </destination>
        </service>
    </services>

    <channels>
        <channel-definition id="my-amf"
            class="mx.messaging.channels.AMFChannel">
            <endpoint
                url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
                class="flex.messaging.endpoints.AMFEndpoint" />
        </channel-definition>
    </channels>

</services-config>

通过org.abc.services.EventService

中的注释
@RemotingDestination(value="myService",channels={"my-amf"})

我在appContext.xml中也尝试了定义,它也有效:

<flex:remoting-destination destination-id="myService"  ref="eventService"/>

但我仍然不明白为什么services-config中的配置不起作用,主要问题是每个AMF请求Spring都在生成新的eventService对象(没有自动装配dao),请注意那些不需要的函数Dao工作得很好。


我的web.xml

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>rws</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rws</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>
        index.jsp
    </welcome-file>
</welcome-file-list>

1 个答案:

答案 0 :(得分:0)

如果你不使用注释@RemotingDestination,或者你没有在appContext.xml(或Spring使用的另一个xml)中声明组件,那么Spring框架将无法管理你的bean(也就是inhect属性等)这就是DAO对象为空的原因。对于在“经典”BlazeDS配置文件中声明的所有组件都是如此。