Spring框架——IOC 容器的创建与使用
企业级开发框架
Spring Framework 是整个 Spring 生态的基础,各个模块都是基于 Spring Framework 衍生出来的。
Spring 的两大核心机制 IOC 控制翻转、AOP 面向切面编程。
IOC 对象创建不再由开发者完成,而是容器自动创建,开发者直接取出来用即可。
1、新建maven项目
2、添加依赖
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.sunjian</groupId>
- <artifactId>springPrac</artifactId>
- <version>1.0-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.2.2.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
3、新建实体类
Student
- package com.sunjian.entity;
- /**
- * @author sunjian
- * @date 2020/3/14 9:13
- */
- public class Student {
- private String id;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- private String name;
- private String age;
- @Override
- public String toString() {
- return "Student{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", age='" + age + '\'' +
- '}';
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- }
Student2
- package com.sunjian.entity;
- /**
- * @author sunjian
- * @date 2020/3/14 10:05
- */
- public class Student2 {
- private String id;
- private String name;
- private String age;
- public Student2(String id, String name, String age) {
- this.id = id;
- this.name = name;
- this.age = age;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- @Override
- public String toString() {
- return "Student{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", age='" + age + '\'' +
- '}';
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- }
Student3
- package com.sunjian.entity;
- /**
- * @author sunjian
- * @date 2020/3/14 10:05
- */
- public class Student3 {
- private String id;
- private String name;
- private String age;
- private Student4 student4;
- public Student4 getStudent4() {
- return student4;
- }
- public void setStudent4(Student4 student4) {
- this.student4 = student4;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Student3{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", age='" + age + '\'' +
- ", student4=" + student4 +
- '}';
- }
- }
Student4
- package com.sunjian.entity;
- /**
- * @author sunjian
- * @date 2020/3/14 10:05
- */
- public class Student4 {
- private String id;
- private String name;
- private String age;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- @Override
- public String toString() {
- return "Student{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", age='" + age + '\'' +
- '}';
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- }
Students
- package com.sunjian.entity;
- /**
- * @author sunjian
- * @date 2020/3/14 12:05
- */
- public class Students {
- private String id;
- private String name;
- private String age;
- private Classes classes;
- @Override
- public String toString() {
- return "Students{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", age='" + age + '\'' +
- '}';
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public Classes getClasses() {
- return classes;
- }
- public void setClasses(Classes classes) {
- this.classes = classes;
- }
- }
Classes
- package com.sunjian.entity;
- import java.util.List;
- /**
- * @author sunjian
- * @date 2020/3/14 12:02
- */
- public class Classes {
- private String id;
- private String name;
- private List<Students> students;
- public List<Students> getStudents() {
- return students;
- }
- public void setStudents(List<Students> students) {
- this.students = students;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Classes{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", students=" + students +
- '}';
- }
- }
4、在resources目录下新建spring.xml文件,添加bean
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="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">
- <!-- 无参构造方式 -->
- <bean id="stu" class="com.sunjian.entity.Student">
- <property name="id" value="1"></property>
- <!--<property name="name" value="sunjian"></property>-->
- <!--特殊字符处理-->
- <property name="name">
- <value>
- <![CDATA[<孙健>]]>
- </value>
- </property>
- <property name="age" value="32"></property>
- </bean>
- <!-- 有参构造方式 1-->
- <bean id="stu_1" class="com.sunjian.entity.Student2">
- <constructor-arg index="0" value="1"></constructor-arg>
- <constructor-arg index="1" value="孙健"></constructor-arg>
- <constructor-arg index="2" value="31"></constructor-arg>
- </bean>
- <!-- 有参构造方式 2 常用 -->
- <bean id="stu_2" class="com.sunjian.entity.Student2">
- <constructor-arg name="id" value="1"></constructor-arg>
- <constructor-arg name="name" value="孙健"></constructor-arg>
- <constructor-arg name="age" value="32"></constructor-arg>
- </bean>
- <!-- 有参构造方式 3-->
- <bean id="stu_3" class="com.sunjian.entity.Student2">
- <constructor-arg value="1"></constructor-arg>
- <constructor-arg value="孙健"></constructor-arg>
- <constructor-arg value="33"></constructor-arg>
- </bean>
- <!-- 两个bean的级联方式 一对一-->
- <bean id="stu3" class="com.sunjian.entity.Student3">
- <property name="id" value="1"></property>
- <property name="age" value="33"></property>
- <property name="name" value="sunjian stu3"></property>
- <property name="student4" ref="stu4"></property>
- </bean>
- <bean id="stu4" class="com.sunjian.entity.Student4">
- <property name="id" value="1"></property>
- <property name="age" value="34"></property>
- <property name="name" value="sunjian stu4"></property>
- </bean>
- <!-- bean对象之间的集合注入 一对多-->
- <bean id="classes" class="com.sunjian.entity.Classes">
- <property name="id" value="1"></property>
- <property name="name" value="243班"></property>
- <property name="students">
- <list>
- <ref bean="stus1"></ref>
- <ref bean="stus2"></ref>
- <ref bean="stus3"></ref>
- </list>
- </property>
- </bean>
- <bean id="stus1" class="com.sunjian.entity.Students">
- <property name="id" value="1"></property>
- <property name="name" value="张三"></property>
- <property name="age" value="30"></property>
- </bean>
- <bean id="stus2" class="com.sunjian.entity.Students">
- <property name="id" value="2"></property>
- <property name="name" value="李四"></property>
- <property name="age" value="31"></property>
- </bean>
- <bean id="stus3" class="com.sunjian.entity.Students">
- <property name="id" value="3"></property>
- <property name="name" value="赵四"></property>
- <property name="age" value="32"></property>
- </bean>
- </beans>
5、测试类
- package com.sunjian.test;
- import com.sunjian.entity.*;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * @author sunjian
- * @date 2020/3/14 9:14
- */
- public class Test {
- public static void main(String[] args) {
- // 加载IOC容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
- System.out.println("-------------无参构造方式-------------------------");
- // 根据bean的id去拿
- // Student student = (Student) applicationContext.getBean("stu");
- // 根据类型去拿 (注意:spring.xml中只能有一个class="com.sunjian.entity.Student")
- Student student = applicationContext.getBean(Student.class);
- System.out.println(student);
- System.out.println("-------------有参构造方式-------------------------");
- // Student2 student2 = (Student2) applicationContext.getBean("stu_2");
- // Student2 student2 = (Student2) applicationContext.getBean("stu_3");
- Student2 student2 = (Student2) applicationContext.getBean("stu_1");
- System.out.println(student2);
- System.out.println("--------------两个bean的级联方式------------------------");
- Student3 student3 = (Student3) applicationContext.getBean("stu3");
- System.out.println(student3);
- System.out.println("---------------bean对象之间的集合注入-----------------------");
- Classes classes = (Classes) applicationContext.getBean("classes");
- System.out.println(classes);
- }
- }
运行结果
- -------------无参构造方式-------------------------
- Student{id='1', name='
- <孙健>
- ', age='32'}
- -------------有参构造方式-------------------------
- Student{id='1', name='孙健', age='31'}
- --------------两个bean的级联方式------------------------
- Student3{id='1', name='sunjian stu3', age='33', student4=Student{id='1', name='sunjian stu4', age='34'}}
- ---------------bean对象之间的集合注入-----------------------
- 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 容器的创建与使用的更多相关文章
- Spring框架IOC容器和AOP解析 非常 有用
Spring框架IOC容器和AOP解析 主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...
- Spring框架IOC容器和AOP解析
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- spring框架--IOC容器,依赖注入
思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action 多个 [维护成员变量] service 一个 [不需要维护公共变量] dao 一个 [不需要维护 ...
- SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系
一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...
- Spring的IOC容器第一辑
一.Spring的IOC容器概述 Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后 ...
- spring的Ioc容器与AOP机制
为什么要使用Spring的Ioc容器? 1.首先,spring是一个框架,框架存在的目的就是给我们的编程提供简洁的接口,可以使得我们专注于业务的开发,模块化,代码简洁,修改方便. 通过使用spring ...
- spring之IOC容器创建对象
1.术语了解 1.1组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架非侵入式设计 引入了框架,对现有的类结构没有影响. 例如:Hi ...
- Spring的IoC容器(转)BeanFactory
Spring的IoC容器 Spring读书笔记-----Spring的Bean之Bean的基本概念 加菲猫 Just have a little faith. Spring的IoC容器 (用户持久化类 ...
- [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired
Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...
随机推荐
- Java遍历文件夹的两种方法(非递归和递归)
import java.io.File; import java.util.LinkedList; public class FileSystem { public static int num ...
- gitlab配置邮箱
邮件测试Notify.test_email('xx@qq.com', 'Message Subject', 'Message Body').deliver_now 参考 https://www.cen ...
- flutter 入门(Mac)
背景 近一年时间都在用 React Native 开发公司的 APP,水平基本上可以说是登堂入室了.前一段时间兴起一阵 Flutter 热,后端的同事都有推荐,今年 Google 大会又推出 flut ...
- 无线城域网 WMAN
无线城域网 WMAN (Wireless Metropolitan Area Network) 1.1.概述 2002 年 4 月通过了 802.16 无线城域网的标准.欧洲的 ETSI 也制订类似的 ...
- [ASP.NET Core 3框架揭秘] 服务承载系统[1]: 承载长时间运行的服务[上篇]
借助.NET Core提供的承载(Hosting)系统,我们可以将任意一个或者多个长时间运行(Long-Running)的服务寄宿或者承载于托管进程中.ASP.NET Core应用仅仅是该承载系统的一 ...
- vim 编辑器技巧 打开多窗口编辑 vsp
我有两个配置文件 [root@gameserver1 conf]# ls auth_1.json auth_2.json 先打开auth_2.json 在vim编辑器中打开auth_1.json,在打 ...
- 关于Markdown下无法使用表格的解决方案
关于Markdown下无法使用表格的解决方案 写表格,出现如下场景 解决方法.点击左下角M的表示,切换到extra模式 打开了新世界.如果不能点击,估计是你没有激活pro的权限,百度下就可以了. 或者 ...
- Dart 运行速度测评与比较
引言 Dart 是一门优秀的跨平台语言,尽管生态方面略有欠缺,但无疑作为一门编程语言来说,Dart 是很优美,很健壮的,同时也引入了一些先进的编程范式,值得去学习. 测试内容 现在,我们就来测评一下D ...
- 微信小程序支付到第三方商户账号
使用场景:合作商家使用本公司小程序开店,要求支付金额直接到合作商家的公司微信账户; 使用要求:合作商家需提供微信支付关联,商户号,商户API密钥,API证书(该证书只用作退款功能,不开发退款可以不用) ...
- Newman+Jenkins实现接口自动化测试
目录 一.是什么Newman 二.如何安装 三.如何使用 1.运行本地文件 2.运行在线文件 3.以node.js库运行 4.导出报告 四.命令行测试真实接口 1.导出collection文件 2.导 ...