1、set注入方式

(1)注入的为值类型(八大数据类型)的数据

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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-4.2.xsd ">
  3. <bean name="student" class="pers.zhb.domain.Student">
  4. <property name="sname" value="zhai"></property>
  5. <property name="snum" value="20200210"></property>
  6. <property name="sex" value="nan"></property>
  7. </bean>
  8. </beans>

也可以以子标签的方式配置:

  1. <property name="">
  2. <value>123</value>
  3. </property>

测试类:

  1. public class Test {
  2. public void test1(){
  3. ApplicationContext applicationContext=new
  4. ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
  5. Student student=(Student)applicationContext.getBean("student");
  6. System.out.println(student);
  7. }
  8. public static void main(String[] args){
  9. Test test=new Test();
  10. test.test1();
  11. }
  12. }

(2)注入的为引用数据类型的数据:

Student对象:

  1. package pers.zhb.domain;
  2. public class Student {
  3. private String snum;
  4. private String sname;
  5. private String sex;
  6. private Course course;
  7. public Course getCourse() {
  8. return course;
  9. }
  10.  
  11. public void setCourse(Course course) {
  12. this.course = course;
  13. }
  14.  
  15. public Student(){
  16. System.out.println("Student对象创建了!");
  17. }
  18. public String getSnum() {
  19. return snum;
  20. }
  21.  
  22. public void setSnum(String snum) {
  23. this.snum = snum;
  24. }
  25.  
  26. public String getSname() {
  27. return sname;
  28. }
  29.  
  30. public void setSname(String sname) {
  31. this.sname = sname;
  32. }
  33.  
  34. public String getSex() {
  35. return sex;
  36. }
  37.  
  38. public void setSex(String sex) {
  39. this.sex = sex;
  40. }
  41. @Override
  42. public String toString() {
  43. return "Student{" +
  44. "snum='" + snum + '\'' +
  45. ", sname='" + sname + '\'' +
  46. ", sex='" + sex + '\'' +
  47. ", course=" + course +
  48. '}';
  49. }
  50.  
  51. public void destory(){
  52. System.out.println("我是销毁的方法!");
  53. }
  54.  
  55. public void init(){
  56. System.out.println("我是初始化的方法!");
  57. }
  58.  
  59. }

