和 XML 配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。
本质上:所有一切的操作都是 Java 代码来完成的,XML 和注解只是告诉框架中的 Java 代码如何执行。

新建一个maven项目

Spring 为了知道程序员在哪些地方标记了什么注解,就需要通过扫描的方式,来进行检测。然后根据注解进行后续操作。

pom.xml

<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>spring-aop</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-aop</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<!--这里是设置作用域-->
<!--注释作用域,表明我想让它为全局使用-->
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
</project>

注解开发的话当然需要配置类

@Configuration //这个类就是配置类相当于配置文件

@ComponentScan

用于类或接口上主要是指定扫描路径,spring会把指定路径下带有指定注解的类自动装配到bean容器里。会被自动装配的注解包括@Controller、@Service、@Component、@Repository等等。与ComponentScan注解相对应的XML配置就是<context:component-scan/>, 根据指定的配置自动扫描package,将符合条件的组件加入到IOC容器中;

@EnableAspectJAutoProxy

package lps.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; /**
* @author 阿水
* @create 2023-02-23 14:52
*/
@Configuration
@ComponentScan({"lps"})
@EnableAspectJAutoProxy
public class SpringConfig {
}

①使用@Component注解标记的普通组

②使用@Controller注解标记的控制器组件

③使用@Service注解标记的业务逻辑组件

④使用@Repository注解标记的持久化层组件

通过查看源码我们得知,@Controller、@Service、@Repository这三个注解只是在@Component注解的基础上起了三个新的名字。
对于Spring使用IOC容器管理这些组件来说没有区别,也就是语法层面没有区别。所以@Controller、@Service、@Repository这三个注解只是给开发人员看的,让我们能够便于分辨组件的作用。
注意:虽然它们本质上一样,但是为了代码的可读性,为了程序结构严谨我们肯定不能随便胡乱标记。

@Autowired和@Resource区别

提供方不同

​ @Autowired 是Spring提供的,@Resource 是J2EE提供的。

装配时默认类型不同

​ @Autowired只按type装配,@Resource默认是按name装配。

切入点表达式的写法-execution函数

关键字:execution(表达式)

表达式:访问修饰符 返回值 包名.包名.包名...类名.方法名

规则 表达式 示例
匹配所有类的public方法 execution(public **(..))  
匹配指定包下所有类的方法(不包含子包) execution(* 包名.*(..)) execution(* test.aop.t1.*(..))
匹配指定包下所有类的方法(包含子包) execution(* 包名.**(..)) execution(* test.aop.t1..*(..))
匹配指定类下的所有方法 execution(* 包名.类名.*(..)) execution(* test.aop.t1.UserDao.*(..))
匹配指定类下的指定方法 execution(* 包名.类名.方法名(..)) execution(* test.aop.t1.UserDao.add(..))
匹配实现指定接口的所有类的方法 execution(* 包名.接口名+*(..)) execution(* test.aop.t1.UserDao+*(..))
匹配所有名称以save开头的方法 execution(* save*(..))  

Spring中的通知

Spring中一共有5种通知

前置通知(before)、后置通知(returning)、异常通知(throwing)、最终通知(after)、环绕通知(around)
复制代码
通知类型 作用 应用场景
前置通知 在目标方法执行前增强代码逻辑 权限控制(权限不足抛出异常)、记录方法调用信息日志
后置通知 在目标方法运行后返回值后再增强代码逻辑,在出现异常时不执行 与业务相关的,如银行在存取款结束后的发送短信消息 */
异常通知 目标代码出现异常,通知执行。记录异常日志、通知管理员(短信、邮件) 处理异常(一般不可预知),记录日志
最终通知 不管目标方法是否发生异常,最终通知都会执行(类似于finally代码功能) 释放资源 (关闭文件、 关闭数据库连接、 网络连接、 释放内存对象 )
环绕通知 目标方法执行前后,都进行增强(控制目标方法执行) 日志、缓存、权限、性能监控、事务管理

UserService.java
package lps.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; /**
* @author 阿水
* @create 2023-02-23 14:50
*/
@Service("userService")
public class UserService {
/* public void sing(){
System.out.println("我爱唱歌");
}
public void dance(){
System.out.println("我爱跳舞");
}
public void rap() {
//int i=10/0;
System.out.println("我爱rap 篮球 起~");
}*/
public void doSum(){
long sum=0;
for (long i = 0; i < 1000000000; i++) {
sum+=i;
}
System.out.println(sum);
}
}

Aspect代码块

package lps.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; import java.util.Date; /**
* @author 阿水
* @create 2023-02-23 15:18
*/
@Component
@Aspect
public class LoggerAspect {
//@Pointcut("execution(public * lps.service.*.*(..))")
@Pointcut("execution(* lps.*.*.*(..))")
private void pointcut() {
} /* @AfterReturning("pointcut()")
public void after() {
System.out.println("AfterReturning————————————————————你的鸡哥————————————————————");
} @Before("pointcut()")
public void before() {
System.out.println("Before________________________你的坤坤________________________");
}
*//* @Around("pointcut()")
public void around(){
System.out.println("_+++++环绕的鸡哥_+++++");
}*//*
@AfterThrowing("pointcut()")
public void logAfterThrowing(){
System.out.println("AfterThrowing出错啦 赶紧回来");
}
@After("pointcut()")
public void afterFinalyy() {
System.out.println("After最终通知 ");
}*/
@Around("pointcut()")
public Object test(ProceedingJoinPoint point){
Object result=null;
//long beginTime = System.currentTimeMillis();
long beginTime = new Date().getTime();
    
Object[] args = point.getArgs(); try {
result = point.proceed(args);
} catch (Throwable e) {
throw new RuntimeException(e);
} //long endTime = System.currentTimeMillis();
long endTime = new Date().getTime();
System.out.println("一起用了"+(endTime-beginTime)+"毫秒");
return result; } }

