GWT和Spring Annotations不与外部项目一起使用

时间:2012-08-27 14:31:06

标签: java spring spring-annotations gwt-platform gwtp

我正在使用GWT和Spring服务器。我有一个项目,我用于许多项目,其中有Spring注释(@ Repository,@ Service,@ Component等),可以在其他webapps和项目中使用但是当我将Spring与GWT集成时,依赖注入失败了bean没有匹配的类型。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.xxx.yy.api.phone.call.PhoneCallDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(mappedName=, shareable=true, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:222)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)
at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)
at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)
at com.google.gwt.dev.DevMode.main(DevMode.java:311)

DAO和服务被正确注释,因为正如我上面所说的那样,这个项目正在另一个webapp中使用。这是我的应用程序上下文:

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


<!-- This line sets up the fact that we can use annotations throughout -->
<context:annotation-config />

<!-- This line tells the Spring boot-strapper where to scan for any objects 
    that are marked with annotations such as @Repository or @Service or @Component -->
<context:component-scan
    base-package="com.xxx.yyy, com.xxx.yyy.api, com.xxx.zzz.api, com.xxx.www, com.gwtplatform.dispatch.server.spring" />


<!-- NOTE: Properties are LAST ONE WINS. Meaning that if there are duplicate 
    properties the one that is loaded LAST will be used. Because of this we should 
    make the JNDI name *LAST* This particular class (bean) can be found in the 
    maher-spring-extension project and it was coded so we can use JNDI to find 
    an environment specific configuration file. We use these for things such 
    as the database, log file directories or anything that is specific to that 
    particular environment. This enables us to be able to build ONE war file 
    and deploy it to any server and so long as that server environment is configured 
    with the appropriate configuration file. (example: we configure the STAGE 
    system to point to the STAGE database and put that properties file somewhere 
    on that machine and then configure it in the Tomcat JNDI system. Then this 
    application will find that file and load the properties overwriting any of 
    the previously set properties (LAST ONE WINS)) -->
<bean
    class="org.springframework.beans.factory.config.JndiAwarePropertyPlaceholderConfigurer"
    init-method="initialize">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>classpath:local.properties</value>
            <!-- these properties are environment specific and are bound in JNDI. 
                If there is no entry then it will gracefully ignore them since we set ignoreUnresolvablePlaceholders 
                to true -->
            <value>file:${IVRProperties}</value>
        </list>
    </property>
</bean>


<!-- Here we are configuring our server module for the GWTP -->
<bean id="serverModule" class="com.xxx.yyy.gwt.server.spring.ServerModule" />


<!-- Here we configure the logger that will be used by the GWTP -->
<bean id="logger"
    class="com.gwtplatform.dispatch.server.spring.LoggerFactoryBean">
    <constructor-arg>
        <bean class="java.util.logging.Logger" factory-method="getAnonymousLogger">
            <property name="level">
                <value>INFO</value>
            </property>
        </bean>
    </constructor-arg>
</bean>


<!-- Here is our data source. This is the only place you will see anything 
    to do with a database connection -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver.class}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="${jdbc.maxActive}" />
</bean>

<!-- Used to stamp every entity that has a createdAt or updatedAt field 
    on insert and update 
<bean id="entityInterceptor" class="com.maher.ivr.server.interceptor.EntityInterceptor"/> -->

<!-- Here we configure the Session Factory. -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="schemaUpdate" value="${hibernate.schema.update}" />
    <!-- <property name="entityInterceptor" ref="entityInterceptor"/> -->
    <property name="packagesToScan" value="com.xxx.yyy, com.xxx.zzz.api" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
            <prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use.optimizer}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.generateDdl">${hibernate.generateDdl}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>
        </props>
    </property>
</bean>


<!-- Here we configure the Transaction manager. -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- Here we enable the transaction manager to be picked up by Aspects. 
    Because of this we can simply add a @Transactional annotation to our service 
    classes and all aspects of the transactions are handled for us, such as Rollback, 
    Commit etc... -->
<bean
    class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

<bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributeSource">
        <bean
            class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
    </property>
</bean>

<bean
    class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor" ref="transactionInterceptor" />
</bean>

<bean id="velocityEngine"
    class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
        </value>
    </property>
</bean>

我不确定问题是这些类是否是GWT项目中包含的单独项目的一部分,但我不知道这将是一个什么问题。我可以在applicationContext.xml中手动定义所有bean,但是这很繁琐,并且有很多要定义的因为我需要为每个注入的类定义它们很多。

1 个答案:

答案 0 :(得分:0)

你是什么意思  这些类是GWT项目中包含的单独项目的一部分

看起来包含的类对弹簧容器不可见。