当前位置: 首页 > news >正文

小网站 收入网站建没有前景

小网站 收入,网站建没有前景,定制网站设计公司,iis wordpress 兼容Springboot如何使用面向切面编程AOP? 在 Spring Boot 中使用面向切面编程#xff08;AOP#xff09;非常简单#xff0c;Spring Boot 提供了对 AOP 的自动配置支持。以下是详细的步骤和示例#xff0c;帮助你快速上手 Spring Boot 中的 AOP。 1. 添加依赖 首先#xff…Springboot如何使用面向切面编程AOP? 在 Spring Boot 中使用面向切面编程AOP非常简单Spring Boot 提供了对 AOP 的自动配置支持。以下是详细的步骤和示例帮助你快速上手 Spring Boot 中的 AOP。 1. 添加依赖 首先在 pom.xmlMaven或 build.gradle.ktsGradle中添加 Spring Boot Starter AOP 依赖 Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId /dependencyGradle-Kotlin build.gradle.kts dependencies {implementation(org.springframework.boot:spring-boot-starter-aop) }implementation(org.springframework.boot:spring-boot-starter-aop)Gradle-Groovy build.gradle dependencies {implementation org.springframework.boot:spring-boot-starter-aop }implementation org.springframework.boot:spring-boot-starter-aop2. 编写切面类 切面类是一个普通的 Spring Bean使用 Aspect 注解标记。切面类中定义了切点Pointcut和通知Advice。 示例记录方法执行日志的切面 import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;Aspect // 标记为切面类 Component // 标记为 Spring Bean public class LoggingAspect {// 定义切点拦截 com.example.service 包下的所有方法Pointcut(execution(* com.example.service.*.*(..)))public void serviceMethods() {}// 前置通知在目标方法执行前执行Before(serviceMethods())public void logBeforeMethod() {System.out.println(方法即将执行...);} }3. 定义目标服务类 编写一个普通的 Spring 服务类作为 AOP 的目标对象。 示例用户服务类 import org.springframework.stereotype.Service;Service public class UserService {public void createUser(String name) {System.out.println(创建用户: name);}public void deleteUser(String name) {System.out.println(删除用户: name);} }4. 启用 AOP 支持 Spring Boot 默认会自动启用 AOP 支持无需额外配置。如果需要手动启用可以在主应用类上添加 EnableAspectJAutoProxy 注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy;SpringBootApplication EnableAspectJAutoProxy // 启用 AOP 支持 public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);} }5. 运行并验证 启动 Spring Boot 应用调用 UserService 的方法观察切面是否生效。 示例调用服务方法 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;Component public class AppRunner implements CommandLineRunner {Autowiredprivate UserService userService;Overridepublic void run(String... args) throws Exception {userService.createUser(Alice);userService.deleteUser(Bob);} }输出结果 方法即将执行... 创建用户: Alice 方法即将执行... 删除用户: Bob6. 常用 AOP 注解 Spring AOP 提供了多种通知类型以下是常用的注解 注解说明Before在目标方法执行前执行。After在目标方法执行后执行无论是否抛出异常。AfterReturning在目标方法成功返回后执行。AfterThrowing在目标方法抛出异常后执行。Around环绕通知可以控制目标方法的执行如修改参数、返回值或捕获异常。 示例环绕通知 import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component;Aspect Component public class TimingAspect {Around(execution(* com.example.service.*.*(..)))public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {long startTime System.currentTimeMillis();Object result joinPoint.proceed(); // 执行目标方法long endTime System.currentTimeMillis();System.out.println(方法执行时间: (endTime - startTime) ms);return result;} }7. 切点表达式 切点表达式用于定义拦截的目标方法。以下是常见的表达式示例 表达式说明execution(* com.example.service.*.*(..))拦截 com.example.service 包下的所有方法。execution(* com.example.service.UserService.*(..))拦截 UserService 类中的所有方法。execution(* com.example.service.*.create*(..))拦截 com.example.service 包下以 create 开头的方法。annotation(com.example.LogExecutionTime)拦截带有 LogExecutionTime 注解的方法。 自定义注解示例 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface LogExecutionTime {}在方法上使用注解 Service public class UserService {LogExecutionTimepublic void createUser(String name) {System.out.println(创建用户: name);} }在切面中拦截注解 Aspect Component public class LogExecutionTimeAspect {Around(annotation(com.example.LogExecutionTime))public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {long startTime System.currentTimeMillis();Object result joinPoint.proceed();long endTime System.currentTimeMillis();System.out.println(方法执行时间: (endTime - startTime) ms);return result;} }8. 总结 Spring Boot 通过 spring-boot-starter-aop 提供了对 AOP 的自动支持。使用 Aspect 定义切面类结合 Before、After、Around 等注解实现通知。切点表达式execution用于定义拦截的目标方法。可以通过自定义注解实现更灵活的切面逻辑。 通过以上步骤你可以在 Spring Boot 中轻松实现面向切面编程AOP增强代码的可维护性和可扩展性。 在Spring Boot中使用面向切面编程AOP通常涉及以下几个步骤 1. 引入依赖 虽然Spring Boot的spring-boot-starter已经包含了AOP的依赖但为了确保AOP功能被正确启用你可以在pom.xml中显式添加spring-boot-starter-aop依赖尽管这通常是可选的因为spring-boot-starter-web等常用starter已经包含了它。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId /dependency2. 启用AOP 在Spring Boot中你通常不需要显式启用AOP因为SpringBootApplication注解已经包含了EnableAspectJAutoProxy后者负责启用AOP代理。但是如果你想要自定义AOP代理的行为例如使用CGLIB而不是JDK动态代理你可以通过添加EnableAspectJAutoProxy注解并设置其属性来实现。 SpringBootApplication EnableAspectJAutoProxy(proxyTargetClass true) // 使用CGLIB代理 public class MySpringBootApplication {public static void main(String[] args) {SpringApplication.run(MySpringBootApplication.class, args);} }然而在大多数情况下默认设置就足够了。 3. 定义切面类 切面类是一个用Aspect注解标记的类它包含了切点pointcut和通知advice。 切点定义了哪些方法将被拦截。通知定义了拦截到方法时要执行的操作。 Aspect Component public class MyAspect {// 定义一个切点匹配所有com.example.service包下的所有方法Pointcut(execution(* com.example.service..*(..)))public void myPointcut() {// 这是一个空方法仅用于定义切点表达式}// 在方法执行之前执行Before(myPointcut())public void beforeAdvice(JoinPoint joinPoint) {System.out.println(Before method: joinPoint.getSignature());}// 在方法执行之后执行无论是否抛出异常After(myPointcut())public void afterAdvice(JoinPoint joinPoint) {System.out.println(After method: joinPoint.getSignature());}// 在方法执行之后执行仅当方法正常返回时AfterReturning(pointcut myPointcut(), returning result)public void afterReturningAdvice(JoinPoint joinPoint, Object result) {System.out.println(After returning method: joinPoint.getSignature() with result: result);}// 在方法抛出异常时执行AfterThrowing(pointcut myPointcut(), throwing exception)public void afterThrowingAdvice(JoinPoint joinPoint, Throwable exception) {System.out.println(After throwing method: joinPoint.getSignature() with exception: exception);}// 环绕通知可以在方法执行前后自定义逻辑Around(myPointcut())public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println(Before proceeding method: joinPoint.getSignature());Object result joinPoint.proceed(); // 执行目标方法System.out.println(After proceeding method: joinPoint.getSignature() with result: result);return result;} }4. 应用切面 一旦你定义了切面类并将其作为Spring组件通过Component注解注册到Spring容器中Spring AOP将自动应用这些切面到匹配的bean上。 注意事项 切面类本身必须是一个Spring管理的bean因此通常会在切面类上使用Component或其他相关的Spring注解。切点表达式定义了哪些方法将被拦截。你可以使用AspectJ的切点表达式语言Pointcut Expression Language, PEL来编写复杂的切点表达式。通知方法可以有参数这些参数可以是JoinPoint、ProceedingJoinPoint对于环绕通知或任何由切点表达式捕获的参数通过Args注解。环绕通知必须处理目标方法的调用通过ProceedingJoinPoint.proceed()并且可以返回结果或抛出异常。 通过以上步骤你就可以在Spring Boot项目中成功使用AOP来实现横切关注点如日志记录、事务管理、权限校验等的分离。 在 Spring Boot 中使用面向切面编程AOP可以帮助你分离横切关注点如日志记录、事务管理等从主要业务逻辑中分离出来。Spring AOP 是基于代理的实现默认情况下支持方法级别的拦截。下面是如何在 Spring Boot 应用中配置和使用 AOP 的步骤 1. 添加依赖 首先确保你的 build.gradle 或 pom.xml 文件中包含了 spring-boot-starter-aop 依赖。 对于 Gradle添加如下依赖到你的 build.gradle 文件 dependencies {implementation org.springframework.boot:spring-boot-starter-aop }对于 Maven在你的 pom.xml 文件中添加 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId /dependency2. 启用 AOP 支持 Spring Boot 自动配置了 AOP 支持所以通常不需要额外的手动配置。但是如果你的应用有多个配置类确保它们被正确扫描到。一般情况下只需保证主应用程序类或配置类上包含 EnableAspectJAutoProxy 注解尽管大多数情况下自动配置已经足够。 import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;Configuration EnableAspectJAutoProxy public class AopConfig { }不过如果你只是简单地使用 Spring Boot并且没有特别复杂的配置需求这个注解通常是不必要的因为 Spring Boot 已经为你自动启用了 AOP 支持。 3. 创建一个切面 创建一个类并使用 Aspect 注解标记它。然后定义切入点Pointcut和通知Advice例如前置通知Before、后置通知After、返回通知AfterReturning、异常通知AfterThrowing和环绕通知Around。 示例代码 import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;Aspect Component public class LoggingAspect {Before(execution(* com.example.demo.service.*.*(..)))public void beforeMethodExecution() {System.out.println(A method in the service layer is about to be called.);} }4. 定义切入点表达式 上述例子中的 execution(* com.example.demo.service.*.*(..)) 是一个切入点表达式表示匹配 com.example.demo.service 包下的所有类的所有方法。你可以根据需要调整此表达式来精确控制哪些方法会被拦截。 5. 测试你的切面 最后编写一些测试用例或者运行你的应用来验证 AOP 切面是否按预期工作。确保目标方法被调用时相应的通知也会被执行。 通过以上步骤你应该能够在 Spring Boot 应用中成功配置并使用 AOP。这种方式不仅能够帮助你清晰地分离关注点还能使代码更加简洁和易于维护。 在 Spring Boot 中使用面向切面编程AOP可以高效地实现日志记录、事务管理、权限校验等横切关注点。以下是 详细步骤和实际应用场景 的总结 1. 添加依赖 Spring Boot 默认通过 spring-boot-starter-aop 提供对 AOP 的自动配置支持 Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId /dependencyGradle dependencies {implementation(org.springframework.boot:spring-boot-starter-aop) }2. 编写切面类 切面类需用 Aspect 和 Component 注解标记定义切点和通知。 示例日志记录切面 import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;Aspect Component public class LoggingAspect {// 定义切点拦截 service 包下的所有方法Pointcut(execution(* com.example.service.*.*(..)))public void serviceMethods() {}// 前置通知方法执行前记录日志Before(serviceMethods())public void logMethodStart() {System.out.println(方法开始执行...);}// 后置通知方法执行后记录日志无论是否异常After(serviceMethods())public void logMethodEnd() {System.out.println(方法执行结束。);}// 环绕通知计算方法执行时间Around(serviceMethods())public Object measureTime(ProceedingJoinPoint joinPoint) throws Throwable {long start System.currentTimeMillis();Object result joinPoint.proceed(); // 执行目标方法long end System.currentTimeMillis();System.out.println(方法执行耗时: (end - start) ms);return result;} }3. 定义目标服务 编写一个普通的 Spring Bean 作为切面拦截的目标。 示例用户服务 Service public class UserService {public void createUser(String name) {System.out.println(创建用户: name);} }4. 验证效果 调用 UserService 的方法时切面逻辑自动生效 SpringBootApplication public class Application {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(Application.class, args);UserService userService context.getBean(UserService.class);userService.createUser(Alice);} }输出结果 方法开始执行... 创建用户: Alice 方法执行结束。 方法执行耗时: 2ms5. 核心注解详解 (1) 切点表达式Pointcut 语法execution(修饰符 返回类型 包名.类名.方法名(参数类型))常用示例 execution(* com.example.service.*.*(..))拦截 service 包下所有类的所有方法。execution(* com.example.service.UserService.create*(..))拦截 UserService 中以 create 开头的方法。annotation(com.example.LogTrack)拦截带有 LogTrack 注解的方法。 (2) 通知类型Advice 注解说明Before目标方法执行前触发。After目标方法执行后触发无论是否抛出异常。AfterReturning目标方法成功返回后触发可获取返回值。AfterThrowing目标方法抛出异常后触发可捕获异常对象。Around包裹目标方法可控制方法执行、修改参数或返回值。 示例获取方法参数和返回值 Aspect Component public class DataAspect {AfterReturning(pointcut execution(* com.example.service.*.*(..)),returning result)public void logResult(Object result) {System.out.println(方法返回值: result);}Before(execution(* com.example.service.*.*(..)) args(name))public void logArgument(String name) {System.out.println(方法参数: name);} }6. 高级用法 (1) 自定义注解实现切面 定义注解标记需要拦截的方法提高代码可读性。 步骤1定义注解 Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface LogTrack {}步骤2在方法上使用注解 Service public class OrderService {LogTrackpublic void placeOrder(String orderId) {System.out.println(下单成功: orderId);} }步骤3切面拦截注解 Aspect Component public class LogTrackAspect {Around(annotation(com.example.LogTrack))public Object trackMethod(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println(开始追踪方法...);Object result joinPoint.proceed();System.out.println(追踪结束。);return result;} }(2) 处理异常 在 AfterThrowing 中捕获并处理异常。 Aspect Component public class ExceptionAspect {AfterThrowing(pointcut execution(* com.example.service.*.*(..)),throwing ex)public void handleException(Exception ex) {System.out.println(捕获异常: ex.getMessage());// 发送报警邮件或记录日志} }7. 常见问题与解决 问题1切面未生效 原因 切面类未被 Spring 管理缺少 Component。切点表达式未匹配到目标方法。目标方法未被 Spring 代理如直接通过 new 创建对象。 解决 确保切面类添加了 Component。使用 Autowired 获取 Bean而非直接实例化。 问题2环绕通知未调用 proceed() 现象目标方法未执行。解决在 Around 方法中必须调用 joinPoint.proceed()。 8. 实际应用场景 日志记录自动记录方法入参、返回值、执行时间。事务管理结合 Transactional 实现声明式事务。权限校验在方法执行前检查用户权限。性能监控统计接口耗时优化慢查询。缓存管理在方法执行前后操作缓存如 Redis。 总结 Spring Boot 通过简化配置和自动代理机制使得 AOP 的实现非常便捷。核心步骤 添加 spring-boot-starter-aop 依赖。使用 Aspect 和 Component 定义切面类。通过切点表达式精准定位目标方法。选择合适的通知类型Before、Around 等实现横切逻辑。 掌握 AOP 后可以大幅减少重复代码提升系统的可维护性和扩展性。
http://www.dnsts.com.cn/news/22060.html

