给不同数据类型注入值:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
  6. <bean id="entity" class="entity.TestEntity">
  7. <!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
  8. <property name="specialCharacter1">
  9. <value><![CDATA[P&G]]></value>
  10. </property>
  11. <!-- 把XML特殊字符替换为实体引用 -->
  12. <property name="specialCharacter2">
  13. <value>P&amp;G</value>
  14. </property>
  15. <!-- 定义内部Bean -->
  16. <property name="innerBean">
  17. <bean class="entity.User">
  18. <property name="username">
  19. <value>Mr. Inner</value>
  20. </property>
  21. </bean>
  22. </property>
  23. <!-- 注入List类型 -->
  24. <property name="list">
  25. <list>
  26. <!-- 定义List中的元素 -->
  27. <value>足球</value>
  28. <value>篮球</value>
  29. </list>
  30. </property>
  31. <!-- 注入数组类型 -->
  32. <property name="array">
  33. <arry>
  34. <!-- 定义数组中的元素 -->
  35. <value>足球</value>
  36. <value>篮球</value>
  37. </arry>
  38. </property>
  39. <!-- 注入Set类型 -->
  40. <property name="set">
  41. <set>
  42. <!-- 定义Set或数组中的元素 -->
  43. <value>足球</value>
  44. <value>篮球</value>
  45. </set>
  46. </property>
  47. <!-- 注入Map类型 -->
  48. <property name="map">
  49. <map>
  50. <!-- 定义Map中的键值对 -->
  51. <entry>
  52. <key>
  53. <value>football</value>
  54. </key>
  55. <value>足球</value>
  56. </entry>
  57. <entry>
  58. <key>
  59. <value>basketball</value>
  60. </key>
  61. <value>篮球</value>
  62. </entry>
  63. </map>
  64. </property>
  65. <!-- 注入Properties类型 -->
  66. <property name="props">
  67. <props>
  68. <!-- 定义Properties中的键值对 -->
  69. <prop key="football">足球</prop>
  70. <prop key="basketball">篮球</prop>
  71. </props>
  72. </property>
  73. <!-- 注入空字符串值 -->
  74. <property name="emptyValue">
  75. <value></value>
  76. </property>
  77. <!-- 注入null值 -->
  78. <property name="nullValue">
  79. <null/>
  80. </property>
  81. </bean>
  82. </beans>

1.DI(给属性赋值)的四种方式:

01.Student实体类:

  1. package cn.pb.bean;
  2.  
  3. /**
  4. * 学生实体类
  5. */
  6. public class Student {
  7. private String name; //姓名
  8. private Integer age; //年龄
  9. private Grade grade; //年级
  10.  
  11. @Override
  12. public String toString() {
  13. return "Student [name=" + name + ", age=" + age + ", grade=" + grade
  14. + "]";
  15. }
  16.  
  17. // p 注入的时候 必须要有无参构造
  18. public Student() {
  19. super();
  20. }
  21. // c 注入的时候 必须要有带参构造
  22. public Student(String name, Integer age, Grade grade) {
  23. super();
  24. this.name = name;
  25. this.age = age;
  26. this.grade = grade;
  27. }
  28. //p注入的时候 必须要有set()
  29. public String getName() {
  30. return name;
  31. }
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. public Integer getAge() {
  36. return age;
  37. }
  38. public void setAge(Integer age) {
  39. this.age = age;
  40. }
  41. public Grade getGrade() {
  42. return grade;
  43. }
  44. public void setGrade(Grade grade) {
  45. this.grade = grade;
  46. }
  47.  
  48. }

02.Grade实体类:

  1. package cn.pb.bean;
  2.  
  3. /**
  4. * 年级实体类
  5. */
  6. public class Grade {
  7. private String name; //年级名称
  8.  
  9. @Override
  10. public String toString() {
  11. return "Grade [name=" + name + "]";
  12. }
  13.  
  14. public Grade() {
  15. super();
  16. }
  17.  
  18. public Grade(String name) {
  19. super();
  20. this.name = name;
  21. }
  22.  
  23. public String getName() {
  24. return name;
  25. }
  26.  
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. }

