首页 > 编程语言 >spring中的aop(面向切面编程)需要到导入的包与简单示例

spring中的aop(面向切面编程)需要到导入的包与简单示例

时间:2023-09-07 17:24:00浏览次数:46  
标签:示例 spring void hh aop org import com public

2023-09-07

<?xml version="1.0" encoding="UTF-8"?>
<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>com.hh</groupId>
    <artifactId>SpringAOP</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>

    </dependencies>

</project>

BookDao
package com.hh.dao;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-07 15:29:13
 */
public interface BookDao {

    public void save();
    public void update();
}

BookDaoImpl

 

package com.hh.dao.impl;

import com.hh.dao.BookDao;
import org.springframework.stereotype.Repository;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-07 15:29:53
 */
@Repository
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println(System.currentTimeMillis());
        System.out.println("book dao save...");
    }

    @Override
    public void update() {
        System.out.println("book dao update...");
    }
}

MyAdvice

package com.hh.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-07 15:39:41
 */
@Component
@Aspect
public class MyAdvice {

    @Pointcut("execution(void com.hh.dao.BookDao.update())")
    private void pt(){}

    @Before("pt()")
    public void method(){
        System.out.println(System.currentTimeMillis());
    }
}

SpringConfig

package com.hh.comfig;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-07 15:31:16
 */
@Configuration
@ComponentScan({"com.hh"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringConfig {
}

 

标签:示例,spring,void,hh,aop,org,import,com,public
From: https://www.cnblogs.com/isDaHua/p/17685528.html

相关文章

  • springsecurity
    编辑 pom.xml,添加 spring-boot-starter-securtiy 依赖即可。添加后项目中所有的资源都会被保护起来。<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-security</artifactId></dependency>编写SecurityConfig......
  • springboot随项目启动,实时监控日志文件并进行操作
    项目中用到了一个开源音视频服务,但是同事的服务有可能导致开源服务崩溃,所以就写了一个实时监控开源服务输出日志的服务,如果日志中有error信息的话就自动重启那个开源服务。不过后来还是在项目中把这部分屏蔽了。 1@Component2publicclassFileWatcherRunnerimplem......
  • Android 调试桥 (adb) 使用教程/示例
    sidebar:autoAndroid调试桥(adb)Android调试桥(adb)是一种功能多样的命令行工具,可让您与设备进行通信。adb命令可用于执行各种设备操作,例如安装和调试应用。adb提供对Unixshell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:客......
  • Spring注解开发
    在Spring4之后,要使用注解开发,必须保证aop的依赖包导入。这里我们在maven的pom.xml中导入spring-webmvc这个大的依赖整合包就可以了。<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>6.0.11</version></d......
  • spring整合junit中使用到的依赖以及简单的测试案例演示
    2023-09-07注意:spring整合junit中的依赖要和spring-context的依赖版本相一致<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>......
  • pagehelper分页框架进行定时跑批分页 在插入与sql语句的编写,当插入有库中有报异常显示
    以下是一个示例的业务类,使用PageHelper分页框架进行定时跑批分页插入操作,并在遇到重复数据时跳过继续插入:importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importorg.springframework.beans.factory.annotation.Autowired;importorg.spring......
  • springboot 服务端接口公网远程调试﹣实现 HTTP 服务监听【端口映射】
    前言前后端分离项目中,在调用接口调试时候,我们可以通过cpolar内网穿透将本地服务端接口模拟公共网络环境远程调用调试,本次教程我们以Java服务端接口为例。1.本地环境搭建1.1环境参数JDK1.8IDEASpringBootMavenTomcat9.0Postman1.2搭建springboot服务项目搭建一个springboot服务的......
  • Spring 参数校验注解失效
    问题描述使用@Notnull、@Max、@Min等参数校验注解时失效。解决在Controller层请求参数前加入@Valid注解//查询用Get@GetMapping("/query-list")//@Valid让req中的验证注解生效publicCommonResp<List<PassengerQueryResp>>queryList(@ValidPasse......
  • Spring—Document root element "beans", must match DOCTYPE root "null"分析及解决
    Documentrootelement"beans",mustmatchDOCTYPEroot"null".的错误提示,网上很多人说要把applicationContex.xml文件中加上如下第二行的<!DOCTYPE/>标签,说明DTD,其实并不准确。<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBL......
  • springboot打包运行失败
    没有包含启动类的class添加mavenpom.xml;`org.springframework.bootspring-boot-maven-plugin${spring-boot.version}com.hidisp.HidispApplication<!--issue:SpringBoot-0.0.1-SNAPSHOT.jar中没有主清单属性 resolve:①注释掉,或者②将skip值改为......