java學習總結(六)Spring IOC

一、Spring框架介紹

Spring優點:

1、方便解耦,簡化開發,IOC控制反轉

Spring 就是一個大工廠,可以將所有對象創建和依賴關系維護交給Spring

2、AOP 編程的支持

Spring 提供面向切編程,可以方便的實現對序進行權限攔截、運監控等功能

3、聲明式事務的支持(張三給李四轉賬,要么同時成功,要么同時失敗)

只需要通過配置就可以完成對事務的管理,而無手動編程

4、方便集成各種優秀框架

Spring 不排斥各種優秀的開源框架,其內部提供了對各種優優秀框架的支持(如Struts,Mybatis,Hibernate)。

SSH SSM

二、IOC和DI

控制反轉(Inversion on Control)IOC:對象的創建交給外部容器來完成(這里就是交給Spring容器),這就叫控制反轉。

IOC:Spring就是一個大的內存容器(一塊內存區域),三層架構里面上一層不需要再去new下一層對象,在上一層只需要寫下一層的接口,new對象由Spring容器幫我們完成,各層直接向Spring容器要對象就可以。

class StudentController{// 需要什么,就去創建什么(自己去new),這就叫“控制正轉”(通俗一點就是自己控制new哪個對象)private IStudentService studentService = new StudentServiceImpl();
}class StudentController{// 對象的創建交給別人去new(現在交給Spring容器new StudentServiceImpl(),new出來對方放在Spring容器),這就叫控制反轉“IOC”private IStudentService studentService; // 將Spring容器中new出來的對象通過set方法賦值給studentService,這個過程叫依賴注入:DIpublic void setStudentService(IStudentService studentService) {this.studentService = studentService;}
} 

依賴注入:Dependency injection (DI)

現在new這個對象不是由自己new,是由Spring容器幫我們new對象,現在要得到這個Spring容器new出來的對象,就要“依賴”Spring容器,“注入”Spring容器new出來的對象。

IOC和DI區別:

IOC:解決對象創建的問題(對象的創建交給別人)Inversion of Control。

DI:在創建完對象后,對象關系的處理就是依賴注入(通過set方法實現依賴注入)Dependency injection。

先有IOC(對象創建),再有DI(處理對象關系)

在三層架構中最終實現的效果是:在Controller不會出現Service層具體的實現類代碼,只會看到Service層接口,Service層也不會出現Dao層具體實現類的代碼,也只會看到Dao層的接口

四、Bean的屬性: scope范圍

1、singleton

是scope的默認值,單例模式,在Spring容器中只存在一個實例。

public void test3() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Student student1 = (Student) context.getBean("student");Student student2 = (Student) context.getBean("student");System.out.println(student1 == student2);// true
}

2、prototype

多例,會創建多個對象,每次去容器里面拿會創建一個新的實例

<bean scope="prototype" name="student" class="com.situ.spring.pojo.Student"/>public void test3() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Student student1 = (Student) context.getBean("student");Student student2 = (Student) context.getBean("student");System.out.println(student1 == student2);// false
}

五、Spring屬性注入方式

1、set方法注入

<bean name="banji" class="com.situ.spring.pojo.Banji"><property name="id" value="1"/><property name="name" value="Java2023"/>
</bean>
<bean name="student" class="com.situ.spring.pojo.Student"><!-- 值類型注入 --><property name="id" value="1"/><property name="name" value="張三"/><property name="age" value="23"/><property name="gender" value="男"/><!-- ref:reference參考、引用引用類型的注入--><property name="banji" ref="banji"/>
</bean>

2、構造方法注入

argument:參數

parameter:參數

<bean name="banji" class="com.situ.spring.pojo.Banji"><constructor-arg name="id" value="1"/><constructor-arg name="name" value="Java2023"/>
</bean>
<bean name="student" class="com.situ.spring.pojo.Student"><constructor-arg name="id" value="1"/><constructor-arg name="name" value="李四"/><constructor-arg name="age" value="23"/><constructor-arg name="gender" value="男"/><constructor-arg name="banji" ref="banji"/>
</bean>

3、注解方式注入

@Resource @Autowired

六、三層架構使用Spring來管理

1、set方法注入

三層架構使用Spring來管理,達到一個目的:在上層只看到下一層的接口就可以,不需要出現具體的實現類。