03.DI注入的方式:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:c="http://www.springframework.org/schema/c"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd">
  9.  
  10. <!-- 配置年级对应的bean-->
  11. <bean id="grade" class="com.xdf.bean.Grade">
  12. <!--01.设值注入 (推荐使用,便于阅读) 在对应的类中必须有set方法,因为底层执行反射机制查询类中对应的setXxx(DI) -->
  13. <property name="gradeId" value="1"/>
  14. <property name="name" value="一年级"/>
  15. </bean>
  16.  
  17. <!-- 配置学生对应的bean 02.p命名空间赋值 必须有set()和无参构造
  18. <bean id="student" class="com.xdf.bean.Student"
  19. p:age="18" p:name="小黑" p:grade-ref="grade"/>-->
  20.  
  21. <!--03.通过构造方法给属性赋值 前提是 必须有对应的带参构造方法 不需要set和get,无参构造也不需要
  22. <bean id="student" class="com.xdf.bean.Student">
  23. 001:使用参数的下标
  24. <constructor-arg index="0" value="xiaohei"/>
  25. <constructor-arg index="1" value="19"/>
  26. <constructor-arg index="2" ref="grade"/>
  27. 002:使用参数的名称
  28. <constructor-arg name="name" value="xiaohei"/>
  29. <constructor-arg name="age" value="19"/>
  30. <constructor-arg name="grade" ref="grade"/>
  31. 003:使用参数的默认顺序
  32. <constructor-arg value="xiaohei"/>
  33. <constructor-arg value="19"/>
  34. <constructor-arg ref="grade"/>
  35. </bean>-->
  36.  
  37. <!--04.通过c命名空间(构造方法)给属性赋值 前提是 必须有对应的带参构造方法-->
  38. <bean id="student" class="com.xdf.bean.Student"
  39. c:age="18" c:name="xiaobai" c:grade-ref="grade"/>
  40.  
  41. </beans>

04.测试代码:

  1. package cn.pb;
  2.  
  3. import cn.pb.bean.Student;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. public class StudentTest {
  9. @Test
  10. public void test01(){
  11. ApplicationContext context=
  12. new ClassPathXmlApplicationContext("applicationContext.xml");
  13. /*
  14. * 获取容器给我们创建的Student对象 ioc的体现
  15. * 本身由自身创建对象的过程,把创建对象的权利移交给了spring容器! IOC 控制反转
  16. *
  17. * 之前 Student student=new Student();
  18. * 现在context.getBean("student");
  19. * context容器来创建对象了
  20. *
  21. */
  22. Student student=(Student) context.getBean("student");
  23. System.out.println(student);
  24. }
  25. }

2.自动装配(autowire):

01.主人实体类:

  1. /**
  2. * 主人类
  3. */
  4. public class Person {
  5.  
  6. private String name; //姓名
  7. private int age; //年龄
  8. private Dog dog; //主人的宠物
  9. private Cat cat; //主人的宠物
  10.  
  11. public Cat getCat() {
  12. return cat;
  13. }
  14.  
  15. public void setCat(Cat cat) {
  16. this.cat = cat;
  17. }
  18.  
  19. public Dog getDog() {
  20. return dog;
  21. }
  22.  
  23. public void setDog(Dog dog) {
  24. this.dog = dog;
  25. }
  26.  
  27. public String getName() {
  28. return name;
  29. }
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public int getAge() {
  36. return age;
  37. }
  38.  
  39. public void setAge(int age) {
  40. this.age = age;
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. return "Person{" +
  46. "name='" + name + '\'' +
  47. ", age=" + age +
  48. ", dog=" + dog +
  49. ", cat=" + cat +
  50. '}';
  51. }
  52.  
  53. public Person(String name, int age) {
  54. this.name = name;
  55. this.age = age;
  56. }
  57.  
  58. public Person() {
  59.  
  60. }
  61. }

02.宠物狗实体类:

  1. /**
  2. * 宠物狗类
  3. */
  4. public class Dog {
  5. private String name;
  6.  
  7. public String getName() {
  8. return name;
  9. }
  10.  
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return "Dog{" +
  18. "name='" + name + '\'' +
  19. '}';
  20. }
  21. }

03.宠物狗的子类小狗实体类:

  1. /**
     *继承了宠物狗类 所以宠物狗的非私有的东西 他全有
     */
    public class SmallDog extends Dog {
  2.  
  3. }

04.宠物猫实体类:

  1. /**
  2. * 小猫咪类
  3. */
  4. public class Cat {
  5. private String name;
  6.  
  7. public String getName() {
  8. return name;
  9. }
  10.  
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return "Dog{" +
  18. "name='" + name + '\'' +
  19. '}';
  20. }
  21. }

05.applicationContext.xml配置文件(自动装配):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd">
  7. <!-- autowire 自动装配对象 有两种方式
  8. 01.byName
  9. spring会根据实体类中的属性名,去找xml文件中id为属性名的bean进行装配!
  10. 02.byType
  11. spring会根据实体类中的属性类型,去找xml文件中找一个class是 属性类型的类进行装配,
  12. 如果有多个 会报错!
  13. -->
  14.  
  15. <!--配置的主人bean-->
  16. <bean id="person" class="cn.pb.bean.Person" autowire="byName">
  17. <property name="age" value="20"/>
  18. <property name="name" value="xiaohei"/>
  19. <!--直接引用 autowire属性没有效果 <property name="dog" ref="dog"/>-->
  20. </bean>
  21.  
  22. <!--配置 宠物狗bean-->
  23. <bean id="dog" class="cn.pb.bean.Dog">
  24. <property name="name" value="哈士奇"/>
  25. </bean>
  26. <!--配置 小狗bean-->
  27. <bean id="smallDog" class="cn.pb.bean.SmallDog">
  28. <!--SmallDog中没有任何属性 ,但是继承父类的name-->
  29. <property name="name" value="小狗哈士奇"/>
  30. </bean>
  31.  
  32. <!--主人的第二个宠物 猫咪-->
  33. <bean id="cat" class="cn.pb.bean.Cat">
  34. <property name="name" value="机器锚"/>
  35. </bean>
  36. </beans>

