0

I am going to read properties from Spring.xml file and I am getting below an exception.

Can you please tell me where the mistake is.

Test.java

public class Test {

    private Properties driver;
    public void setDriver(Properties driver) {
        this.driver = driver;
    }

    public void printData(){
        Set keys = driver.keySet();
        for(Object key : keys){
            System.out.println(key+":"+driver.getProperty((String) key));
        }
    }
}

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
            "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="t" class="beans.Test">
        <property name="driver" value="com.mysql.jdbc.Driver" />
        <property name="URL" value="jdbc:mysql://localhost:3306/test" />
        <property name="user" value="root" />
        <property name="password" value="" />   
    </bean>
</beans>

Client.java

public class Client {
    public static void main(String[] args) {
        ApplicationContext ap = new ClassPathXmlApplicationContext("resources/spring.xml");
        Test t = (Test) ap.getBean("t");
        t.printData();
    }
}

Below is the exception of what I am getting...

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'URL' of bean class [beans.Test]: Bean property 'URL' 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:1344)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at test.Client.main(Client.java:10)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'URL' of bean class [beans.Test]: Bean property 'URL' 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:1012)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:857)
    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:1341)
    ... 13 more
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Ashvin Ranpariya
  • 661
  • 2
  • 13
  • 25

2 Answers2

0

A ClassNotFoundException indicates that the necessary class is not in your classpath, you need spring jdbc in your classpath, you can find id here: http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.springframework%22%20AND%20a%3A%22spring-jdbc%22 and you can aggregate it in your maven project like that:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.0.RELEASE</version>
</dependency>

or gradle:

compile 'org.springframework:spring-jdbc:4.2.0.RELEASE'

dventura
  • 46
  • 2
  • I am not using maven, simply added jars to run spring example. is there any other solution without this ... org.springframework spring-jdbc 4.2.0.RELEASE – Ashvin Ranpariya Aug 21 '15 at 17:13
  • You should download and add the necessary jars to your buildpath in your IDE or indicates in the classpath option in your java command [link](http://stackoverflow.com/questions/2096283/including-jars-in-classpath-on-commandline-javac-or-apt) – dventura Aug 21 '15 at 17:19
  • I have already added spring-jdbc-4.1.4.RELEASE. lets try to add spring-jdbc-4.2.0.RELEASE. – Ashvin Ranpariya Aug 21 '15 at 17:22
  • i have tried spring-jdbc-4.2.0.RELEASE but cant get success, same Exception, can you please look again spring.xml, i think somewhere in xml having problem – Ashvin Ranpariya Aug 21 '15 at 17:26
  • Where are you running your code? simple compile and run, IDE, generated jar or application server? – dventura Aug 21 '15 at 17:36
  • I am running in Eclipse JAVA EE – Ashvin Ranpariya Aug 21 '15 at 17:46
  • I think that you are adding the Spring libraries from eclipse Library, and this does not contain spring-jdbc dependency, then you must add an external jar in your project build path. -Right click on the name of the project. -Build Path > Add External Archives -and select the spring-jdbc jar or -Right click on the name of the project. -Build Path > Configure Build Path -Libraries and add external jar. – dventura Aug 21 '15 at 18:17
  • sir, same way I am doing and adding external jars but i think problem is not in adding jars, problem should be in spring.xml – Ashvin Ranpariya Aug 21 '15 at 18:24
  • Ok, your context loader does not work because your bean "t" definition is incorrect, check that your properties names and values, example "user" property must be "username", also "connectionCachingEnabled" does not exist and you will must add the mysql driver dependency in your project. – dventura Aug 21 '15 at 19:02
0

If you are not using maven then add this library commons-dbcp-jar in your classpath. This should solve your problem.

pradex
  • 328
  • 4
  • 18