企业级开发框架  

  Spring Framework 是整个 Spring 生态的基础,各个模块都是基于 Spring Framework 衍生出来的。

  Spring 的两大核心机制 IOC 控制翻转、AOP 面向切面编程。

    IOC 对象创建不再由开发者完成,而是容器自动创建,开发者直接取出来用即可。

1、新建maven项目

2、添加依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com.sunjian</groupId>
  8. <artifactId>springPrac</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-context</artifactId>
  15. <version>5.2.2.RELEASE</version>
  16. </dependency>
  17. </dependencies>
  18.  
  19. </project>

3、新建实体类

Student

  1. package com.sunjian.entity;
  2.  
  3. /**
  4. * @author sunjian
  5. * @date 2020/3/14 9:13
  6. */
  7. public class Student {
  8. private String id;
  9.  
  10. public String getId() {
  11. return id;
  12. }
  13.  
  14. public void setId(String id) {
  15. this.id = id;
  16. }
  17.  
  18. private String name;
  19. private String age;
  20.  
  21. @Override
  22. public String toString() {
  23. return "Student{" +
  24. "id='" + id + '\'' +
  25. ", name='" + name + '\'' +
  26. ", age='" + age + '\'' +
  27. '}';
  28. }
  29.  
  30. public String getName() {
  31. return name;
  32. }
  33.  
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37.  
  38. public String getAge() {
  39. return age;
  40. }
  41.  
  42. public void setAge(String age) {
  43. this.age = age;
  44. }
  45. }

Student2

  1. package com.sunjian.entity;
  2.  
  3. /**
  4. * @author sunjian
  5. * @date 2020/3/14 10:05
  6. */
  7. public class Student2 {
  8. private String id;
  9. private String name;
  10. private String age;
  11.  
  12. public Student2(String id, String name, String age) {
  13. this.id = id;
  14. this.name = name;
  15. this.age = age;
  16. }
  17.  
  18. public String getId() {
  19. return id;
  20. }
  21.  
  22. public void setId(String id) {
  23. this.id = id;
  24. }
  25.  
  26. @Override
  27. public String toString() {
  28. return "Student{" +
  29. "id='" + id + '\'' +
  30. ", name='" + name + '\'' +
  31. ", age='" + age + '\'' +
  32. '}';
  33. }
  34.  
  35. public String getName() {
  36. return name;
  37. }
  38.  
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42.  
  43. public String getAge() {
  44. return age;
  45. }
  46.  
  47. public void setAge(String age) {
  48. this.age = age;
  49. }
  50. }

Student3

  1. package com.sunjian.entity;
  2.  
  3. /**
  4. * @author sunjian
  5. * @date 2020/3/14 10:05
  6. */
  7. public class Student3 {
  8. private String id;
  9. private String name;
  10. private String age;
  11. private Student4 student4;
  12.  
  13. public Student4 getStudent4() {
  14. return student4;
  15. }
  16.  
  17. public void setStudent4(Student4 student4) {
  18. this.student4 = student4;
  19. }
  20.  
  21. public String getId() {
  22. return id;
  23. }
  24.  
  25. public void setId(String id) {
  26. this.id = id;
  27. }
  28.  
  29. public String getName() {
  30. return name;
  31. }
  32.  
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36.  
  37. public String getAge() {
  38. return age;
  39. }
  40.  
  41. public void setAge(String age) {
  42. this.age = age;
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return "Student3{" +
  48. "id='" + id + '\'' +
  49. ", name='" + name + '\'' +
  50. ", age='" + age + '\'' +
  51. ", student4=" + student4 +
  52. '}';
  53. }
  54. }