相关文章:

  • 写网站软件手机版网站建设
  • 晋城网站设计云浮哪有做网站公司
  • 免费网站制作视频教程苏州新闻最新消息今天
  • 网站建设与管理是干嘛的咨询邯郸网站建设
  • 太原模板建站系统教育类网站框架
  • 沅江网站设计公司五屏网站建设代理商
  • 自己的网站怎样做优化手机壳定制网站制作
  • 外贸建站网站推广企业网站排名软件度智能优化
  • 画画外包网站怎么建网站详细步骤
  • 网站结构优化的内容和方法企业网站的用户需求
  • 深圳网站建设 手机网站建设海外直邮购物网站
  • 网站优化关键词排名自己怎么做wordpress图片展示插件
  • 免费个人网站服务器 html一个网站的建设流程有哪些
  • 桂林企业建站找人做app网站吗
  • 广州有哪些做网站的公司wordpress搭建网盘
  • 响应式网站跟一般网站的区别做网站 怎么做留言
  • 高端+旅游+网站建设网站总浏览量
  • 环保企业的网站怎么做如何制作海报宣传图片
  • 网站排名查询alexa建筑用模板尺寸一般是多少
  • php网站开发参考文献建设工程人力资源官网
  • 国际购物网站数字创意设计包括哪些方面
  • 温州市网站优化公司网站简介怎么做
  • 温州专业网站推广企业年金是什么?缴费比例是多少?
  • 外贸网站定做做网站规划
  • 鞍山专业做网站公司wordpress仪表盘密码
  • 绍兴建设网站商城的网站设计
  • 做网站怎么发布养老网站建设
  • 优秀设计集锦网站免费的销售管理系统
  • 百度推广总部电话seo的作用
  • 休闲采摘园网站建设公司专业网页制作