Invalid property ‘name’ of bean class

來源:互聯網
上載者:User

在Struts2 +  Hibernate4 +  Spring3.2 整合的時候出現的:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:         Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]:         Error setting property values;         nested exception is org.springframework.beans.NotWritablePropertyException:        Invalid property 'name' of bean class [org.apache.commons.dbcp.BasicDataSource]:        Bean property 'name' 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:1427)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)    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.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)    at com.ssh.test.Test.main(Test.java:25)Caused by: org.springframework.beans.NotWritablePropertyException:        Invalid property 'name' of bean class [org.apache.commons.dbcp.BasicDataSource]:       Bean property 'name' 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:1424)    ... 13 more

我的applicationContext.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:contex="http://www.springframework.org/schema/context"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">        <bean id="testService" class="com.ssh.test.TestService" p:name="這是一個測試屬性值"></bean>        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://127.0.0.1:3306/ssh"/>        <property name="name" value="root"/>        <property name="password" value="root"/>                <property name="initialSize" value="3"/>        <property name="maxActive" value="500"/>        <property name="maxIdle" value="2"/>        <property name="minIdle" value="1"/>            </bean>        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">                <property name="dataSource" ref="dataSource"></property>                <property name="hibernateProperties">                    <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>                <prop key="hibernate.show_sql">true</prop>            </props>                    </property>                <property name="mappingResources">            <list>                <value>com/ssh/domain/Employee.hbm.xml</value>            </list>        </property>        </bean>    </beans>

原來是:

 <property name="name" value="root"/>

寫錯了,改成:

 <property name="username" value="root"/>

問題解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.