在2023idea中實現(xiàn)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層一般用這個。表示的是當(dāng)這個類被掃描到的時候,會自動創(chuàng)建該類的對象,把它裝載到ioc容器當(dāng)中供調(diào)用
public class studentDaoImpl implements studentDao {
@Override
public void insertStudent() {
System.out.println("insertStudent被調(diào)用");
}
}(三)創(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已經(jīng)不是類名了,變成括號里面的studentService
public class studentServiceImpl implements studentService {
@Autowired//依賴注入中最常用的。表示的是會到容器中按照類型查找和裝配進ioc容器
private com.example.demo.dao.studentDao studentDao; //應(yīng)用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(..))")//切點表達式的簡略寫法 *表示對一個或者多個包沒有限制/也表示對訪問修飾符和返回類型無所謂 函數(shù)名(..)中的..表示對函數(shù)中的參數(shù)沒有限制
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")直接調(diào)換
*/
@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();
}
}(七)導(dǎo)入spring-boot-starter-aop依賴
在pom.xml中導(dǎo)入spring-boot-starter-aop依賴
1.file->Settings->Plugins->搜索maven-search下載應(yīng)用

2.Tools->Maven Search->搜索spring-boot-starter-aop任意版本復(fù)制粘貼到pom.xml中

3.去版本號

4.如圖所示,一直刷新到出現(xiàn)Dependencies,其里面找到org.springframework.boot:spring-boot-starter-aop:3.0.2

Maven配置查看博主的上一條發(fā)布內(nèi)容
(八)運行結(jié)果

二.IoC
(一)什么是IoC
IoC (Inversion of Control )即控制反轉(zhuǎn)/反轉(zhuǎn)控制。它是一種思想不是一個技術(shù)實現(xiàn)。描述的是:Java 開發(fā)領(lǐng)域?qū)ο蟮膭?chuàng)建以及管理的問題。
例如:現(xiàn)有類 A 依賴于類 B
傳統(tǒng)的開發(fā)方式 :往往是在類 A 中手動通過 new 關(guān)鍵字來 new 一個 B 的對象出來
使用 IoC 思想的開發(fā)方式 :不通過 new 關(guān)鍵字來創(chuàng)建對象,而是通過 IoC 容器(Spring 框架) 來幫助我們實例化對象。我們需要哪個對象,直接從 IoC 容器里面去取即可。
(二)為什么叫控制反轉(zhuǎn)
控制 :指的是對象創(chuàng)建(實例化、管理)的權(quán)力
反轉(zhuǎn) :控制權(quán)交給外部環(huán)境(IoC 容器)