使用set方式注入:

<bean name="studentDao" class="com.situ.spring.dao.impl.StudentDaoImpl"/>
<bean name="studentService" class="com.situ.spring.service.impl.StudentServiceImpl"><property name="studentDao" ref="studentDao"/>
</bean>
<bean name="studentController" class="com.situ.spring.controller.StudentController"><property name="studentService" ref="studentService"/>
</bean>

2、注解開發方式

<!--base-package:是要掃描的包,掃描這個包下面類上帶有注解@Controller @Service @Repositioy  -->
<context:component-scan base-package="com.situ.spring"/>

@Controller @Service @Repository 本身沒有區別,都是new一個對象放到Spring容器中

@Component new一個對象放到Spring容器中,不起名字,放到容器中默認的名字是類名首個單詞首字母小寫

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {/*** The value may indicate a suggestion for a logical component name,* to be turned into a Spring bean in case of an autodetected component.* @return the suggested component name, if any (or empty String otherwise)*/@AliasFor(annotation = Component.class)String value() default "";}
/*
<bean name="courseController" class="com.situ.spring.controller.CourseController">
</bean>
@Controller 這個注解相當于在applicationContext.xml中寫的上面的bean,
默認的名字是類名的首個單詞小寫courseController
*/
@Controller("courseController")
public class CourseController {// <property name="courseService" ref="courseService"/>// @Resource:從Spring容器中根據名字拿出指定的對象注入進來@Resource(name = "courseService")private ICourseService courseService;public void  selectAll() {System.out.println("CourseController.selectAll()");courseService.selectAll();}
}/*
<bean name="courseService" class="com.situ.spring.service.impl.CourseServiceImpl">
</bean>
*/
@Service("courseService")
public class CourseServiceImpl implements ICourseService{//<property name="courseDao" ref="courseDao"/>@Resource(name = "courseDao")private ICourseDao courseDao;@Overridepublic void selectAll() {System.out.println("CourseServiceImpl.selectAll()");courseDao.selectAll();}
}// <bean name="courseDao" class="com.situ.spring.dao.impl.CourseDaoImpl"></bean>
@Repository("courseDao")
public class CourseDaoIml implements ICourseDao{@Overridepublic void selectAll() {System.out.println("CourseDaoIml.selectAll()");}
}
@Controller、@Service、@Repository這三個注解的作用和@Component是一樣的,都是new一個對象放到Spring容器中,目的是為了表明三層架構中不同的層。

七、Autowired 自動裝配

1、@Autowired和@Resource區別

  1. @Resource默認是按照名稱裝配的,是JDK提供的。
byName 通過參數名自動裝配,如果一個bean的name 和另外一個bean的 property 相同,就自動裝配。
  1. @Autowired是默認按照類型裝配的 ,是Spring提供的。
byType 通過參數的數據類型自動自動裝配,如果一個bean的數據類型和另外一個bean的property屬性的數據類型兼容,就自動裝配。

2、@Autowired一個接口有多個子類情況

@Service
public class BanjiServiceImpl implements IBanjiService {}@Service
public class BanjiServiceImpl2 implements IBanjiService{}

這樣會報錯: expected single matching bean but found 2: banjiServiceImpl,banjiServiceImpl2
因為在容器中子類對象有兩個,而且變量的名字是banjiService和任何一個容器中對象的名字都不一致,所以找不到(只有一個子類對象情況下變量名可以隨便寫)
也同時證明,@Controller、@Service、@Repository不起名字,默認的名字是類的名字首字母變小寫。
解決方法:
  1. 方法一:IBanjiService banjiServiceImpl2;
根據變量名去容器中找相同名字的bean對象,所以注入過來的是new BanjiServiceImpl2()的對象
  1. 方法二:還是希望寫成IBanjiService banjiService,@Qualifier中限定名字
// @Resource(name = "banjiServiceImpl2")
@Autowired
@Qualifier(value = "banjiServiceImpl2")
private IBanjiService banjiService;

3、@Autowired注入bean