Course对象:

  1. public class Course {
  2. private String cname;
  3. public String getCname() {
  4. return cname;
  5. }
  6.  
  7. public void setCname(String cname) {
  8. this.cname = cname;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Course{" +
  13. "cname='" + cname + '\'' +
  14. '}';
  15. }
  16. }

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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-4.2.xsd ">
  3. <bean name="student" class="pers.zhb.domain.Student">
  4. <property name="sname" value="zhai"></property>
  5. <property name="snum" value="20200210"></property>
  6. <property name="sex" value="nan"></property>
  7. <property name="course" ref="course"></property>
  8. </bean>
  9. <bean name="course" class="pers.zhb.domain.Course">
  10. <property name="cname" value="算法设计与分析"></property>
  11. </bean>
  12. </beans>

测试类:

  1. public class Test {
  2. public void test1(){
  3. ApplicationContext applicationContext=new
  4. ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
  5. Student student=(Student)applicationContext.getBean("student");
  6. System.out.println(student);
  7. }
  8. public static void main(String[] args){
  9. Test test=new Test();
  10. test.test1();
  11. }
  12. }

2、构造函数注入

创建Student对象:

  1. package pers.zhb.domain;
  2. public class Student {
  3. private String snum;
  4. private String sname;
  5. private String sex;
  6. private Course course;
  7. public Student(String snum, String sname, String sex, Course course) {
  8. this.snum = snum;
  9. this.sname = sname;
  10. this.sex = sex;
  11. this.course = course;
  12. }
  13. public Course getCourse() {
  14. return course;
  15. }
  16.  
  17. public void setCourse(Course course) {
  18. this.course = course;
  19. }
  20.  
  21. public Student(){
  22. System.out.println("Student对象创建了!");
  23. }
  24. public String getSnum() {
  25. return snum;
  26. }
  27.  
  28. public void setSnum(String snum) {
  29. this.snum = snum;
  30. }
  31.  
  32. public String getSname() {
  33. return sname;
  34. }
  35.  
  36. public void setSname(String sname) {
  37. this.sname = sname;
  38. }
  39.  
  40. public String getSex() {
  41. return sex;
  42. }
  43.  
  44. public void setSex(String sex) {
  45. this.sex = sex;
  46. }
  47. @Override
  48. public String toString() {
  49. return "Student{" +
  50. "snum='" + snum + '\'' +
  51. ", sname='" + sname + '\'' +
  52. ", sex='" + sex + '\'' +
  53. ", course=" + course +
  54. '}';
  55. }
  56.  
  57. public void destory(){
  58. System.out.println("我是销毁的方法!");
  59. }
  60.  
  61. public void init(){
  62. System.out.println("我是初始化的方法!");
  63. }
  64.  
  65. }

需要在Student类中创建一个构造函数。

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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-4.2.xsd ">
  3. <bean name="student" class="pers.zhb.domain.Student">
  4. <constructor-arg name="sname" value="zhai"></constructor-arg>
  5. <constructor-arg name="snum" value="123456"></constructor-arg>
  6. <constructor-arg name="sex" value="nan"></constructor-arg>
  7. <constructor-arg name="course" ref="course"></constructor-arg>
  8. </bean>
  9. <bean name="course" class="pers.zhb.domain.Course">
  10. <property name="cname" value="算法设计与分析"></property>
  11. </bean>
  12. </beans>

配置文件中的配置需要和构造函数中属性的配置一一对应。

其他属性:

  1. index:指定构造函数的参数的索引
  2. type:指定构造函数参数的类型

3、p名称空间方式(对set方式注入进行简化)

(1)导入p名称空间(前提):

  1. xmlns:p="http://www.springframework.org/schema/p"

(2)配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
  6. <bean name="student" class="pers.zhb.domain.Student" p:sname="zhai"
  7. p:sex="nan" p:snum="1234" p:course-ref="course">
  8. </bean>
  9. <bean name="course" class="pers.zhb.domain.Course">
  10. <property name="cname" value="电子技术"></property>
  11. </bean>
  12. </beans>

测试:

4、spel(Spring表达式语言)注入

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
  5. <bean name="student" class="pers.zhb.domain.Student">
  6. <property name="sname" value="zhang"></property>
  7. <property name="snum" value="11111"></property>
  8. <property name="sex" value="nv"></property>
  9. </bean>
  10. <bean name="course" class="pers.zhb.domain.Course">
  11. <property name="cname" value="电子技术"></property>
  12. </bean>
  13. <bean name="student1" class="pers.zhb.domain.Student">
  14. <property name="sname" value="#{student.sname}"></property>
  15. <property name="snum" value="#{student.snum}"></property>
  16. <property name="sex" value="#{student.sex}"></property>
  17. <property name="course" ref="course"></property>
  18. </bean>
  19. </beans>

可以直接去取已经创建的对象的值。

5、复杂类型注入

(1)数组类型:

添加一个值:

  1. <bean name="arr" class="pers.zhb.domain.CollectionBean">
  2. <property name="arr" value="加油!!"></property>
  3. </bean>

添加多个值:

  1. <bean name="arr" class="pers.zhb.domain.CollectionBean">
  2. <property name="arr">
  3. <array>
  4. <value>你好</value>
  5. <value>加油</value>
  6. <value>努力</value>
  7. </array>
  8. </property>
  9. </bean>

值加对象:

  1. <bean name="arr" class="pers.zhb.domain.CollectionBean">
  2. <property name="arr">
  3. <array>
  4. <value>你好</value>
  5. <value>加油</value>
  6. <value>努力</value>
  7. <ref bean="student"></ref>
  8. </array>
  9. </property>
  10. </bean>

第四个值为一个Student对象。

(2)List类型:

  1. <bean name="cb" class="pers.zhb.domain.CollectionBean">
  2. <property name="li">
  3. <list>
  4. <value>你好</value>
  5. <value>加油</value>
  6. <value>努力</value>
  7. <ref bean="student"></ref>
  8. </list>
  9. </property>
  10. </bean>

(3)map类型:

  1. <bean name="map" class="pers.zhb.domain.CollectionBean">
  2. <property name="map">
  3. <map>
  4. <entry key="key" value="value"></entry>
  5. <entry key="student" value-ref="student"></entry>
  6. </map>
  7. </property>
  8. </bean>

(4)properties类型:

  1. <bean name="prop" class="pers.zhb.domain.CollectionBean">
  2. <property name="properties">
  3. <props>
  4. <prop key="key">key</prop>
  5. </props>
  6. </property>
  7. </bean>

Spring属性注入(set方式、构造函数方式、p名称空间、spel、复杂类型)的更多相关文章

  1. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  2. Spring中属性注入的几种方式以及复杂属性的注入

    在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...

  3. Spring中属性注入的几种方式以及复杂属性的注入详解

    在spring框架中,属性的注入我们有多种方式,我们可以通过set方法注入,可以通过构造方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List.Map.Prope ...

  4. Spring静态注入的三种方式

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...

  5. Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

    Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...

  6. Spring 属性注入(二)BeanWrapper 结构

    Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...

  7. Spring 属性注入(三)AbstractNestablePropertyAccessor

    Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...

  8. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  9. SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式

           于java发育.一类程序猿必须依靠类的其他方法,它是通常new依赖类的方法,然后调用类的实例,这样的发展问题new良好的班统一管理的例子.spring提出了依赖注入的思想,即依赖类不由程 ...

随机推荐

  1. pandas 数据表中的字符与日期数据的处理

    前面我们有学习过有关字符串的处理和正在表达式,但那都是基于单个字符串或字符串列表的操作.下面将学习如何基于数据框操作字符型变量. 同时介绍一下如何从日期型变量中取出年份,月份,星期几等,如何计算两个日 ...

  2. python的各种包安装地址

    http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy-stack 这个网页里有python的所有包,whl的后缀是python压缩包的意思.在windows ...

  3. Java多线程_ReentrantLock

    ReentrantLock是重入锁,它与synchronized很像,它是synchronized的加强版,因为它具有一些synchronized没有的功能.下面我们看看两者的区别:synchroni ...

  4. 《Head First 设计模式》:外观模式

    正文 一.定义 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用. 要点: 外观模式将一个或数个类的复杂的一切都隐藏在背后,只显露出一个干净美好的外 ...

  5. 关于函数式接口, printable 自定义

    这段代码在jdk1.8可以使用,  由于我是jdk14, 会报错. 这里可以优化, lambda表达式进一步优化写为:  printString(System.Out::println); 注意案例版 ...

  6. Docker 镜像构建之 docker commit

    我们可以通过公共仓库拉取镜像使用,但是,有些时候公共仓库拉取的镜像并不符合我们的需求.尽管已经从繁琐的部署工作中解放出来,但是实际开发时,我们可能希望镜像包含整个项目的完整环境,在其他机器上拉取打包完 ...

  7. FormData格式的数据

    向服务器提交的是FormData格式的数据 || 必须添加以下两个配置项 contentType:false, processData:false,

  8. UI自动化测试、接口测试等自动化测试策略

    今天跟大家介绍UI测试.接口测试.单元测试主要内容,以及每种测试花费时间讨论.UI测试[Selenium]UI测试是最接近软件真实用户使用行为的测试类型.通常是模拟真实用户使用软件的行为,即模拟用户在 ...

  9. 原生 JavaScript30 练习 Day 1 (原生JS控制键盘模拟击鼓)

     代码如下   <!DOCTYPE html> <html lang="en"> <head>     <meta charset=&qu ...

  10. Ubuntu 16.04 安装CP210x,CH340驱动

    CH340 https://github.com/juliagoda/CH341SER CP210x 因为源码版本不是linux-source-4.15.0-91-generic,导致error,一个 ...