创建bean名称“t”时出错

时间:2017-08-10 12:24:34

标签: spring

我是Spring的新手,在使用Spring时尝试使用Spring,同时执行一个简单的程序,我遇到了错误

WARNING: Exception encountered during context initialization - cancelling refresh attempt: 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 'driver' of bean class [beans.Test]: Bean property 'driver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
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 'driver' of bean class [beans.Test]: Bean property 'driver' 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:1568)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    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 'driver' of bean class [beans.Test]: Bean property 'driver' 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.createNotWritablePropertyException(BeanWrapperImpl.java:243)
    at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:437)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:292)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1564)
    ... 13 more

这是我的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/emp"/>
    <property name="user" value="root"/>
    <property name="password" value="root123"/>
  </bean>
</beans>

Test.java

package beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Test implements InitializingBean, DisposableBean
{
   private String driver,url,user,password;
   private Connection con;

   public void setDriver(String driver) {
    this.driver = driver;
}
   public void setUrl(String url) {
    this.url = url;
}
   public void setUser(String user) {
    this.user = user;
}
   public void setPassword(String password) {
    this.password = password;
}

    @Override
public void afterPropertiesSet() throws Exception 
   {
      Class.forName(driver);
      con= DriverManager.getConnection(url, user, password);
      System.out.println("Connection Opened");
   }

    public void insert(int id,int age, String first, String last )throws Exception
    {
        PreparedStatement ps=con.prepareStatement("insert into employee vvalues(?,?,?,?)");
        ps.setInt(1, id);
        ps.setInt(2, age);
        ps.setString(3, first);
        ps.setString(4, last);
        ps.executeUpdate();
        System.out.println("Successfully Inserted");

    }

   @Override
    public void destroy() throws Exception 
    {
        con.close();
       System.out.println("Connection Closed");
    }

}

main()的

  package test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client 
{
public static void main(String[] args) 
  {
    ConfigurableApplicationContext cap=new ClassPathXmlApplicationContext("resources/spring.xml");

  }

}

这可能是什么原因?

我正在使用 Eclipse Oxygen 基本上这是使用JDBC的一些概念,我正在尝试测试Spring生命周期属性。

1 个答案:

答案 0 :(得分:1)

看起来你需要一个属性驱动程序Does the parameter type of the setter match the return type of the getter?的getter。

添加

 public String getDriver() {
    return driver;
}

其他属性相同。