JavaEE 学习笔记 —— Spring Framework
spring framework runtime
核心容器
容器:servlet 容器、集合
线程池、常量池、数据库连接池的作用类似于容器
但是他们属于设计模式中的享元模式,即从池子中取东西,如果取不到,再创建
spring 中的核心容器,类似于集合
作用
- 存放对象
- 取出来使用
在 spring 中,有些对象是由容器来创建的,并且复制管理对象间的依赖
对于对象间的依赖:A 对象的属性为 B 对象,A 和 B 就产生了依赖关系
IOC
概念:IOC,(Inversion of Control,控制反转) —— 以前自己 new 对象以及组织对象间的关系,现在全部交给容器来统一管理,控制权发送了反转,因此称为控制反转。
实现方式:使用 IOC 容器来实现
在 spring 在,称为 spring bean 容器、spring 容器或 IoC 容器
DI
概念:DI, (Dependency Injection,依赖注入) —— 由 IoC 容器在运行期间,动态的将某种依赖关系注入到对象之中
注入
把 B 对象注入到 A ,称为 A 的属性
把 A 注入 IoC 容器中
IoC/DI 的作用
不使用:
使用:
作用
解耦
spring 容器使用流程
使用流程
- 配置 pom.xml 文件
<?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>org.example</groupId><artifactId>spring01</artifactId><version>1.0-SNAPSHOT</version><properties><java.version>1.8</java.version><maven.compiler.source>${java.version}</maven.compiler.source><maven.compiler.target>${java.version}</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-framework.version>5.2.10.RELEASE</spring-framework.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring-framework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-framework.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version></dependency></dependencies><build><plugins><!-- 明确指定一些插件的版本,以免受到 maven 版本的影响 --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version></plugin><plugin><artifactId>maven-resources-plugin</artifactId><version>3.1.0</version></plugin><plugin><artifactId>maven-site-plugin</artifactId><version>3.3</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.2</version></plugin></plugins></build>
</project>
点击刷新,使得配置文件完成
- 在 resources 目录下创建 beans.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- org.example 代表扫描 org.example 包下的文件--><context:component-scan base-package="org.example"/></beans>
- 启动 spring 容器
package org.example;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {//根据Spring配置文件路径创建容器:应用上下文对象ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//关闭容器((ClassPathXmlApplicationContext) context).close();}
}
理解注入流程
- 启动 spring 容器:new ApplicationContext 的实现类
- 注册 / 注入对象到容器中:加载指定的配置文件,执行包扫描,指定的包,子包下,带 spring 框架提供的四个注解,会注入到容器中
@Controller (控制器)
@Service(服务)
@Repository(存储库)
@Component (组件) - 装配 / 注入依赖关系:如果注册到容器中的对象,属性使用一下两个注释
@Autowired(自动装配)
@Resource(资源)
注入代码讲解
1.使用 Controller 注解、Bean 注解 + 方法注入
bean 注解使用在方法上,该方法所在的类也需要注解
从容器中获取 Bean 对象的方法:
- 通过类型获取(需要容器中只有一个对象)
- 通过名称、id 获取
package org.example.controller;import org.example.model.Duck;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;// 使用 Controller 注解将该类注入到 Bean 容器中
@Controller
public class LoginController {// 方法前使用 Bean 注解, 也能够注入对象@Beanpublic Duck d1() {Duck duck = new Duck();duck.setName("唐老鸭");return duck;}@Beanpublic Duck d2() {Duck duck = new Duck();duck.setName("香酥鸭");return duck;}
}
- 使用 Repository 注解注入
package org.example.dao;import org.springframework.stereotype.Repository;@Repository
public class LoginRepository {}
- 创建 Bean 注入的类
package org.example.model;import lombok.Getter;
import lombok.Setter;
import lombok.ToString;@Getter
@Setter
@ToString
public class Duck {private String name;
}
- 通过 Service 注解注入
package org.example.service;import org.springframework.stereotype.Service;@Service
public class LoginService {// 如果注入的对象不止一个 那么需要使用 @Qualifier 注解来确定使用的是哪个对象@Autowired@Qualifier("luYa")public Duck duck;
}
- @Scope 注解每次都能创建新对象
package org.example.service;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;// Scope 注解每次都能获取新的 Bean 对象
@Service
@Scope("prototype")
public class LoginServicePrototype {}
- 检查注入结果
package org.example;import org.example.controller.LoginController;
import org.example.dao.LoginRepository;
import org.example.model.Duck;
import org.example.service.LoginService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {//根据Spring配置文件路径创建容器:应用上下文对象// 类加载路径下的 beans.xml 文件ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");// 1. 通过类型获取LoginRepository login1 = context.getBean(LoginRepository.class);// 2. 通过类名获取, 注意, 类名首字母小写LoginRepository login2 = (LoginRepository) context.getBean("loginRepository");System.out.println(login1);System.out.println(login2);// 通过类型获取LoginController login3 = context.getBean(LoginController.class);// 通过类名获取LoginController login4 = (LoginController) context.getBean("loginController");System.out.println(login3);System.out.println(login4);LoginService login5 = context.getBean(LoginService.class);// 通过类名获取LoginService login6 = (LoginService) context.getBean("loginService");System.out.println(login5);System.out.println(login6);Duck d1 = (Duck) context.getBean("d1");Duck d2 = (Duck) context.getBean("d2");System.out.println(d1);System.out.println(d2);// 同一类型注册了两个以上对象, 不能通过类型来获取
// Duck d3 = context.getBean(Duck.class);
// System.out.println(d3);//关闭容器((ClassPathXmlApplicationContext) context).close();}
}// 打印结果
// org.example.dao.LoginRepository@76a4d6c
// org.example.dao.LoginRepository@76a4d6c
// org.example.controller.LoginController@517cd4b
// org.example.controller.LoginController@517cd4b
// org.example.service.LoginService@6cc7b4de
// org.example.service.LoginService@6cc7b4de
// Duck(name=唐老鸭)
// Duck(name=香酥鸭)
BeanFactory 和 FactoryBean 的区别
- BeanFactory 是 Spring 的顶级接口
- FactroyBean 是工厂 Bean,本身是一个 Bean,且主要是生成 Bean 的工厂
把 getObject() 方法的返回值注册到容器中
JavaEE 学习笔记 —— Spring Framework相关推荐
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Linux+javaEE学习笔记之Linux网络环境配置
Linux+javaEE学习笔记之Linux网络环境配置 网络知识简单介绍: Ip地址是:IP地址是IP协议提供的一种统一的地址格式,它为互联网上的每一个网络和每一台主机分配一个逻辑地址,以此来屏蔽物 ...
- 学习笔记---Spring 5
Spring 5 1. 介绍 1.1 简介 历史:2002年,首次推出了 Spring 框架的雏形:interface 21.2004年3月24日,Spring诞生,发布了 1.0 正式版本. Rod ...
- JavaEE学习笔记整理
目录 1:明白什么是JavaEE 2:JavaEE的主要技术 (1)JDBC (2)JavaBeans (3)JSP (4)Servlet 3:JavaEE框架 (1)MVC框架 (2)SSM Jav ...
- javaee学习笔记1
1.JavaEE 我们编写的JSP代码中,由于大量的显示代码和业务逻辑混淆在一起,彼此嵌套,不利于程序的维护和扩展.当业务需求发生变化的时候,对于程序员和美工都是一个很重的负担. 为了程序的易维护性和 ...
- 学习笔记-Spring Boot 开发 RESTful Web API(一)
题记: 本篇是Spring Boot 开发学习系列中基础知识学习的一部分,为 RESTful Web API 相关基础知识,为实践操作奠定理论基础. REST不是一个标准,而是一种软件应用架构风格.基 ...
- Java学习笔记-Spring IoC 通过XML把Bean给IoC容器
运行截图如下: 程序结构: 源码如下: Bean.java package springioc.demo.bean;public class Bean {public Bean(){System.ou ...
- 小汤学编程之JavaEE学习day10——Spring
一.Spring简介 1.特点 2.核心特性 3.两大核心 二.Spring环境搭建 1.导包 2.准备数据库和表.实体类 3.定义dao层接口和接口映射文件 ...
- Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...
最新文章
- 艾伟:尽可能摆脱对HttpContext的依赖
- 当前主流、最新技术回眸(四)
- Socket编程知识必学/SELECT 编程
- 人工智障学习笔记——深度学习(2)卷积神经网络
- markdown简明使用语法
- 文本的垂直居中 WPF
- 通过图像的指针读取图像
- 20182319彭淼迪 2019-2020-1 《数据结构与面向对象程序设计》实验一报告
- 51 nod 1682 中位数计数
- awl伪装MAC地址进行多线程SYN攻击
- python_pandas入门(by offical document/reference)/loc和iloc操作/dataframe插入操作/pandas读取无表头的文件/查找某一列是否有某个值
- 智慧公路养护管理系统实现养护管理精细化
- U1C2 文本预处理
- 学术圈很火的 超材料、超表面、超透镜:什么时候可以代替传统透镜?
- 中国的手机支付在全球遥遥领先
- go sync.Mutex
- IDEA代码规约插件灵狐安装
- 阿里云弹性裸金属服务器规格族详解和使用场景
- 珂朵莉与妖精仓库的妖精们
- Java如何将word转成html_[JavaWeb基础] 025.JAVA把word转换成html