<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><beans><!-- 通过set方法向类中注入值-->
<bean id = "student" class = "test.student.Student">
<!-- propeity只能通过set方法对 class所指向的类中注入值
注:该方法使用时class所指向的类中不能出现有参数构造器-->
<property name="age" value = "14"></property>
<property name="name" value = "张三"></property>
<property name="num" value = "1"></property></bean></beans></beans>
通过构造器赋值
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><beans><!-- <bean id = "student" class = "test.student.Student">
<property name="age" value = "14"></property>
<property name="name" value = "张三"></property>
<property name="num" value = "1"></property>
</bean> --><!-- 通过构造器注入 index可以不加。
如果不加index则<constrctor/>中参数顺序必须与构造器中参数顺序相同
切index中下表从‘0’开始 name参数双引号中的值是class所指向的类中的
变量名-->
<bean id = "student" class = "test.student.Student">
<constructor-arg value = "1" index = "0" name = "num"></constructor-arg><!-- 该语句可以理解为向test.student包中Student类中通过构造器给变量构造器中第一个参数‘num’赋值‘1’-->
<constructor-arg value = "13" index = "1" name = "age"></constructor-arg>
<constructor-arg value = "张三" index = "2" name = "name"></constructor-arg><!-- 注:该<constructor-arg/>方法中 index name 两个参数可有可无,
若添加则必须与你要住入值的变量的位置和变量名完全相同。若不添加
该语句的顺序必须与Class类的构造器中的参数顺序保持一致--></bean></beans></beans>
Spring IOC容器帮我们节省了new 的过程不需要我们再创建类的对象。在大规模、大任务量的项目中如果有一个类的类名或者赋值方式发生更该,那我们可能整个文件大规模报错很难维护,但是在Spring IOC容器 完美的解决了这个麻烦,我们只需要将applicatioContext.xml中引用该类的"bean"中的class更改即可,大大的增加了程序的可维护性。
目录Spring 学习总结(一) Spring IOC容器1.Spring简介2.学前准备3、编写代码Spring 学习总结(一) Spring IOC容器1.Spring简介简介:Spring是一个分层的JavaSE/EE一站式轻量级开源框架,解决业务逻辑层和其他各层的耦合问题,将面型接口的编程思想贯穿整个系统。目的:解决企业应用开发的复杂性功能:使用基本的JavaBean代替EJB...