06.测试代码:

  1. public class PersonDemo {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. ApplicationContext context=new ClassPathXmlApplicationContext
  6. ("applicationContext.xml");
  7.  
  8. //获取主人信息
  9. Person person = (Person) context.getBean("person");
  10. System.out.println(person);
  11. }
  12.  
  13. }

Spring笔记04(DI(给属性赋值),自动装配(autowire))的更多相关文章

  1. Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析

    一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...

  2. spring(四):spring中给bean的属性赋值

    spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...

  3. Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配

    1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...

  4. Spring学习记录(三)---bean自动装配autowire

    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...

  5. Spring点滴十:Spring自动装配(Autowire)

    在基于XML配置元数据,在bean的配置信息中我们可以使用<constructor-arg/>和<property/>属性来实现Spring的依赖注入.Spring 容器也可以 ...

  6. Spring学习七----------Bean的配置之自动装配

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...

  7. Spring(二)scope、集合注入、自动装配、生命周期

    原文链接:http://www.orlion.ga/189/ 一.scope bean的scope属性中常用的有两种:singleton(单例,默认)和prototype(原型,每次创建新对象) 例: ...

  8. 大厂面试官问你META-INF/spring.factories要怎么实现自动扫描、自动装配?

    大厂面试官问你META-INF/spring.factories要怎么实现自动扫描.自动装配?   很多程序员想面试进互联网大厂,但是也有很多人不知道进入大厂需要具备哪些条件,以及面试官会问哪些问题, ...

  9. Spring注解 - 生命周期、属性赋值、自动装配

    一.Bean的生命周期 流程 Bean创建 -- 初始化 -- 销毁 创建: 单实例:在容器启动时创建对象 多实例:每次调用时创建对象 初始化: 都是在对象创建完成后,调用初始化方法 销毁: 单实例: ...

随机推荐

  1. Lua学习九----------Lua字符串

    © 版权声明:本文为博主原创文章,转载请注明出处 1.Lua字符串 - ''单引号间的一串字符 - ""双引号之间的一串字符 - [[]]之间的一串字符 2.Lua转义字符 3.字 ...

  2. 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。

    错误原因: xml文件中,本来是要配置成下面这样的: http://www.springframework.org/schema/context http://www.springframework. ...

  3. C#如何遍历数组?

    // 一维数组 int[] arr = { 1, 2, 3, 4, 5 }; foreach (int i in arr) { Console.WriteLine(i.ToString() + &qu ...

  4. antd引入普通html使用,将ant Design本地化

    一直想着能本地化antd的,不用npm以及dva那么复杂的配置环境来开发,并且本地化以后对以后链接flask的模板渲染机制也能很好的结合.下面是具体的实现方法: 1.将react的相关链接引入: &l ...

  5. Android发送验证码的倒计时button

    1 直接上图 2 原理 原理非常easy,就是把对应的倒计时逻辑等封装到一个控件中,并向外部提供接口. 3 代码 import java.util.Timer; import java.util.Ti ...

  6. iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题

    本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37  segmentfault-博客原文  http://segm ...

  7. EasyNVR如何实现跨域鉴权

    EasyNVR提供简单的登录鉴权,客户端通过用户名密码登录成功后,服务端返回认证token的cookie, 后续的接口访问, 服务端从cookie读取token进行校验. 但是, 在与客户系统集成时, ...

  8. 基于Darwin实现的分布式流媒体直播服务器系统

    各位EasyDarwin开源项目的爱好者,您好,这篇博客的年限有点老了,目前EasyDarwin已经采用全新的云平台架构,详细可以参考博客:http://blog.csdn.net/xiejiashu ...

  9. WCF基础之设计和实现服务协定

    本来前面还有一个章节“WCF概述”,这章都是些文字概述,就不“复制”了,直接从第二章开始. 当然学习WCF还是要些基础的.https://msdn.microsoft.com/zh-cn/hh1482 ...

  10. windows系统下nodejs、npm、express的下载和安装教程——2016.11.09

    1. node.js下载 首先进入http://nodejs.org/dist/,这里面的版本呢,几乎每个月都出几个新的,建议大家下载最新版本,看看自己的电脑是多少位的,别下错了. 下载完解压到你想放 ...