Student4

  1. package com.sunjian.entity;
  2.  
  3. /**
  4. * @author sunjian
  5. * @date 2020/3/14 10:05
  6. */
  7. public class Student4 {
  8. private String id;
  9. private String name;
  10. private String age;
  11.  
  12. public String getId() {
  13. return id;
  14. }
  15.  
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19.  
  20. @Override
  21. public String toString() {
  22. return "Student{" +
  23. "id='" + id + '\'' +
  24. ", name='" + name + '\'' +
  25. ", age='" + age + '\'' +
  26. '}';
  27. }
  28.  
  29. public String getName() {
  30. return name;
  31. }
  32.  
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36.  
  37. public String getAge() {
  38. return age;
  39. }
  40.  
  41. public void setAge(String age) {
  42. this.age = age;
  43. }
  44. }

Students

  1. package com.sunjian.entity;
  2.  
  3. /**
  4. * @author sunjian
  5. * @date 2020/3/14 12:05
  6. */
  7. public class Students {
  8. private String id;
  9. private String name;
  10. private String age;
  11. private Classes classes;
  12.  
  13. @Override
  14. public String toString() {
  15. return "Students{" +
  16. "id='" + id + '\'' +
  17. ", name='" + name + '\'' +
  18. ", age='" + age + '\'' +
  19. '}';
  20. }
  21.  
  22. public String getId() {
  23. return id;
  24. }
  25.  
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29.  
  30. public String getName() {
  31. return name;
  32. }
  33.  
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37.  
  38. public String getAge() {
  39. return age;
  40. }
  41.  
  42. public void setAge(String age) {
  43. this.age = age;
  44. }
  45.  
  46. public Classes getClasses() {
  47. return classes;
  48. }
  49.  
  50. public void setClasses(Classes classes) {
  51. this.classes = classes;
  52. }
  53. }

Classes

  1. package com.sunjian.entity;
  2.  
  3. import java.util.List;
  4.  
  5. /**
  6. * @author sunjian
  7. * @date 2020/3/14 12:02
  8. */
  9. public class Classes {
  10. private String id;
  11. private String name;
  12. private List<Students> students;
  13.  
  14. public List<Students> getStudents() {
  15. return students;
  16. }
  17.  
  18. public void setStudents(List<Students> students) {
  19. this.students = students;
  20. }
  21.  
  22. public String getId() {
  23. return id;
  24. }
  25.  
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29.  
  30. public String getName() {
  31. return name;
  32. }
  33.  
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37.  
  38. @Override
  39. public String toString() {
  40. return "Classes{" +
  41. "id='" + id + '\'' +
  42. ", name='" + name + '\'' +
  43. ", students=" + students +
  44. '}';
  45. }
  46. }

