在使用级联的方法进行类属性的参数设置时报错
D:\jdk8u312\bin\java.exe "-javaagent:D:\IDEA_2020_2\IntelliJ IDEA 2020.2\lib\idea_rt.jar=51245:D:\IDEA_2020_2\IntelliJ IDEA 2020.2\bin" -Dfile.encoding=UTF-8 -classpath D:\jdk8u312\jre\lib\charsets.jar;D:\jdk8u312\jre\lib\ext\access-bridge-64.jar;D:\jdk8u312\jre\lib\ext\cldrdata.jar;D:\jdk8u312\jre\lib\ext\dnsns.jar;D:\jdk8u312\jre\lib\ext\jaccess.jar;D:\jdk8u312\jre\lib\ext\localedata.jar;D:\jdk8u312\jre\lib\ext\nashorn.jar;D:\jdk8u312\jre\lib\ext\sunec.jar;D:\jdk8u312\jre\lib\ext\sunjce_provider.jar;D:\jdk8u312\jre\lib\ext\sunmscapi.jar;D:\jdk8u312\jre\lib\ext\sunpkcs11.jar;D:\jdk8u312\jre\lib\ext\zipfs.jar;D:\jdk8u312\jre\lib\jce.jar;D:\jdk8u312\jre\lib\jfr.jar;D:\jdk8u312\jre\lib\jsse.jar;D:\jdk8u312\jre\lib\management-agent.jar;D:\jdk8u312\jre\lib\resources.jar;D:\jdk8u312\jre\lib\rt.jar;D:\121\apache-tomcat-8.0.50\spring5_demo1\out\production\spring5_demo1;D:\121\apache-tomcat-8.0.50\spring5_demo1\lib\commons-logging-1.1.1.jar;D:\121\apache-tomcat-8.0.50\spring5_demo1\lib\spring-beans-5.2.0.RC2.jar;D:\121\apache-tomcat-8.0.50\spring5_demo1\lib\spring-context-5.2.0.RC2.jar;D:\121\apache-tomcat-8.0.50\spring5_demo1\lib\spring-core-5.2.0.RC2.jar;D:\121\apache-tomcat-8.0.50\spring5_demo1\lib\spring-expression-5.2.0.RC2.jar com.test.Test2
九月 30, 2023 9:29:00 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userservice12' defined in class path resource [bean2.xml]: Error setting property values; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'dept' of bean class [com.service.UserService1]: Value of nested property 'dept' is null
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userservice12' defined in class path resource [bean2.xml]: Error setting property values; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'dept' of bean class [com.service.UserService1]: Value of nested property 'dept' is null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1744)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:878)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.test.Test2.addtest(Test2.java:12)
at com.test.Test2.main(Test2.java:24)
Caused by: org.springframework.beans.NullValueInNestedPathException: Invalid property 'dept' of bean class [com.service.UserService1]: Value of nested property 'dept' is null
at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:845)
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:816)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:256)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1740)
... 14 more
Process finished with exit code 1
这是xml代码:
<bean id="userservice12" class="com.service.UserService1">
<property name="username" value="小明"></property>
<property name="password" value="11231"></property>
<property name="userDao1">
<bean id="userdao11" class="com.dao.UserDaoImpl1">
<property name="dnmae" value="11xx"></property>
</bean>
</property>
<property name="dept.ddname" value="eqwe"></property>
</bean>
原因是dept.ddname的ddname属性是null,所以需要先将dept.ddname赋值
改为如下代码后成功:
<bean id="userservice12" class="com.service.UserService1">
<property name="username" value="小明"></property>
<property name="password" value="11231"></property>
<property name="userDao1">
<bean id="userdao11" class="com.dao.UserDaoImpl1">
<property name="dnmae" value="11xx"></property>
</bean>
</property>
<property name="dept">
<bean id="userdao11" class="com.dao.Dept">
<property name="ddname" value="222"></property>
</bean>
</property>
<property name="dept.ddname" value="eqwe"></property>
</bean>