如何获得默认值(28)?这是 github 上更完整的代码https://github.com/Teemitze/test
package ru.otus.spring01.dao;
import ru.otus.spring01.domain.Person;
public class PersonDaoSimple implements PersonDao {
public Person findByName(String name) {
return new Person(name, 18);
}
public void setDefaultAge(String defaultAge) {
}
}
春天.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personDao" class="ru.otus.spring01.dao.PersonDaoSimple">
<property name="defaultAge" value="28"/>
</bean>
<bean id="personService" class="ru.otus.spring01.service.PersonServiceImpl">
<constructor-arg name="dao" ref="personDao"/>
</bean>
</beans>
正如评论中已经写的那样,您需要初始化字段并通过setter将值从xml传递给它