(三)IOC 解決了什么問題
IoC 的思想就是兩方之間不互相依賴,由第三方容器來管理相關(guān)資源。這樣有什么好處呢?
對象之間的耦合度或者說依賴程度降低;資源變的容易管理;
比如你用 Spring 容器提供的話很容易就可以實現(xiàn)一個單例。
(四)IoC 最常見以及最合理的實現(xiàn)方式叫做依賴注入(Dependency Injection,簡稱 DI)
(五)IoC注解 1.用于創(chuàng)建對象的
(1)@Repository:一般用于持久層(即Dao層)
作用:當(dāng)這個類被掃描到的時候,會自動創(chuàng)建該類的對象,把它裝載到ioc容器當(dāng)中供調(diào)用(用于把當(dāng)前類對象存入spring容器中)
屬性:value 用于指定 bean 的 id。當(dāng)我們不寫時,他默認(rèn)值是當(dāng)前類名,且首字母改小寫。 屬性 value 當(dāng)只有一個值時,value 書寫可以省略。
如: 在IOC容器反射創(chuàng)建 AccountServiceImpl 的對象的時候。如果不傳入?yún)?shù),則表現(xiàn)層:
IAccountService as = (IAccountService) ac.getBean("accountServiceImpl");
也可以通過在類上面配置 @Component("accountService"),則表現(xiàn)層傳入的參數(shù)應(yīng)保持一樣:
IAccountService as = (IAccountService) ac.getBean("accountService");
(2)@Component:表示自動實例化對象
(3)@Controller:一般用在表現(xiàn)層(即Controller層)
(4)@Service:一般用在業(yè)務(wù)層(即Service層)
以上三個注解他們的作用和屬性與 Component 是一模一樣的,因此可以混用。
2.用于注入數(shù)據(jù)的
(1)@Autowired----依賴注入中最常用的
作用:會到容器中按照類型查找和裝配進IoC容器
(自動按照類型注入: 只要容器中有唯一的一個 bean 對象類型和要注入的數(shù)據(jù)類型匹配,就可以注入成功;如果 ioc 容器中沒有任何 bean 的類型和要注入的變量類型匹配,則報錯;如果 ioc 容器中有多個 bean 的類型和要注入的變量類型匹配,先根據(jù) 數(shù)據(jù)類型 (如:IAccountDao)找到范圍,并在范圍中在根據(jù) 變量的名稱(如:accountDao)與 IOC 容器的 key 相匹配,最終注入。)
出現(xiàn)的位置: 可以是變量上,也可以是方法上
細(xì)節(jié):在使用 Autowired 注解注入時,set 方法就不是必須的了。
(2)@Qualifier
作用:在按照類中注入的基礎(chǔ)之上再按照 value 注入。
他在給類成員注入時不能單獨(使用需和 Autowired 配合),但是在給方法參數(shù)注入時可以。
屬性: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 類型的數(shù)據(jù),而基本類型和 String 類型無法使用上述注解實現(xiàn)。
另外,集合類型的注入只能通過 XML 來實現(xiàn)。
(4)@Value
作用:用于注入基本類型和 String 類型的數(shù)據(jù)
屬性:value 用于指定數(shù)據(jù)的值。
它可以使用 spring 中的 SpEl(也就是 spring 的 el 表達式),SpEl的寫法也是 ${表達式}
3.用于改變作用范圍的 @Scope
作用:用于指定bean的作用范圍
屬性:value 指定范圍的取值。常用取值:singleton, prototype@Scope(“prototype”)
在類上使用,如:
@Component("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {...}4.和生命周期相關(guān)
他們的作用就和在 bean 標(biāo)簽中使用 init-method 和 destroy-method 的作用是一樣的
(1)@PostConstruct
作用:用于指定初始化方法
(2)@PreDestroy
作用:用于指定銷毀方法
@PostConstruct
public void init(){
System.out.println("初始化");
}
@PreDestroy
public void destroy(){
System.out.println("銷毀方法Service");//這里單例對象需要手動關(guān)閉才會執(zhí)行該方法
}三.AOP
(一)什么是AOP
AOP:在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個部分進行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
(二)AOP 的作用及其優(yōu)勢
作用:在程序運行期間,在不修改源碼的情況下對方法進行功能增強
優(yōu)勢:減少重復(fù)代碼,提高開發(fā)效率,并且便于維護
(三)AOP 的底層實現(xiàn)
AOP 的底層是通過 Spring 提供的的動態(tài)代理技術(shù)實現(xiàn)的。在運行期間,Spring通過動態(tài)代理技術(shù)動態(tài)的生成代理對象,代理對象方法執(zhí)行時進行增強功能的介入,在去調(diào)用目標(biāo)對象的方法,從而完成功能的增強。
(四)常用的動態(tài)代理技術(shù)
JDK 代理 : 基于接口的動態(tài)代理技術(shù)
cglib 代理:基于父類的動態(tài)代理技術(shù)

(五)AOP相關(guān)概念
1.Target(目標(biāo)對象):代理的目標(biāo)對象 (織入 Advice 的目標(biāo)對象.。)
2.Proxy (代理):一個類被 AOP 織入增強后,就產(chǎn)生一個結(jié)果代理類
3.Joinpoint(連接點):所謂連接點是指那些被攔截到的點。在spring中,這些點指的是方法,因為spring只支持方法類型的連接點。(表示在程序中明確定義的點,典型的包括方法調(diào)用,對類成員的訪問以及異常處理程序塊的執(zhí)行等等,它自身還可以嵌套其它 joint point。)
4.Pointcut(切入點):所謂切入點是指我們要對哪些 Joinpoint 進行攔截的定義。(表示一組 joint point,這些 joint point 或是通過邏輯關(guān)系組合起來,或是通過通配、正則表達式等方式集中起來,它定義了相應(yīng)的 Advice 將要發(fā)生的地方。)
5.Advice(通知/ 增強):所謂通知是指攔截到 Joinpoint 之后所要做的事情就是通知 (Advice 定義了在 Pointcut 里面定義的程序點具體要做的操作,它通過 before、after 和 around 來區(qū)別是在每個 joint point 之前、之后還是代替執(zhí)行的代碼。)
6.Aspect(切面):是切入點和通知的結(jié)合( Aspect 聲明類似于 Java 中的類聲明,在 Aspect 中會包含著一些 Pointcut 以及相應(yīng)的 Advice。)
7.Weaving(織入):是指把增強應(yīng)用到目標(biāo)對象來創(chuàng)建新的代理對象的過程。spring采用動態(tài)代理織入,而AspectJ采用編譯期織入和類裝載期織入。 安裝的過程(將 Aspect 和其他對象連接起來, 并創(chuàng)建 Adviced object 的過程)

(六)AOP注解
1.@Aspect
位置:類定義上方
作用:設(shè)置當(dāng)前類為切面類
格式:
@Aspect
public class AopAdvice {
}說明:一個beans標(biāo)簽中可以配置多個aop:config標(biāo)簽
2.@Pointcut
位置:方法定義上方
作用:使用當(dāng)前方法名作為切入點引用名稱
格式:
@Pointcut("execution(* *(..))")
public void pt() {
}說明:被修飾的方法忽略其業(yè)務(wù)功能,格式設(shè)定為無參無返回值的方法,方法體內(nèi)空實現(xiàn)(非抽象)
3.@Before
位置:方法定義上方
作用:標(biāo)注當(dāng)前方法作為前置通知
格式:
@Before("pt()")
public void before(){
}無特殊參數(shù)
4.@After
位置:方法定義上方
作用:標(biāo)注當(dāng)前方法作為后置通知
格式:
@After("pt()")
public void after(){
}無特殊參數(shù)
5.@AfterReturning
位置:方法定義上方
作用:標(biāo)注當(dāng)前方法作為返回后通知
格式:
@AfterReturning(value="pt()",returning = "ret")
public void afterReturning(Object ret) {
}特殊參數(shù):
returning :設(shè)定使用通知方法參數(shù)接收返回值的變量名
6.@AfterThrowing
位置:方法定義上方
作用:標(biāo)注當(dāng)前方法作為異常后通知
格式:
@AfterThrowing(value="pt()",throwing = "t")
public void afterThrowing(Throwable t){
}特殊參數(shù):
throwing :設(shè)定使用通知方法參數(shù)接收原始方法中拋出的異常對象名
7.@Around
位置:方法定義上方
作用:標(biāo)注當(dāng)前方法作為環(huán)繞通知
格式:
@Around("pt()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object ret = pjp.proceed();
return ret;
}無特殊參數(shù)
到此這篇關(guān)于在2023idea中如何實現(xiàn)SpringBoot的IoC和AOP的文章就介紹到這了,更多相關(guān)idea中實現(xiàn)SpringBoot的IoC和AOP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
企業(yè)級Kubernetes管理平臺Wayne功能特性介紹
這篇文章主要為大家介紹了企業(yè)級Kubernetes管理平臺Wayne的功能特性及架構(gòu)設(shè)計,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02
Springboot?引入?Redis?并配置序列化并封裝RedisTemplate?
這篇文章主要介紹了Springboot?引入?Redis?并配置序列化并封裝RedisTemplate。文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
ActiveMQ消息隊列技術(shù)融合Spring過程解析
這篇文章主要介紹了ActiveMQ消息隊列技術(shù)融合Spring過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題
這篇文章主要介紹了解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題,項目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12

