Spring(看这一篇就够了)

Spring 概述
Spring 是最受欢迎的企业级 Java 应用程序开发框架,数以百万的来自世界各地的开发人员使用 Spring
框架来创建性能好、易于测试、可重用的代码。
Spring 框架是一个开源的 Java 平台,它最初是由 Rod Johnson 编写的,并且于 2003 6 月首次在
Apache 2.0 许可下发布。
Spring 是轻量级的框架,其基础版本只有 2 MB 左右的大小。
Spring 框架的核心特性是可以用于开发任何 Java 应用程序,但是在 Java EE 平台上构建 web 应用程序
是需要扩展的。 Spring 框架的目标是使 J2EE 开发变得更容易使用,通过启用基于 POJO 编程模型来促
进良好的编程实践。
Spring 三大核心容器: Beans Core Context
Spring 中两大核心技术, ICO( 控制反转 / 依赖注入 ),AOP( 面向切面编程 )
引入 Spring SpringMVC JAR
<spring.version>4.0.2.RELEASE</spring.version>
<!-- spring框架包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

控制反转 (其实就是把 new 对象放到了配置文件里)
将组件对象的控制权从代码本身转移到外部容器
组件化的思想:分离关注点,使用接口,不再关注实现
依赖的注入:将组件的构建和使用分开
实体类
public class HelloWorld {
private String message;
public void show(){
System.out.println(message);
}
public void setMessage(String message) {
this.message = message;
}
创建 xml 配置文件引入头部,然后使用 bean 创建对象 HelloWorld 并参数赋值 message
<bean id="helloWorld" class="com.hz.pojo.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
注意:
相当于创建对象
helloWorld = new HelloWorld();
helloWorld.setMassage("Hello World!");

读取配置文件并运行

//使用ClassPathXmlApplicationContext读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring
.xml");
//使用getBean("bean中ID属性值")获取对象
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
helloWorld.show();

依赖注入
Spring 创建对象的过程中,将对象依赖属性(简单值,集合,对象)通过配置设值给该对象
1 . 引入另一个 bean( 使用 ref 属性 )
dao
public class UserDaoImpl {
public void show(){
System.out.println("输出UserDaoImpl信息");
}

service
public class UserServiceImpl {
private UserDaoImpl userDao;
public void serviceShow(){
userDao.show();//调用userdao.show方法
}
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}

xml 配置文件
<!--定义UserDaoImpl类-->
<bean id="userDaoImpl" class="com.hz.dao.UserDaoImpl"></bean>
<!--定义UserServiceImpl类-->
<bean id="userService" class="com.hz.service.UserServiceImpl">
<!--使用ref属性将userDaoImpl注入userDao-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>

2. 构造函数注入( constructor-arg
<bean id="userDao" class="dao.impl.UserDaoImpl" />
<bean id="userService" class="service.impl.UserServiceImpl">
<constructor-arg><ref bean="userDao" /></constructor-arg>
</bean>

3.p命名空间注入属性值

引入命名空间 xmlns:p=" http://www.springframework.org/schema/p "
对于直接量(基本数据类型、字符串)属性: p: 属性名 =" 属性值 "
对于引用 Bean 的属性: p: 属性名 -ref="Bean id"
<bean id="user" class="pojo.User" p:age="23" p:username="张三" />
<bean id="userService" class="service.impl.UserServiceImpl"
p:dao-ref="userDao" />

4. 注入数组,字符串,集合 ..... 各种类型
private String specialCharacter1; // 特殊字符值1
private String specialCharacter2; // 特殊字符值2
private User innerBean; // JavaBean类型
private List<String> list; // List类型
private String[] array; // 数组类型
private Set<String> set; // Set类型
private Map<String, String> map; // Map类型
private Properties props; // Properties类型
private String emptyValue; // 注入空字符串值
private String nullValue = "init value"; // 注入null值
​
<bean id="entity" class="entity.TestEntity">
<!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
<property name="specialCharacter1">
<value><![CDATA[P&G]]></value>
</property>
<!-- 把XML特殊字符替换为实体引用 -->
<property name="specialCharacter2">
<value>P&amp;G</value>
</property>
<!-- 定义内部Bean -->
<property name="innerBean">
<bean class="entity.User">
<property name="username">
<value>Mr. Inner</value>
</property>
</bean>
</property>
<!-- 注入List类型 -->
<property name="list">
<list>
<!-- 定义List中的元素 -->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- 注入数组类型 -->​<property name="array">
<list>
<!-- 定义数组中的元素 -->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- 注入Set类型 -->
<property name="set">
<set>
<!-- 定义Set或数组中的元素 -->
<value>足球</value>
<value>篮球</value>
</set>
</property>
<!-- 注入Map类型 -->
<property name="map">
<map>
<!-- 定义Map中的键值对 -->
<entry>
<key>
<value>football</value>
</key>
<value>足球</value>
</entry>
<entry>
<key>
<value>basketball</value>
</key>
<value>篮球</value>
</entry>
</map>
</property>
<!-- 注入Properties类型 -->
<property name="props">
<props>
<!-- 定义Properties中的键值对 -->
<prop key="football">足球</prop>
<prop key="basketball">篮球</prop>
</props>
</property>
<!-- 注入空字符串值 -->
<property name="emptyValue">
<value></value>
</property>
<!-- 注入null值 -->
<property name="nullValue">
<null/>
</property>
</bean>
Spring IOC 常用注解
@Component :实现 Bean 组件的定义
@Repository([ 实例化名称 ]) :用于标注 DAO
@Service([ 实例化名称 ]) :用于标注业务类
@Controller :用于标注控制器类
@Autowired+@Qualifier("userDao") 等价于 @Resource(name = "userDao")
使用注解前要先开启注解
<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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 扫描包中注解标注的类 -->
<context:component-scan base-package="service,dao" />
Spring AOP 面向切面编程
如果说 IoC Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中
切面编程被广泛使用。
AOP Aspect Oriented Program 面向切面编程 ( 公共功能集中解决 )
AOP 当中的概念:
切入点( Pointcut : 在哪些类,哪些方法上切入( where
通知( Advice : 在方法执行的什么实际( when: 方法前 / 方法后 / 方法前后)做什么( what: 增强
的功能)
切面( Aspect : 切面 = 切入点 + 通知,通俗点就是:在什么时机,什么地方,做什么增强!
织入( Weaving : 把切面加入到对象,并创建出代理对象的过程。(由 Spring 来完成)
五中增强方式:
创建目标方法:
public class UserServiceImpl {
//…省略代码
public void addNewUser() {
......
}
}

创建增强处理类 UserServiceLogger
<!--使用joinPrint需要引入包 ,另外还需引入log4j包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
public class UserServiceLogger {
//用于打印日志信息
private static Logger log=Logger.getLogger(UserServiceLogger.class);
public void before(JoinPoint jp) {
log.info("调用 " + jp.getTarget() + " 的 " + jp.getSignature().
getName() + " 方法。方法入参:" + Arrays.toString(jp.getArgs()));
}
public void afterReturning(JoinPoint jp, Object result) {
log.info("调用 " + jp.getTarget() + " 的 " + jp.getSignature().
getName() + " 方法。方法返回值:" + result);
}
}

引入 xml 头部
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
<aop:config>
<!--定义切入点-->
<aop:pointcut id="pointcut" expression="execution(public void show())"/>
<!--织入增强处理-->
<aop:aspect ref="serviceLogger">
<!--使用前置增强,将切入点与before方法绑定-->
<aop:before method="before"
pointcut-ref="pointcut"></aop:before>
<!--使用后置增强,将切入点与before方法绑定 returning 返回参数接收-->
<aop:after-returning method="afterReturning"
pointcut-ref="pointcut" returning="result"/>
</aop:aspect>
</aop:config>
execution表达式匹配规则:
public * addNewUser(entity.User): “*”表示匹配所有类型的返回值。
public void *(entity.User): “*”表示匹配所有方法名。
public void addNewUser(..): “..”表示匹配所有参数个数和类型。
* com.service.*.*(..):匹配com.service包下所有类的所有方法。
* com.service..*.*(..):匹配com.service包及其子包下所有类的所有方法

AOP 注解
开启注解
<aop:aspectj-autoproxy>
**/**
* 使用注解定义切面
*/
@Aspect
@Component
public class UserServiceLogger {
private static final Logger log = Logger.getLogger(UserServiceLogger.class);
@Pointcut("execution(* service.UserService.*(..))")
public void pointcut() {}
@Before("pointcut()")
public void before(JoinPoint jp) {
log.info("调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName()
+ " 方法。方法入参:" + Arrays.toString(jp.getArgs()));
}
@AfterReturning(pointcut = "pointcut()", returning = "returnValue")
public void afterReturning(JoinPoint jp, Object returnValue) {
log.info("调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName()
+ " 方法。方法返回值:" + returnValue);
}
}

AOP 事务处理
导入 tx aop 命名空间
定义事务管理器 DataSourceTransactionManager 并为其注入数据源 Bean
<!--定义事务采用JDBC管理事务-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* com.hz.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
tx:method 其他属性
timeout :事务超时时间,允许事务运行的最长时间,以秒为单位。默认值为 -1 ,表示不超时
read-only :事务是否为只读,默认值为 false
rollback-for :设定能够触发回滚的异常类型 Spring 默认只在抛出 runtime exception 时才标识事
务回滚
可以通过全限定类名指定需要回滚事务的异常,多个类名用逗号隔开
no-rollback-for :设定不触发回滚的异常类型
Spring 默认 checked Exception 不会触发事务回滚
可以通过全限定类名指定不需回滚事务的异常,多个类名用英文逗号隔开
propagation 事务传播机制
propagation_requierd( 默认 ) :如果当前没有事务,就新建一个事务,如果已存在一个事务中,
加入到这个事务中,这是最常见的选择。
propagation_supports :支持当前事务,如果没有当前事务,就以非事务方法执行。
propagation_mandatory :使用当前事务,如果没有当前事务,就抛出异常。
propagation_required_new :新建事务,如果当前存在事务,把当前事务挂起。
propagation_not_supported :以非事务方式执行操作,如果当前存在事务,就把当前事务挂
起。
propagation_never :以非事务方式执行操作,如果当前事务存在则抛出异常。
propagation_nested :如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与
propagation_required 类似的操作
事务注解
开启注解
<tx:annotation-driven />

@Transactional 注解使用
@Transactional //类中开启事务
@Service("userService")
public class UserServiceImpl{
//使用事务
@Transactional(propagation = Propagation.REQUIRED)
public boolean addNewUser(User user) {
....
}
}

Spring 能帮我们做什么
①.Spring 能帮我们根据配置文件创建及组装对象之间的依赖关系。
.Spring 面向切面编程能帮助我们无耦合的实现日志记录,性能统计,安全控制。
.Spring 能非常简单的帮我们管理数据库事务。
.Spring 还提供了与第三方数据访问框架(如 Hibernate JPA )无缝集成,而且自己也提供了一
JDBC 访问模板来方便数据库访问。
.Spring 还提供与第三方 Web (如 Struts1/2 JSF )框架无缝集成,而且自己也提供了一套
Spring MVC 框架,来方便 web 层搭建。
.Spring 能方便的与 Java EE (如 Java Mail 、任务调度)整合,与更多技术整合(比如缓存框
架)。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/54702.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SFUD库移植

1.源码 GitHub - armink/SFUD: An using JEDECs SFDP standard serial (SPI) flash universal driver library | 一款使用 JEDEC SFDP 标准的串行 (SPI) Flash 通用驱动库 2.介绍 这个通用驱动库,实际就是帮你封装好了读写spiflash的函数, 我们只需要对接以下底层,就可以轻松…

【个人笔记】线程和线程池的状态以及转换方式

线程和线程池的状态是不一样的&#xff01;&#xff01; 线程有 6 种状态&#xff0c;查看Thread的State枚举类&#xff1a; NEW&#xff1a;创建后没启动的线程就处于这种状态RUNNABLE&#xff1a;正在java虚拟机中执行的线程就处于这种状态BLOCKED&#xff1a;受阻塞并等待…

Observability:构建下一代托管接入服务

作者&#xff1a;来自 Elastic Vishal Raj, Marc Lopez Rubio 随着无服务器&#xff08;serverless&#xff09;的引入&#xff0c;向 Elastic Cloud 发送可观察性数据变得越来越容易。你可以在 Elastic Cloud Serverless 中创建一个可观察性无服务器项目&#xff0c;并将可观察…

【Java】虚拟机(JVM)内存模型全解析

目录 一、运行时数据区域划分 版本的差异&#xff1a; 二、程序计数器 程序计数器主要作用 三、Java虚拟机 1. 虚拟机运行原理 2. 活动栈被弹出的方式 3. 虚拟机栈可能产生的错误 4. 虚拟机栈的大小 四、本地方法栈 五、堆 1. 堆区的组成&#xff1a;新生代老生代 …

Ubuntu磁盘不足扩容

1.问题 Ubuntu磁盘不足扩容 2.解决方法 安装一下 sudo apt-get install gpartedsudo gparted

Mysql梳理6——order by排序

目录 6 order by排序 6.1 排序数据 6.2 单列排序 6.3 多行排列 6 order by排序 6.1 排序数据 使用ORDER BY字句排序 ASC&#xff08;ascend&#xff09;:升序DESC(descend):降序 ORDER BY子句在SELECT语句的结尾 6.2 单列排序 如果没有使用排序操作&#xff0c;默认…

C语言课程设计题目一:职工信息管理系统设计

文章目录 题目一&#xff1a;职工信息管理系统设计代码块employeeManagement.hemployeeManage.ctest.c 调试验证录入信息&#xff0c;并浏览验证职工号唯一保存职工信息&#xff0c;加载职工信息按职工号进行查询根据id删除职工修改职工信息 题目一&#xff1a;职工信息管理系统…

下水道内缺陷识别检测数据集 yolo数据集 共2300张

下水道内缺陷识别检测数据集 yolo数据集 共2300张 下水道内部缺陷识别数据集&#xff08;Sewer Interior Defect Recognition Dataset, SIDRD&#xff09; 摘要 SIDRD 是一个专门针对下水道内部缺陷识别的数据集&#xff0c;旨在为城市基础设施维护和管理提供一个标准化的训练…

VmWare安装虚拟机保姆级教程(centos7,虚拟机网络设置,虚拟机桌面显示)

VMWare下载&#xff1a; 下载 VMware Workstation Pro - VMware Customer Connect 安装包&#xff1a;&#xff08;16的版本&#xff09;免费&#xff01;&#xff08;一个赞就行&#xff09; 一直点下一步即可&#xff0c;注意修改一下安装位置就好 二、安装虚拟机 安装虚…

论文复现:考虑电网交互的风电、光伏与电池互补调度运行(MATLAB-Yalmip-Cplex全代码)

论文复现:考虑电网交互的风电、光伏与电池储能互补调度运行(MATLAB-Yalmip-Cplex全代码) 针对风电、光伏与电化学储能电站互补运行的问题,已有大量通过启发式算法寻优的案例,但工程上更注重实用性和普适性。Yalmip工具箱则是一种基于MATLAB平台的优化软件工具箱,被广泛应用…

[uni-app]小兔鲜-02项目首页

轮播图 轮播图组件需要在首页和分类页使用, 封装成通用组件 准备轮播图组件 <script setup lang"ts"> import type { BannerItem } from /types/home import { ref } from vue // 父组件的数据 defineProps<{list: BannerItem[] }>()// 高亮下标 const…

【React】Ant Design 5.x版本drawer抽屉黑边问题

环境 antd: ^5.14.1react: ^18 问题情况 <Drawer open{open} closable{false} mask{false} width{680}getContainer{props.getContainer || undefined}><p>Some contents...</p><p>Some contents...</p><p>Some contents...</p> …

时序数据库 TDengine 的入门体验和操作记录

时序数据库 TDengine 的学习和使用经验 什么是 TDengine &#xff1f;什么是时序数据 &#xff1f;使用RPM安装包部署默认的网络端口 TDengine 使用TDengine 命令行&#xff08;CLI&#xff09;taosBenchmark服务器内存需求删库跑路测试 使用体验文档纠错 什么是 TDengine &…

OpenAI GPT o1技术报告阅读(2)- 关于模型安全性的测试案例

✨报告阅读&#xff1a;使用大模型来学习推理(Reason) 首先是原文链接&#xff1a;https://openai.com/index/learning-to-reason-with-llms/ 接下来我们看一个简单的关于模型安全性的测试&#xff0c;当模型被问到一个有风险的话题时&#xff0c;会如何思考并回答用户呢&…

C++ | Leetcode C++题解之第421题数组中两个数的最大异或值

题目&#xff1a; 题解&#xff1a; struct Trie {// 左子树指向表示 0 的子节点Trie* left nullptr;// 右子树指向表示 1 的子节点Trie* right nullptr;Trie() {} };class Solution { private:// 字典树的根节点Trie* root new Trie();// 最高位的二进制位编号为 30static…

【linux】gcc makefile

&#x1f525;个人主页&#xff1a;Quitecoder &#x1f525;专栏&#xff1a;linux笔记仓 目录 01.gcc如何完成02.gcc选项03.函数库与动静态链接静态链接动态链接库文件特点和用途动态链接版本和兼容性 04.makefile自动推导 01.gcc如何完成 预处理(进行宏替换) 预处理功能主要…

828华为云征文|使用Flexus X实例创建FDS+Nginx服务实现图片上传功能

一、Flexus X实例 什么是Flexus X实例呢&#xff0c;这是华为云最新推出的云服务器产品&#xff0c;如下图&#xff1a; 华为云推出的Flexus云服务器X系列&#xff0c;是在华为顶尖技术团队&#xff0c;特别是荣获国家科技进步奖的领军人物顾炯炯博士及其团队的主导下精心研发…

通过document获取节点元素

1.层级节点 <ul><li id"li1">1</li><li>2</li><li id"li3">3</li><li>4</li><li>5</li></ul><script>//获取id名为li1的元素赋值给li1let li1document.getElementById(li…

Java语言程序设计基础篇_编程练习题**18.34 (游戏:八皇后问题)

目录 题目&#xff1a;**18.34 (游戏:八皇后问题) 代码示例 代码解析 输出结果 使用文件 题目&#xff1a;**18.34 (游戏:八皇后问题) 八皇后问题是要找到一个解决方案&#xff0c;将一个皇后棋子放到棋盘上的每行中&#xff0c;并且两个皇后棋子之间不能相互攻击。编写个…

基于C#+SQL Server2005(WinForm)图书管理系统

图书管理系统 一、 首先把数据库脚本贴出来(数据库名为library) USE [library] GO /****** Object: Table [dbo].[books] Script Date: 06/12/2016 11:27:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[books]([bNum] [nvarchar](10…