依赖注入
1.构造器注入
2.Set方式注入
2.1 Address类
Address为引用对象
2.2 Student类
get,set,toString方法要添加,此处省略。
2.3 beans.xml
2.4 测试类
2.5 其他类型的注入
修改为student.toString,Address.toSring,同时Address类添加toString方法,beans.xml中Address添加属性
2.6 其他类型测试结果
3.拓展方式注入
1、P命名空间注入 : 需要在头文件中加入约束文件
导入约束 : xmlns:p=“http://www.springframework.org/schema/p”
2、c 命名空间注入 : 需要在头文件中加入约束文件导入约束 : xmlns:c=“http://www.springframework.org/schema/c”
发现问题:爆红了,刚才我们没有写有参构造!解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!
3.1 P命名空间注入
user类
新建userbeans.xml
测试类
3.2 C命名空间注入
user类
userbeans.xml
测试类