[英]" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource
I am going to read properties from Spring.xml file and I am getting below an exception. 我将从Spring.xml文件中读取属性,并且得到了下面的异常。
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 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
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: ClassNotFoundException指示必需的类不在您的类路径中,您需要在类路径中使用spring jdbc,您可以在此处找到ID: http : //search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org。 springframework%22%20AND%20a%3A%22spring-jdbc%22 ,您可以像这样在您的Maven项目中进行汇总:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
or gradle: 或gradle:
compile 'org.springframework:spring-jdbc:4.2.0.RELEASE' 编译'org.springframework:spring-jdbc:4.2.0.RELEASE'
If you are not using maven then add this library commons-dbcp-jar in your classpath. 如果您不使用maven,则在您的类路径中添加此库commons-dbcp-jar 。 This should solve your problem. 这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.