4、在resources目录下新建spring.xml文件,添加bean

  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 http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <!-- 无参构造方式 -->
  7. <bean id="stu" class="com.sunjian.entity.Student">
  8. <property name="id" value="1"></property>
  9. <!--<property name="name" value="sunjian"></property>-->
  10. <!--特殊字符处理-->
  11. <property name="name">
  12. <value>
  13. <![CDATA[<孙健>]]>
  14. </value>
  15. </property>
  16. <property name="age" value="32"></property>
  17. </bean>
  18.  
  19. <!-- 有参构造方式 1-->
  20. <bean id="stu_1" class="com.sunjian.entity.Student2">
  21. <constructor-arg index="0" value="1"></constructor-arg>
  22. <constructor-arg index="1" value="孙健"></constructor-arg>
  23. <constructor-arg index="2" value="31"></constructor-arg>
  24. </bean>
  25. <!-- 有参构造方式 2 常用 -->
  26. <bean id="stu_2" class="com.sunjian.entity.Student2">
  27. <constructor-arg name="id" value="1"></constructor-arg>
  28. <constructor-arg name="name" value="孙健"></constructor-arg>
  29. <constructor-arg name="age" value="32"></constructor-arg>
  30. </bean>
  31. <!-- 有参构造方式 3-->
  32. <bean id="stu_3" class="com.sunjian.entity.Student2">
  33. <constructor-arg value="1"></constructor-arg>
  34. <constructor-arg value="孙健"></constructor-arg>
  35. <constructor-arg value="33"></constructor-arg>
  36. </bean>
  37.  
  38. <!-- 两个bean的级联方式 一对一-->
  39. <bean id="stu3" class="com.sunjian.entity.Student3">
  40. <property name="id" value="1"></property>
  41. <property name="age" value="33"></property>
  42. <property name="name" value="sunjian stu3"></property>
  43. <property name="student4" ref="stu4"></property>
  44. </bean>
  45. <bean id="stu4" class="com.sunjian.entity.Student4">
  46. <property name="id" value="1"></property>
  47. <property name="age" value="34"></property>
  48. <property name="name" value="sunjian stu4"></property>
  49. </bean>
  50.  
  51. <!-- bean对象之间的集合注入 一对多-->
  52. <bean id="classes" class="com.sunjian.entity.Classes">
  53. <property name="id" value="1"></property>
  54. <property name="name" value="243班"></property>
  55. <property name="students">
  56. <list>
  57. <ref bean="stus1"></ref>
  58. <ref bean="stus2"></ref>
  59. <ref bean="stus3"></ref>
  60. </list>
  61. </property>
  62. </bean>
  63. <bean id="stus1" class="com.sunjian.entity.Students">
  64. <property name="id" value="1"></property>
  65. <property name="name" value="张三"></property>
  66. <property name="age" value="30"></property>
  67. </bean>
  68. <bean id="stus2" class="com.sunjian.entity.Students">
  69. <property name="id" value="2"></property>
  70. <property name="name" value="李四"></property>
  71. <property name="age" value="31"></property>
  72. </bean>
  73. <bean id="stus3" class="com.sunjian.entity.Students">
  74. <property name="id" value="3"></property>
  75. <property name="name" value="赵四"></property>
  76. <property name="age" value="32"></property>
  77. </bean>
  78.  
  79. </beans>

5、测试类

  1. package com.sunjian.test;
  2.  
  3. import com.sunjian.entity.*;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. /**
  8. * @author sunjian
  9. * @date 2020/3/14 9:14
  10. */
  11. public class Test {
  12. public static void main(String[] args) {
  13. // 加载IOC容器
  14. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
  15.  
  16. System.out.println("-------------无参构造方式-------------------------");
  17.  
  18. // 根据bean的id去拿
  19. // Student student = (Student) applicationContext.getBean("stu");
  20. // 根据类型去拿 (注意:spring.xml中只能有一个class="com.sunjian.entity.Student")
  21. Student student = applicationContext.getBean(Student.class);
  22. System.out.println(student);
  23.  
  24. System.out.println("-------------有参构造方式-------------------------");
  25.  
  26. // Student2 student2 = (Student2) applicationContext.getBean("stu_2");
  27. // Student2 student2 = (Student2) applicationContext.getBean("stu_3");
  28. Student2 student2 = (Student2) applicationContext.getBean("stu_1");
  29. System.out.println(student2);
  30.  
  31. System.out.println("--------------两个bean的级联方式------------------------");
  32.  
  33. Student3 student3 = (Student3) applicationContext.getBean("stu3");
  34. System.out.println(student3);
  35.  
  36. System.out.println("---------------bean对象之间的集合注入-----------------------");
  37. Classes classes = (Classes) applicationContext.getBean("classes");
  38. System.out.println(classes);
  39. }
  40. }

