spring学习笔记二 注解及AOP

简介: spring学习笔记二 注解及AOP注解:使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值。 扫描某个包下的所有类中的注解.复制代码<?xml version="1.0" encoding="UTF-8"?>xmlns:context="http://www.

spring学习笔记二 注解及AOP
注解:

使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值。 扫描某个包下的所有类中的注解.

复制代码
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans

                                       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context 
                                       http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop 
                                       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">


<bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>

<bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean>

<aop:config>
    <!-- 配置切入点 
        public void cn.itcast.service.UserServiceImpl.save() 这样配置太啰嗦
        void cn.itcast.service.UserServiceImpl.save() public可以省
        * cn.itcast.service.UserServiceImpl.save() 对返回值不做任何要求
        * cn.itcast.service.UserServiceImpl.*() 所有空参方法
        
        * cn.itcast.service.*ServiceImpl.*(..) 找service包下的所有ServiceImpl结尾的方法
        * cn.itcast.service..*ServiceImpl.*(..) 找Service包下(及其子孙包)的所有的ServiceImpl结尾的方法
    -->
    <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
    <aop:aspect ref="myAdvice" >
        <!-- 指定名为before方法作为前置通知 -->
        <aop:before method="before" pointcut-ref="pc" />
        <!-- 后置 -->
        <aop:after-returning method="afterReturning" pointcut-ref="pc" />
        <!-- 环绕通知 -->
        <aop:around method="around" pointcut-ref="pc" />
        <!-- 异常拦截通知 -->
        <aop:after-throwing method="afterException" pointcut-ref="pc"/>
        <!-- 后置 -->
        <aop:after method="after" pointcut-ref="pc"/>
    </aop:aspect>
</aop:config>


复制代码
复制代码
5、测试代码

复制代码
复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {

@Autowired
private UserService u;

@Test
public void fun1() {
  u.save();
}

}
复制代码
复制代码
使用注解演示AOP
配置文件:

复制代码
复制代码
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">


<bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>

<bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean>

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>


复制代码
复制代码
通知类

复制代码
复制代码
package cn.itcast.e_annotationaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {

@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void pc(){}//抽取相同的execution,方便管理。
//前置通知
//指定该方法是前置通知,并制定切入点
@Before("MyAdvice.pc()")
public void before(){
    System.out.println("这是前置通知!!");
}
//后置通知
@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void afterReturning(){
    System.out.println("这是后置通知(如果出现异常不会调用)!!");
}
//环绕通知
@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("这是环绕通知之前的部分!!");
    Object proceed = pjp.proceed();//调用目标方法
    System.out.println("这是环绕通知之后的部分!!");
    return proceed;
}
//异常通知
@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void afterException(){
    System.out.println("出事啦!出现异常了!!");
}
//后置通知
@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void after(){
    System.out.println("这是后置通知(出现异常也会调用)!!");
}

}
复制代码
原文地址link

相关文章
|
6月前
|
XML Java 数据格式
《深入理解Spring》:AOP面向切面编程深度解析
Spring AOP通过代理模式实现面向切面编程,将日志、事务等横切关注点与业务逻辑分离。支持注解、XML和编程式配置,提供五种通知类型及丰富切点表达式,助力构建高内聚、低耦合的可维护系统。
|
6月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
6月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
672 2
|
7月前
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
1073 128
|
7月前
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
484 12
|
7月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
371 0
探索Spring Boot的@Conditional注解的上下文配置
|
7月前
|
智能设计 Java 测试技术
Spring中最大化@Lazy注解,实现资源高效利用
本文深入探讨了 Spring 框架中的 `@Lazy` 注解,介绍了其在资源管理和性能优化中的作用。通过延迟初始化 Bean,`@Lazy` 可显著提升应用启动速度,合理利用系统资源,并增强对 Bean 生命周期的控制。文章还分析了 `@Lazy` 的工作机制、使用场景、最佳实践以及常见陷阱与解决方案,帮助开发者更高效地构建可扩展、高性能的 Spring 应用程序。
293 0
Spring中最大化@Lazy注解,实现资源高效利用
|
7月前
|
Java 测试技术 数据库
使用Spring的@Retryable注解进行自动重试
在现代软件开发中,容错性和弹性至关重要。Spring框架提供的`@Retryable`注解为处理瞬时故障提供了一种声明式、可配置的重试机制,使开发者能够以简洁的方式增强应用的自我恢复能力。本文深入解析了`@Retryable`的使用方法及其参数配置,并结合`@Recover`实现失败回退策略,帮助构建更健壮、可靠的应用程序。
842 1
使用Spring的@Retryable注解进行自动重试
|
7月前
|
Java 测试技术 编译器
@GrpcService使用注解在 Spring Boot 中开始使用 gRPC
本文介绍了如何在Spring Boot应用中集成gRPC框架,使用`@GrpcService`注解实现高效、可扩展的服务间通信。内容涵盖gRPC与Protocol Buffers的原理、环境配置、服务定义与实现、测试方法等,帮助开发者快速构建高性能的微服务系统。
1408 0
|
7月前
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
702 0

热门文章

最新文章