Spring中的属性命名的小问题有什么?

Spring中的属性命名的小问题有什么?

近日,用Spring做了一个很简单的小练习,从配置文件中产生一个对象,然后打印这个对象的内容。

发现在Spring中,属性命名的第2个字母必须小写,大写就出错,但第3,4,5…个字母大写都正常。不知是怎么回事?

代码如下:

文件MainClass

package cn.itcast;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

public class MainClass

{

public static void main(String[] args)

{

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("/applicationContext.xml"));

Object student = factory.getBean("student");

System.out.printf( "%s /n",student );

}

}

文件Student.java

package cn.itcast;

public class Student

{

private String strName = null;

public String toString()

{

return "姓名:" + strName;

}

public String getStrName() {

return strName;

}

public void setStrName(String strName) {

this.strName = strName;

}

}

文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="student" class="cn.itcast.Student">

<property name="strName">

<value>唐老鸭</value>

</property>

</bean>

</beans>

运行是正常的,输出:

姓名:唐老鸭

但若将strName 的第2个字母改为大写sTrName,运行就出错了,不知是怎么回事???

文件Student.java

package cn.itcast;

public class Student

{

private String sTrName = null;

public String toString()

{

return "姓名:" + sTrName;

}

public String getSTrName() {

return sTrName;

}

public void setSTrName(String trName) {

sTrName = trName;

}

}

文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="student" class="cn.itcast.Student">

<property name="sTrName">

<value>唐老鸭</value>

</property>

</bean>

</beans>

错误信息:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sTrName' of bean class [cn.itcast.Student]: Bean property 'sTrName' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?

org.springframework.beans.NotWritablePropertyException: Invalid property 'sTrName' of bean class [cn.itcast.Student]: Bean property 'sTrName' 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:567)

at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:469)

at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:626)

at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:653)

at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:642)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1027)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:824)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:345)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)

at cn.itcast.MainClass.main(MainClass.java:12)

Eclipse和控制台下的运行结果都是一样。