Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

Spring MVC Configuring Data Source Bean (Spring 3.0,Hibernate,Apache Tomcat, Netbeans)

I’m trying to run/setup a simple Spring MVC 3 + Hibernate + Apache Tomcat 7.0, using NetBeans 7.1 as a development environment.

Netbeans comes with prepackaged Spring and Hibernate files/jars/dependecies already setup in the right directory structure.

In order to setup the data source bean in applicationContext I had 2 choices


Variation #1 (using data source class=”org.springframework.jdbc.datasource.DriverManagerDataSource” which was prepackaged data source with NetBeans for Spring development)

Snippet from applicationContext.xml

<!-- Declare a datasource that has pooling capabilities-->  
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"    
destroy-method="close"    p:driverClass="com.mysql.jdbc.Driver"    
p:jdbcUrl="jdbc:mysql://localhost:3306/yourmarketnet"   
p:user="root"    
p:password="arya6678"   
p:acquireIncrement="5"    
p:idleConnectionTestPeriod="60"    
p:maxPoolSize="100"    
p:maxStatements="50"    
p:minPoolSize="10" /> 
<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->   
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">    
<property name="dataSource" ref="dataSource"/>    
<property name="configLocation" ref="classpath:hibernate.cfg.xml"/>    
<property name="packagesToScan" value="com.yourmarketnet.*" />
</bean>

Variation 2, Error result: My Application fails to run, Apache Tomcat Log:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'acquireIncrement' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'acquireIncrement' 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:1361) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086) 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:293) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'acquireIncrement' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'acquireIncrement' 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:1052) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)

Variation #2
(using data source class=” org.apache.commons.dbcp.BasicDataSource”) Then I decided to download Apache Commons dbcp jar , and I added the “commons-dbcp-1.4.jar” to my project.

Snippet from applicationContext.xml

<!-- Declare a datasource that has pooling capabilities-->  
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"    
destroy-method="close"    p:driverClass="com.mysql.jdbc.Driver"    
p:jdbcUrl="jdbc:mysql://localhost:3306/yourmarketnet"   
p:user="root"    
p:password="arya6678"   
p:acquireIncrement="5"    
p:idleConnectionTestPeriod="60"    
p:maxPoolSize="100"    
p:maxStatements="50"    
p:minPoolSize="10" /> 
<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->   
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">    
<property name="dataSource" ref="dataSource"/>    
<property name="configLocation" ref="classpath:hibernate.cfg.xml"/>    
<property name="packagesToScan" value="com.yourmarketnet.*" />
</bean>

Variation 2, Error result: My Application fails to run, Apache Tomcat Log:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'acquireIncrement' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'acquireIncrement' 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:1361) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086) 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:293) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'acquireIncrement' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'acquireIncrement' 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:1052) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358) ... 21 more

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The p namespace (p:user, p:password, p:acquireIncrement) is used to set properties on your java beans. It looks like you are trying to set the acquireIncrement property on the org.apache.commons.dbcp.BasicDataSource and org.springframework.jdbc.datasource.DriverManagerDataSource classes. According to the documentation for these classes (Apache Commons DBCP and Spring's Data Source), neither of these have a setter method for this property which is why you see the NotWritablePropertyException in your stacktrace.

Check out the Spring's docs regarding the p-namespace about configuration.

If I were choosing, I would pick Apache's data base connection pool (dbcp). Simple & well used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...