@Controller
public class StudentController {// <property name="studentService" ref="studentService"/>// @Resource(name = "studentService")@Autowiredprivate IStudentService studentService;}//@Service("studentService")
@Service
public class StudentServiceImpl implements IStudentService{// @Resource(name = "studentDao")@Autowiredprivate IStudentDao studentDao;}//@Repository("studentDao")
@Repository
public class StudentDaoImpl implements IStudentDao{}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/898171.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/898171.shtml
英文地址,請注明出處:http://en.pswp.cn/news/898171.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

大模型推理:LM Studio在Mac上部署Deepseek-R1模型

LM Studio LM Studio是一款支持離線大模型部署的推理服務框架&#xff0c;提供了易用的大模型部署web框架&#xff0c;支持Linux、Mac、Windows等平臺&#xff0c;并提供了OpenAI兼容的SDK接口&#xff0c;主要使用LLama.cpp和MLX推理后端&#xff0c;在Mac上部署時選擇MLX推理…

AI技術學習筆記系列004:GPU常識

顯卡架構是GPU設計的核心&#xff0c;不同廠商有其獨特的架構演進。以下是主要廠商的顯卡架構概述&#xff1a; 一、NVIDIA Tesla&#xff08;2006-2010&#xff09; 代表產品&#xff1a;GeForce 8000系列&#xff08;G80&#xff09;。特點&#xff1a;首款統一著色架構&…

實驗- 分片上傳 VS 直接上傳

分片上傳和直接上傳是兩種常見的文件上傳方式。分片上傳將文件分成多個小塊&#xff0c;每次上傳一個小塊&#xff0c;可以并行處理多個分片&#xff0c;適用于大文件上傳&#xff0c;減少了單個請求的大小&#xff0c;能有效避免因網絡波動或上傳中斷導致的失敗&#xff0c;并…

Android視頻渲染SurfaceView強制全屏與原始比例切換

1.創建UI添加強制全屏與播放按鈕 2.SurfaceView控件設置全屏顯示 3.全屏點擊事件處理實現 4.播放點擊事件處理 5.使用接口更新強制全屏與原始比例文字 強制全屏/原始比例 點擊實現

數據結構——串、數組和廣義表

串、數組和廣義表 1. 串 1.1 串的定義 串(string)是由零個或多個字符組成的有限序列。一般記為 S a 1 a 2 . . . a n ( n ≥ 0 ) Sa_1a_2...a_n(n\geq0) Sa1?a2?...an?(n≥0) 其中&#xff0c;S是串名&#xff0c;單引號括起來的字符序列是串的值&#xff0c; a i a_i a…

無再暴露源站!群聯AI云防護IP隱匿方案+防繞過實戰

一、IP隱藏的核心原理 群聯AI云防護通過三層架構實現源站IP深度隱藏&#xff1a; 流量入口層&#xff1a;用戶訪問域名解析至高防CNAME節點&#xff08;如ai-protect.example.com&#xff09;智能調度層&#xff1a;基于AI模型動態分配清洗節點&#xff0c;實時更新節點IP池回…

1.5.3 掌握Scala內建控制結構 - for循環

Scala的for循環功能強大&#xff0c;支持單重和嵌套循環。單重for循環語法為for (變量 <- 集合或數組 (條件)) {語句組}&#xff0c;可選篩選條件&#xff0c;循環變量依次取集合值。支持多種任務&#xff0c;如輸出指定范圍整數&#xff08;使用Range、to、until&#xff0…

【MySQL基礎-9】深入理解MySQL中的聚合函數

在數據庫操作中&#xff0c;聚合函數是一類非常重要的函數&#xff0c;它們用于對一組值執行計算并返回單個值。MySQL提供了多種聚合函數&#xff0c;如COUNT、SUM、AVG、MIN和MAX等。這些函數在數據分析和報表生成中扮演著關鍵角色。本文將深入探討這些聚合函數的使用方法、注…

windows版本的時序數據庫TDengine安裝以及可視化工具

了解時序數據庫TDengine&#xff0c;可以點擊官方文檔進行詳細查閱 安裝步驟 首先找到自己需要下載的版本&#xff0c;這邊我暫時只寫windows版本的安裝 首先我們需要點開官網&#xff0c;找到發布歷史&#xff0c;目前TDengine的windows版本只更新到3.0.7.1&#xff0c;我們…

Web測試

7、Web安全測試概述 黑客技術的發展歷程 黑客基本涵義是指一個擁有熟練電腦技術的人&#xff0c;但大部分的媒體習慣將“黑客”指作電腦侵入者。 黑客技術的發展 在早期&#xff0c;黑客攻擊的目標以系統軟件居多。早期互聯網Web并非主流應用&#xff0c;而且防火墻技術還沒有…

華為OD機試 - 最長的完全交替連續方波信號(Java 2023 B卷 200分)

題目描述 給定一串方波信號,要求找出其中最長的完全連續交替方波信號并輸出。如果有多個相同長度的交替方波信號,輸出任意一個即可。方波信號的高位用1標識,低位用0標識。 說明: 一個完整的信號一定以0開始并以0結尾,即010是一個完整的信號,但101,1010,0101不是。輸入的…

游戲引擎學習第163天

我們可以在資源處理器中使用庫 因為我們的資源處理器并不是游戲的一部分&#xff0c;所以它可以使用庫。我說過我不介意讓它使用庫&#xff0c;而我提到這個的原因是&#xff0c;今天我們確實有一個選擇——可以使用庫。 生成字體位圖的兩種方式&#xff1a;求助于 Windows 或…

7、什么是死鎖,如何避免死鎖?【高頻】

&#xff08;1&#xff09;什么是死鎖&#xff1a; 死鎖 是指在兩個或多個進程的執行時&#xff0c;每個進程都持有資源 并 等待其他進程 釋放 它所需的資源&#xff0c;如果此時所有的進程一直占有資源而不釋放&#xff0c;就會陷入互相等待的一種僵局狀態。 死鎖只有同時滿足…

Compose 實踐與探索十四 —— 自定義布局

自定義布局在 Compose 中相對于原生的需求已經小了很多&#xff0c;先講二者在本質上的邏輯&#xff0c;再說它們的使用場景&#xff0c;兩相對比就知道為什么 Compose 中的自定義布局的需求較小了。 原生是在 xml 布局文件不太方便或者無法滿足需求時才會在代碼中通過自定義 …

【C++】:C++11詳解 —— 入門基礎

目錄 C11簡介 統一的列表初始化 1.初始化范圍擴展 2.禁止窄化轉換&#xff08;Narrowing Conversion&#xff09; 3.解決“最令人煩惱的解析”&#xff08;Most Vexing Parse&#xff09; 4.動態數組初始化 5. 直接初始化返回值 總結 聲明 1.auto 類型推導 2. declty…

oracle刪除表中重復數據

需求&#xff1a; 刪除wfd_procs_nodes_rwk表中&#xff0c;huser_id、dnode_id、rwk_name字段值相同的記錄&#xff0c;如果有多條&#xff0c;只保留一條。 SQL&#xff1a; DELETE FROM wfd_procs_nodes_rwk t WHERE t.rowid > (SELECT MIN(t1.rowid)FROM wfd_procs_n…

ESP32學習 -從STM32工程架構進階到ESP32架構

ESP32與STM32項目文件結構對比解析 以下是對你提供的ESP32項目文件結構的詳細解釋&#xff0c;并與STM32&#xff08;以STM32CubeIDE為例&#xff09;的常見結構進行對比&#xff0c;幫助你理解兩者的差異&#xff1a; 1. ESP32項目文件解析 文件/目錄作用STM32對應或差異set…

整形在內存中的存儲(例題逐個解析)

目錄 一.相關知識點 1.截斷&#xff1a; 2.整形提升&#xff1a; 3.如何 截斷&#xff0c;整型提升&#xff1f; &#xff08;1&#xff09;負數 &#xff08;2&#xff09;正數 &#xff08;3&#xff09;無符號整型&#xff0c;高位補0 注意&#xff1a;提升后得到的…

HTML中滾動加載的實現

設置div的overflow屬性&#xff0c;可以使得該div具有滾動效果&#xff0c;下面以div中包含的是table來舉例。 當table的元素較多&#xff0c;以至于超出div的顯示范圍的話&#xff0c;觀察下該div元素的以下3個屬性&#xff1a; clientHeight是div的顯示高度&#xff0c;scrol…

Netty基礎—7.Netty實現消息推送服務二

大綱 1.Netty實現HTTP服務器 2.Netty實現WebSocket 3.Netty實現的消息推送系統 (1)基于WebSocket的消息推送系統說明 (2)消息推送系統的PushServer (3)消息推送系統的連接管理封裝 (4)消息推送系統的ping-pong探測 (5)消息推送系統的全連接推送 (6)消息推送系統的HTTP…