运行结果

  1. -------------无参构造方式-------------------------
  2. Student{id='1', name='
  3. <孙健>
  4. ', age='32'}
  5. -------------有参构造方式-------------------------
  6. Student{id='1', name='孙健', age='31'}
  7. --------------两个bean的级联方式------------------------
  8. Student3{id='1', name='sunjian stu3', age='33', student4=Student{id='1', name='sunjian stu4', age='34'}}
  9. ---------------bean对象之间的集合注入-----------------------
  10. Classes{id='1', name='243班', students=[Students{id='1', name='张三', age='30'}, Students{id='2', name='李四', age='31'}, Students{id='3', name='赵四', age='32'}]}

6、注意事项

  • 实体类中,必须有无参构造。
  • 成员变量必须有setter方法。

  IOC实例化对象的过程,通过反射 + XML解析的方式对spring.xml进行处理,反射拿到无参构造函数,调用创建对象,同时获取setter方法,调用完成成员变量的赋值。

OK.

Spring框架——IOC 容器的创建与使用的更多相关文章

  1. Spring框架IOC容器和AOP解析 非常 有用

    Spring框架IOC容器和AOP解析   主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...

  2. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  3. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  4. SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系

    一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...

  5. Spring的IOC容器第一辑

    一.Spring的IOC容器概述 Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后 ...

  6. spring的Ioc容器与AOP机制

    为什么要使用Spring的Ioc容器? 1.首先,spring是一个框架,框架存在的目的就是给我们的编程提供简洁的接口,可以使得我们专注于业务的开发,模块化,代码简洁,修改方便. 通过使用spring ...

  7. spring之IOC容器创建对象

    1.术语了解 1.1组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架非侵入式设计 引入了框架,对现有的类结构没有影响. 例如:Hi ...

  8. Spring的IoC容器(转)BeanFactory

    Spring的IoC容器 Spring读书笔记-----Spring的Bean之Bean的基本概念 加菲猫 Just have a little faith. Spring的IoC容器 (用户持久化类 ...

  9. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

随机推荐

  1. Java遍历文件夹的两种方法(非递归和递归)

    import java.io.File; import java.util.LinkedList; public class FileSystem {    public static int num ...

  2. gitlab配置邮箱

    邮件测试Notify.test_email('xx@qq.com', 'Message Subject', 'Message Body').deliver_now 参考 https://www.cen ...

  3. flutter 入门(Mac)

    背景 近一年时间都在用 React Native 开发公司的 APP,水平基本上可以说是登堂入室了.前一段时间兴起一阵 Flutter 热,后端的同事都有推荐,今年 Google 大会又推出 flut ...

  4. 无线城域网 WMAN

    无线城域网 WMAN (Wireless Metropolitan Area Network) 1.1.概述 2002 年 4 月通过了 802.16 无线城域网的标准.欧洲的 ETSI 也制订类似的 ...

  5. [ASP.NET Core 3框架揭秘] 服务承载系统[1]: 承载长时间运行的服务[上篇]

    借助.NET Core提供的承载(Hosting)系统,我们可以将任意一个或者多个长时间运行(Long-Running)的服务寄宿或者承载于托管进程中.ASP.NET Core应用仅仅是该承载系统的一 ...

  6. vim 编辑器技巧 打开多窗口编辑 vsp

    我有两个配置文件 [root@gameserver1 conf]# ls auth_1.json auth_2.json 先打开auth_2.json 在vim编辑器中打开auth_1.json,在打 ...

  7. 关于Markdown下无法使用表格的解决方案

    关于Markdown下无法使用表格的解决方案 写表格,出现如下场景 解决方法.点击左下角M的表示,切换到extra模式 打开了新世界.如果不能点击,估计是你没有激活pro的权限,百度下就可以了. 或者 ...

  8. Dart 运行速度测评与比较

    引言 Dart 是一门优秀的跨平台语言,尽管生态方面略有欠缺,但无疑作为一门编程语言来说,Dart 是很优美,很健壮的,同时也引入了一些先进的编程范式,值得去学习. 测试内容 现在,我们就来测评一下D ...

  9. 微信小程序支付到第三方商户账号

    使用场景:合作商家使用本公司小程序开店,要求支付金额直接到合作商家的公司微信账户; 使用要求:合作商家需提供微信支付关联,商户号,商户API密钥,API证书(该证书只用作退款功能,不开发退款可以不用) ...

  10. Newman+Jenkins实现接口自动化测试

    目录 一.是什么Newman 二.如何安装 三.如何使用 1.运行本地文件 2.运行在线文件 3.以node.js库运行 4.导出报告 四.命令行测试真实接口 1.导出collection文件 2.导 ...