测试类

package lps.demo;

import javafx.application.Application;
import lps.config.SpringConfig;
import lps.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author 阿水
* @create 2023-02-23 14:53
*/
public class test1 {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService) context.getBean("userService");
/* userService.sing();
userService.dance();
userService.rap();*/
userService.doSum(); }
}

SpringIOC以及AOP注解开发的更多相关文章

  1. Spring (二)SpringIoC和DI注解开发

    1.Spring配置数据源 1.1 数据源(连接池)的作用 数据源(连接池)是提高程序性能出现的 事先实例化数据源,初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 ...

  2. Spring笔记04_AOP注解开发_模板_事务

    目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...

  3. Spring入门(三)— AOP注解、jdbc模板、事务

    一.AOP注解开发 导入jar包 aop联盟包. aspectJ实现包 . spring-aop-xxx.jar . spring-aspect-xxx.jar 导入约束 aop约束 托管扩展类和被扩 ...

  4. Spring---AOP注解开发&jdbc模板&Spring事务管理

    一.AOP注解开发 此处需要回忆一遍AOP的概念.简单的来说,AOP就是利用动态代理技术,做到不触动源代码但却扩展了功能.那么就需要一个被扩展的对象和一个“新的功能”,例如说给某类的saveUser方 ...

  5. springAop:Aop(Xml)配置,Aop注解配置,spring_Aop综合案例,Aop底层原理分析

    知识点梳理 课堂讲义 0)回顾Spring体系结构 Spring的两个核心:IoC和AOP 1)AOP简介 1.1)OOP开发思路 OOP规定程序开发以类为模型,一切围绕对象进行,OOP中完成某个任务 ...

  6. springIOC、AOP的一些注解

    springIOC.AOP的一些注解(使用这些注解之前要导入spring框架的一些依赖):    1.注入IOC容器        @Compontent:使用注解的方式添加到ioc容器需要在配置文件 ...

  7. Spring基于AspectJ的AOP的开发——注解

    源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...

  8. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  9. Spring_IoC注解开发和AOP的XML开发(学习笔记2)

    一:IoC注解开发 1,在applicationContext.xml中需要引入context约束 <beans xmlns="http://www.springframework.o ...

  10. AOP注解方式ApsectJ开发

    AOP注解方式ApsectJ开发 引入配置文件 编写切面类配置 使用注解的AOP对象目标类进行增强 在配置文件中开启以注解形式进行AOP开发 在切面类上添加注解 注解AOP通知类型 @Before前置 ...

随机推荐

  1. QT4.8.6移植

    sudo apt-get install libx11-dev libxext-dev libxtst-dev 配置: ./configure --prefix=/opt/qt4.8.6 -opens ...

  2. Redux Toolkit——基操

    redux-toolkit是redux的升级版 安装 npm install @reduxjs/toolkit // 在react中还需要搭配react-redux使用 npm install rea ...

  3. NSQ(7)-nsq存在的问题

    nsq存在的缺陷 部署的难度 ​ nsq提供了一种消费者端进行服务发现的模型,所以无需告诉消费者去哪寻找对于的主题(Topic)在哪个nsqd实例上. ​ 然而,它并没有提供一种方案去解决一个生产者应 ...

  4. 一、MySQL 函数

    1.MySQL 字符串函数 函数 描述 实例 结果展示 说明 REPLACE(s,s1,s2) 将字符串s2代替字符串s中的字符串s1 SELECT REPLACE(ccc.contract_no,& ...

  5. zabbix中文显示乱码解决

    问题zabbix使用中文显示,"监测-->图形"查看资源使用情况时会有乱码 解决问题1.修改配置文件(文件位置:$zabbix_path/include/defines.in ...

  6. j2ee目录索引

    j2ee  不完全目录 Eclipse中配置约束(DTD,XSD) Eclipse 快捷键 及其 xsd dtd配置本地约束 ===================================== ...

  7. eccodes 使用girb_filter工具

    参考自ECMWF网站https://confluence.ecmwf.int/display/OPTR/ecCodes%3A+GRIB+and+BUFR+data+decoding+and+encod ...

  8. 关于git基本操作备忘

    1.将远程分支拉取到本地分支 git pull origin 分支名 2.将本地分支代码提交到远程分支 git push origin HEAD:Ft_6.8

  9. Windows11 微软提供三种路径 安装程序应用

    Windows11 微软提供三种路径 安装程序应用

  10. Vulnhub 靶场 DARKHOLE: 2

    Vulnhub 靶场 DARKHOLE: 2 前期准备 前期准备: 靶机地址:https://www.vulnhub.com/entry/darkhole-2,740/ kali攻击机ip:192.1 ...