🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 属性 ``` type: 要注入的数据的数据类型 index: 注入的数据在构造函数中的所以位置,从0开始 name: 参数 value: 值 ref: 用于指定其他的bean类型数据 ``` ### 优缺点 ``` 1. 获取对象,必须注入数据,或者对象无法创建成功 2. 创建对象时,如果用不到,也必须提供 ``` ### bean ~~~ public class UserService { private String name; private Integer age; private Date birthday; public UserService(String name, Integer age, Date birthday) { this.name = name; this.age = age; this.birthday = birthday; } public void saveUser(){ System.out.println(name + " --- "+ age + " --- "+ birthday); } } ~~~ ### bean.xml ~~~ <bean id="userService" class="com.test.service.UserService"> <constructor-arg name="name" value="test"/> <constructor-arg name="age" value="11"/> <constructor-arg name="birthday" ref="now"/> </bean> <!--配置一个日期对象--> <bean id="now" class="java.util.Date"/> ~~~ ### test ~~~ public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.saveUser(); } ~~~