Spring、Hibernate 与 google 应用引擎

2024-04-28

项目名称:CarpoolDB,我已在另一个应用程序名称 Carpool 中添加了该项目的 jar。 运行拼车应用程序时,我遇到以下异常。

项目:拼车。 在这里,我遇到异常,因为“carpoolService”在作为“Google Web应用程序”运行时没有自动装配,但在Tomcat下运行,并且bean正确注入。

@Controller
public class PlacesSearchController {

    @Autowired
    CarpoolService carpoolService=null;

    public CarpoolService getCarpoolService() {
        return carpoolService;
    }
    public void setCarpoolService(CarpoolService carpoolService) {
        this.carpoolService = carpoolService;
    }

} 

拼车应用程序上下文1.xml

我还有 applicationContext-security.xml,用于 spring 安全性。

项目:CarpoolDB 所有类都在包 com.company.carpooldb.db 中

    public interface CarpoolService {
        public boolean validateUser(User user);
    }

    @Service("carpoolService")
    public class CarpoolServiceImpl implements CarpoolService{

        @Autowired
        private CarpoolDao carpoolDao=null;

        public CarpoolDao getCarpoolDao() {
            return carpoolDao;
        }

        public void setCarpoolDao(CarpoolDao carpoolDao) {
            this.carpoolDao = carpoolDao;
        }

        @Override
        public boolean validateUser(User user) {
              return carpoolDao.validateUser(user);;
        }

    }


public interface CarpoolDao {
  public boolean validateUser(User user);
}

public class CarpoolDaoImpl extends HibernateDaoSupport implements CarpoolDao{
    @Override
    public boolean validateUser(User user) {
        // TODO Auto-generated method stub
        return false;
    }
}

拼车应用程序上下文.xml

<context:annotation-config />
        <context:component-scan base-package="com.onmobile" />
    <context:property-placeholder location="classpath:server.properties" />


    <bean id="sessionFactory" 

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingResources">
     <list>
            <value>com\company\carpooldb\hbm\User.hbm.xml</value>
     </list>   
     </property>
     <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect"><!--Which dialect is used of hibernate according to type of underlying database-->
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="dataSource"><ref bean="dataSource"/></property> 

</bean>


<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <beans:property name="driverClassName"><beans:value>${cm.db.driverClassName}</beans:value></beans:property>
    <beans:property name="url"><beans:value>${cm.db.url}</beans:value></beans:property>
    <beans:property name="username"><beans:value>${cm.db.username}</beans:value></beans:property>
    <beans:property name="password"><beans:value>${cm.db.password}</beans:value></beans:property>
    <beans:property name="testOnBorrow"><beans:value>true</beans:value></beans:property>
    <beans:property name="testOnReturn"><beans:value>true</beans:value></beans:property>
    <beans:property name="validationQuery"><beans:value>select 1</beans:value></beans:property>
</beans:bean>

    <!--Hibernate's transaction manager used for handling database transactions/manipulating data happening-->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <bean id="carpoolDaoImpl" class="com.company.carpooldb.db.CarpoolDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

运行应用程序时,我遇到异常,

log4j:WARN Error during default initialization
java.lang.NoClassDefFoundError: java.io.FileOutputStream is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:290)
.........

    at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:162)
    at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:48)
    at com.google.appengine.tools.development.DevAppServerMain.<init>(DevAppServerMain.java:113)
    at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:89)
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
11 Nov, 2012 5:14:18 PM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Failed startup of context com.google.apphosting.utils.jetty.DevAppEngineWebAppContext@5e55ab{/,D:\EclipseEuropa\CarpoolingGAE\war}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'placesSearchController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.onmobile.carpooldb.db.CarpoolService com.onmobile.carpool.controller.PlacesSearchController.carpoolService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onmobile.carpooldb.db.CarpoolDao com.onmobile.carpooldb.db.CarpoolServiceImpl.carpoolDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolDaoImpl' defined in class path resource [carpool-application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
.........   
at com.google.appengine.tools.development.DevAppServerMain.<init>(DevAppServerMain.java:113)
    at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:89)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.onmobile.carpooldb.db.CarpoolService com.onmobile.carpool.controller.PlacesSearchController.carpoolService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onmobile.carpooldb.db.CarpoolDao com.onmobile.carpooldb.db.CarpoolServiceImpl.carpoolDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolDaoImpl' defined in class path resource [carpool-application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    ... 31 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onmobile.carpooldb.db.CarpoolDao com.onmobile.carpooldb.db.CarpoolServiceImpl.carpoolDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolDaoImpl' defined in class path resource [carpool-application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)
....    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:780)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 33 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onmobile.carpooldb.db.CarpoolDao com.onmobile.carpooldb.db.CarpoolServiceImpl.carpoolDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolDaoImpl' defined in class path resource [carpool-application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    ... 44 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolDaoImpl' defined in class path resource [carpool-application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
....    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 46 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
....    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 59 more
Caused by: java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
....    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1457)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    ... 66 more
11 Nov, 2012 5:14:18 PM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'placesSearchController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.onmobile.carpooldb.db.CarpoolService com.onmobile.carpool.controller.PlacesSearchController.carpoolService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onmobile.carpooldb.db.CarpoolDao com.onmobile.carpooldb.db.CarpoolServiceImpl.carpoolDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carpoolDaoImpl' defined in class path resource [carpool-application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [carpool-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.:
java.lang.NoClassDefFoundError: java.net.InetAddress is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
    at org.hibernate.id.AbstractUUIDGenerator.<clinit>(AbstractUUIDGenerator.java:22)
    ......at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)
.....
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
.....   at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:89)
The server is running at http://localhost:8888/

为什么我越来越像 1. java.lang.NoClassDefFoundError: java.io.FileOutputStream 是受限类。

我没有在档案中写任何内容

  1. 据我所知,GAE 不支持少数 API,但我的应用程序已经适用于 MySQL,并且我想同时支持两者。我的意思是,根据我的属性文件中设置的属性,我有时想使用 MySQL,有时想使用 GAE 数据存储。

我用的是弹簧3。

在这种情况下我应该做什么?


正如鲍里斯指出的,问题似乎出在您的会话工厂上。 GAE不支持jdbc,看起来你的sessionFactory是为mysql配置的:

<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect"><!--Which dialect is used of hibernate according to type of underlying database-->
            org.hibernate.dialect.MySQLDialect
        </prop>
        <prop key="hibernate.show_sql">false</prop>
    </props>
</property>

如果您正在使用依赖于 Hibernate 的现有代码库,您可以尝试将 Hibernate 配置为由 Cloud SQL 支持运行:https://developers.google.com/appengine/articles/using_hibernate https://developers.google.com/appengine/articles/using_hibernate

至于 FileOutputStream 问题,我相信 log4j 的默认附加程序之一是 FileAppender,因此简单地不配置 log4j 无法解决它 - 您需要显式配置它。看看此页面是否有帮助:http://blog.xam.de/2010/03/logging-in-google-appengine-for-java.html http://blog.xam.de/2010/03/logging-in-google-appengine-for-java.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring、Hibernate 与 google 应用引擎 的相关文章

随机推荐