在2023idea中實現SpringBoot的IoC和AOP的方法
一.代碼介紹
(一)創(chuàng)建名為studentDao的接口
在com.example.demo包下創(chuàng)建一層dao包,在dao包下建一個名為studentDao的接口
package com.example.demo.dao; public interface studentDao { public void insertStudent(); }
(二)創(chuàng)建名為studentDaoImpl的類
在dao包下建一個impl包,在impl包下建一個名為studentDaoImpl的類
package com.example.demo.dao.impl; import com.example.demo.dao.studentDao; import org.springframework.stereotype.Repository; @Repository//Dao層一般用這個。表示的是當這個類被掃描到的時候,會自動創(chuàng)建該類的對象,把它裝載到ioc容器當中供調用 public class studentDaoImpl implements studentDao { @Override public void insertStudent() { System.out.println("insertStudent被調用"); } }
(三)創(chuàng)建名為studentService的接口
在com.example.demo包下創(chuàng)建一層service包,在service包下建一個名為studentDao的接口
package com.example.demo.service; public interface studentService { public void saveStudent(); }
(四)創(chuàng)建名為studentServiceImpl的類
在service包下建一個impl包,在impl包下建一個名為studentServiceImpl的類
package com.example.demo.service.impl; import com.example.demo.dao.studentDao; import com.example.demo.service.studentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("studentService")//Service層中一般用這個。括號里面表示的是:id已經不是類名了,變成括號里面的studentService public class studentServiceImpl implements studentService { @Autowired//依賴注入中最常用的。表示的是會到容器中按照類型查找和裝配進ioc容器 private com.example.demo.dao.studentDao studentDao; //應用Dao層的東西 @Override public void saveStudent() { studentDao.insertStudent(); } }
(五)創(chuàng)建名為MyAspect的類
在com.example.demo包下創(chuàng)建一層config包,在config包下建一個名為MyAspect的類
package com.example.demo.config; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; @Aspect //表示是一個切面類 //@Component//表示自動化實例對象 @Configuration//表示的是一個配置類,是用來替換配置文件的 public class MyAspect { @Before("execution(* *.*.demo.*.impl.studentDaoImpl.insertStudent(..))")//切點表達式的簡略寫法 *表示對一個或者多個包沒有限制/也表示對訪問修飾符和返回類型無所謂 函數名(..)中的..表示對函數中的參數沒有限制 public void ad(){ } @Pointcut("execution(public void com.example.demo.dao.impl.studentDaoImpl.insertStudent())")//切點表達式 public void pointcut(){ } @Before("pointcut()")//切點表達式簡化 public void before(){ System.out.println("我是前置通知,我要在insertStudent方法之前執(zhí)行"); } }
(六)Demo1ApplicationTests
在test包下找到名為Demo1ApplicationTests的類
package com.example.demo; import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class Demo1ApplicationTests { /* @Autowired @Qualifier("studentService") 這兩個可以用 @Resource(name="studentService")直接調換 */ @Resource(name="studentService")//自動裝配 private com.example.demo.service.studentService studentService;//這里要用全限定類名來聲明 @Autowired//依賴注入中最常用的。表示的是會到bean容器中按照類型查找和裝配進ioc容器 private com.example.demo.dao.studentDao studentDao; @Test//測試類 void contextLoads() { } @Test public void testAnnoioc(){ studentService.saveStudent(); } @Test public void testaspect(){ studentDao.insertStudent(); } }
(七)導入spring-boot-starter-aop依賴
在pom.xml中導入spring-boot-starter-aop依賴
1.file->Settings->Plugins->搜索maven-search下載應用
2.Tools->Maven Search->搜索spring-boot-starter-aop任意版本復制粘貼到pom.xml中
3.去版本號
4.如圖所示,一直刷新到出現Dependencies,其里面找到org.springframework.boot:spring-boot-starter-aop:3.0.2
Maven配置查看博主的上一條發(fā)布內容
(八)運行結果
二.IoC
(一)什么是IoC
IoC (Inversion of Control )即控制反轉/反轉控制。它是一種思想不是一個技術實現。描述的是:Java 開發(fā)領域對象的創(chuàng)建以及管理的問題。
例如:現有類 A 依賴于類 B
傳統(tǒng)的開發(fā)方式 :往往是在類 A 中手動通過 new 關鍵字來 new 一個 B 的對象出來
使用 IoC 思想的開發(fā)方式 :不通過 new 關鍵字來創(chuàng)建對象,而是通過 IoC 容器(Spring 框架) 來幫助我們實例化對象。我們需要哪個對象,直接從 IoC 容器里面去取即可。
(二)為什么叫控制反轉
控制 :指的是對象創(chuàng)建(實例化、管理)的權力
反轉 :控制權交給外部環(huán)境(IoC 容器)
(三)IOC 解決了什么問題
IoC 的思想就是兩方之間不互相依賴,由第三方容器來管理相關資源。這樣有什么好處呢?
對象之間的耦合度或者說依賴程度降低;資源變的容易管理;
比如你用 Spring 容器提供的話很容易就可以實現一個單例。
(四)IoC 最常見以及最合理的實現方式叫做依賴注入(Dependency Injection,簡稱 DI)
(五)IoC注解 1.用于創(chuàng)建對象的
(1)@Repository:一般用于持久層(即Dao層)
作用:當這個類被掃描到的時候,會自動創(chuàng)建該類的對象,把它裝載到ioc容器當中供調用(用于把當前類對象存入spring容器中)
屬性:value 用于指定 bean 的 id。當我們不寫時,他默認值是當前類名,且首字母改小寫。 屬性 value 當只有一個值時,value 書寫可以省略。
如: 在IOC容器反射創(chuàng)建 AccountServiceImpl 的對象的時候。如果不傳入參數,則表現層:
IAccountService as = (IAccountService) ac.getBean("accountServiceImpl");
也可以通過在類上面配置 @Component("accountService"),則表現層傳入的參數應保持一樣:
IAccountService as = (IAccountService) ac.getBean("accountService");
(2)@Component:表示自動實例化對象
(3)@Controller:一般用在表現層(即Controller層)
(4)@Service:一般用在業(yè)務層(即Service層)
以上三個注解他們的作用和屬性與 Component 是一模一樣的,因此可以混用。
2.用于注入數據的
(1)@Autowired----依賴注入中最常用的
作用:會到容器中按照類型查找和裝配進IoC容器
(自動按照類型注入: 只要容器中有唯一的一個 bean 對象類型和要注入的數據類型匹配,就可以注入成功;如果 ioc 容器中沒有任何 bean 的類型和要注入的變量類型匹配,則報錯;如果 ioc 容器中有多個 bean 的類型和要注入的變量類型匹配,先根據 數據類型 (如:IAccountDao)找到范圍,并在范圍中在根據 變量的名稱(如:accountDao)與 IOC 容器的 key 相匹配,最終注入。)
出現的位置: 可以是變量上,也可以是方法上
細節(jié):在使用 Autowired 注解注入時,set 方法就不是必須的了。
(2)@Qualifier
作用:在按照類中注入的基礎之上再按照 value 注入。
他在給類成員注入時不能單獨(使用需和 Autowired 配合),但是在給方法參數注入時可以。
屬性:value 用于指定 bean 的 id,無須考慮 Autowired,但是還需要寫上 Autowired。eg:
@Autowired @Qualifier("accountDao1") private IAccountDao accountDao = null;
(3)@Resource
作用:直接按照 bean 的 id 注入。它可以獨立使用。
屬性:name 用于指定 bean 的 id。如:
@Resource(name = "accountDao1") private IAccountDao accountDao = null ;
以上三個注入只能注入其他的 bean 類型的數據,而基本類型和 String 類型無法使用上述注解實現。
另外,集合類型的注入只能通過 XML 來實現。
(4)@Value
作用:用于注入基本類型和 String 類型的數據
屬性:value 用于指定數據的值。
它可以使用 spring 中的 SpEl(也就是 spring 的 el 表達式),SpEl的寫法也是 ${表達式}
3.
用于改變作用范圍的 @Scope
作用:用于指定bean的作用范圍
屬性:value 指定范圍的取值。常用取值:singleton, prototype@Scope(“prototype”)
在類上使用,如:
@Component("accountService") @Scope("prototype") public class AccountServiceImpl implements IAccountService {...}
4.和生命周期相關
他們的作用就和在 bean 標簽中使用 init-method 和 destroy-method 的作用是一樣的
(1)@PostConstruct
作用:用于指定初始化方法
(2)@PreDestroy
作用:用于指定銷毀方法
@PostConstruct public void init(){ System.out.println("初始化"); } @PreDestroy public void destroy(){ System.out.println("銷毀方法Service");//這里單例對象需要手動關閉才會執(zhí)行該方法 }
三.AOP
(一)什么是AOP
AOP:在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態(tài)代理實現程序功能的統(tǒng)一維護的一種技術。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業(yè)務邏輯的各個部分進行隔離,從而使得業(yè)務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
(二)AOP 的作用及其優(yōu)勢
作用:在程序運行期間,在不修改源碼的情況下對方法進行功能增強
優(yōu)勢:減少重復代碼,提高開發(fā)效率,并且便于維護
(三)AOP 的底層實現
AOP 的底層是通過 Spring 提供的的動態(tài)代理技術實現的。在運行期間,Spring通過動態(tài)代理技術動態(tài)的生成代理對象,代理對象方法執(zhí)行時進行增強功能的介入,在去調用目標對象的方法,從而完成功能的增強。
(四)常用的動態(tài)代理技術
JDK 代理 : 基于接口的動態(tài)代理技術
cglib 代理:基于父類的動態(tài)代理技術
(五)AOP相關概念
1.Target(目標對象):代理的目標對象 (織入 Advice
的目標對象.。)
2.Proxy (代理):一個類被 AOP 織入增強后,就產生一個結果代理類
3.Joinpoint(連接點):所謂連接點是指那些被攔截到的點。在spring中,這些點指的是方法,因為spring只支持方法類型的連接點。(表示在程序中明確定義的點,典型的包括方法調用,對類成員的訪問以及異常處理程序塊的執(zhí)行等等,它自身還可以嵌套其它 joint point。)
4.Pointcut(切入點):所謂切入點是指我們要對哪些 Joinpoint 進行攔截的定義。(表示一組 joint point,這些 joint point 或是通過邏輯關系組合起來,或是通過通配、正則表達式等方式集中起來,它定義了相應的 Advice 將要發(fā)生的地方。)
5.Advice(通知/ 增強):所謂通知是指攔截到 Joinpoint 之后所要做的事情就是通知 (Advice 定義了在 Pointcut
里面定義的程序點具體要做的操作,它通過 before、after 和 around 來區(qū)別是在每個 joint point 之前、之后還是代替執(zhí)行的代碼。)
6.Aspect(切面):是切入點和通知的結合( Aspect 聲明類似于 Java 中的類聲明,在 Aspect 中會包含著一些 Pointcut 以及相應的 Advice。)
7.Weaving(織入):是指把增強應用到目標對象來創(chuàng)建新的代理對象的過程。spring采用動態(tài)代理織入,而AspectJ采用編譯期織入和類裝載期織入。 安裝的過程(將 Aspect
和其他對象連接起來, 并創(chuàng)建 Advice
d object 的過程)
(六)AOP注解
1.@Aspect
位置:類定義上方
作用:設置當前類為切面類
格式:
@Aspect public class AopAdvice { }
說明:一個beans標簽中可以配置多個aop:config標簽
2.@Pointcut
位置:方法定義上方
作用:使用當前方法名作為切入點引用名稱
格式:
@Pointcut("execution(* *(..))") public void pt() { }
說明:被修飾的方法忽略其業(yè)務功能,格式設定為無參無返回值的方法,方法體內空實現(非抽象)
3.@Before
位置:方法定義上方
作用:標注當前方法作為前置通知
格式:
@Before("pt()") public void before(){ }
無特殊參數
4.@After
位置:方法定義上方
作用:標注當前方法作為后置通知
格式:
@After("pt()") public void after(){ }
無特殊參數
5.@AfterReturning
位置:方法定義上方
作用:標注當前方法作為返回后通知
格式:
@AfterReturning(value="pt()",returning = "ret") public void afterReturning(Object ret) { }
特殊參數:
returning :設定使用通知方法參數接收返回值的變量名
6.@AfterThrowing
位置:方法定義上方
作用:標注當前方法作為異常后通知
格式:
@AfterThrowing(value="pt()",throwing = "t") public void afterThrowing(Throwable t){ }
特殊參數:
throwing :設定使用通知方法參數接收原始方法中拋出的異常對象名
7.@Around
位置:方法定義上方
作用:標注當前方法作為環(huán)繞通知
格式:
@Around("pt()") public Object around(ProceedingJoinPoint pjp) throws Throwable { Object ret = pjp.proceed(); return ret; }
無特殊參數
到此這篇關于在2023idea中如何實現SpringBoot的IoC和AOP的文章就介紹到這了,更多相關idea中實現SpringBoot的IoC和AOP內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
企業(yè)級Kubernetes管理平臺Wayne功能特性介紹
這篇文章主要為大家介紹了企業(yè)級Kubernetes管理平臺Wayne的功能特性及架構設計,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02Springboot?引入?Redis?并配置序列化并封裝RedisTemplate?
這篇文章主要介紹了Springboot?引入?Redis?并配置序列化并封裝RedisTemplate。文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09解決springSecurity 使用默認登陸界面登錄后無法跳轉問題
這篇文章主要介紹了解決springSecurity 使用默認登陸界面登錄后無法跳轉問題,項目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-12-12