0

我正在使用 OpenEJB 4.0.0、Spring 3.1.0.RELEASE 和 JUnit 4.8.1。我正在尝试使用 OpenEJB 来设置数据源,我确实喜欢这样……</p>

    final Properties props = loadMyProjectProps();
    final String dsJndiName = props.getProperty("DATASOURCE_JNDI_PREFIX") + "MySqlDS";
    System.out.println("ds jndi name:" + dsJndiName);
    p.put(dsJndiName, "new://Resource?type=DataSource");
    p.put(dsJndiName + ".JdbcDriver", "com.mysql.jdbc.Driver");
    final String url = "jdbc:mysql://" + props.getProperty("DB_SERVER") + ":" + props.getProperty("DB_PORT") + "/" + props.getProperty("DB_NAME");
    p.put(dsJndiName + ".JdbcUrl", url);
    p.put(dsJndiName + ".Username", props.getProperty("DB_USER"));
    p.put(dsJndiName + ".Password", props.getProperty("DB_PASSWORD"));
    final InitialContext initialContext = new InitialContext(p);

但是在初始化我的 Spring 文本应用程序时,它会像这样设置会话工厂和数据源……</p>

    <bean id="myprojSessionFactory" lazy-init="true" scope="singleton"
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="mappingResources">
            <ref bean="_hibernate_config_list"/>
            </property>
            <property name="hibernateProperties">
                    <props>
                            <prop key="hibernate.connection.release_mode">auto</prop>
                            <prop key="hibernate.dialect">${HIBERNATE_DIALECT}</prop>
                            <prop key="hibernate.transaction.factory.class">${HIBERNATE_TRANSACTION_FACTORY}</prop>
                            <prop key="hibernate.current_session_context_class">jta</prop>
                            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
                            <prop key="hibernate.show.sql">true</prop>
                            <prop key="hibernate.transaction.flush_before_completion">true</prop>
                            <prop key="hibernate.transaction.auto_close_session">true</prop>
                    </props>
            </property>
            <property name="dataSource">
                    <ref bean="myproj.ds.${DS_PREFIX}${DB_DATASOURCE}"/>
            </property>
    </bean>

    …

    <bean id="myproj.ds.jndi-MySqlDS" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
            <property name="jndiName"><value>${DATASOURCE_JNDI_PREFIX}MySqlDS</value></property>
    </bean>

我得到了以下异常。知道如何解决这个烦人的“无法将类型 [org.apache.openejb.core.ivm.naming.IvmContext] 的值转换为属性'dataSource'所需的类型[javax.sql.DataSource]”异常吗?

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myprojSessionFactory' defined in class path resource [myproj/spring/config/db/hibernate/Hibernate-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.apache.openejb.core.ivm.naming.IvmContext' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 113 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.apache.openejb.core.ivm.naming.IvmContext' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 119 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.apache.openejb.core.ivm.naming.IvmContext] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
    ... 125 more
4

1 回答 1

2

datasource 属性正在寻找一个数据源,并且您已将一个字符串传递给它(因为您使用了 value=)。您需要将引用传递给您的 bean:

于 2016-03-04T10:41:42.433 回答