zoukankan      html  css  js  c++  java
  • SpringBoot AOP

    <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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
            <relativePath />
        </parent>
        <groupId>com.study</groupId>
        <artifactId>SpringBootTest-1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>RequestBodyTest</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    package com.aop.test;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
    
        SpringApplication.run(Application.class, args);
        }
    
    }
    package com.aop.test;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    /**
     * 在系统运行时动态添加代码的方式称为面向切面编程(AOP)
     *
     */
    @Component
    @Aspect
    public class LogAspect {
        @Pointcut("execution(* com.aop.test.service.*.*(..))")
        public void pc1() {
        }
        @Before(value = "pc1()")
        public void before(JoinPoint jp) {
            String name = jp.getSignature().getName();
            System.out.println(name + "方法开始执行...");
        }
        @After(value = "pc1()")
        public void after(JoinPoint jp) {
            String name = jp.getSignature().getName();
            System.out.println(name + "方法执行结束...");
        }
        @AfterReturning(value = "pc1()", returning = "result")
        public void afterReturning(JoinPoint jp, Object result) {
            String name = jp.getSignature().getName();
            System.out.println(name + "方法返回值为:" + result);
        }
        @AfterThrowing(value = "pc1()",throwing = "e")
        public void afterThrowing(JoinPoint jp,Exception e) {
            String name = jp.getSignature().getName();
            System.out.println(name+"方法抛异常了,异常是:"+e.getMessage());
        }
        @Around("pc1()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            return pjp.proceed();
        }
    }
    package com.aop.test;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.aop.test.service.UserService;
    
    @RestController
    public class UserController {
    
        @Autowired
        UserService userService;
        
        
        @GetMapping("/getUserById")
        public String getUserById(Integer id) {
        return userService.getUserById(id);
        }
        
        @GetMapping("/deleteUserById")
        public void deleteUserById(Integer id) {
        userService.delteUserById(id);
        }
        
    }


    package com.aop.test.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
        public String getUserById(Integer id) {
        System.out.println("Get user by id...");
        return "user";
        }
        
        public void delteUserById(Integer id) {
        System.out.println("Delete user by id ...");
        }
    }

  • 相关阅读:
    大富翁8分析
    DCOM实现分布式应用(三)
    DCOM实现分布式应用(一)
    [转]职业生涯中的10个致命错误
    VC中展开宏
    zoj 2853 Evolution
    poj 1088 滑雪
    hdu 2437 Jerboas
    poj 3070 Fibonacci
    zoj 2976 Light Bulbs
  • 原文地址:https://www.cnblogs.com/luffystory/p/12024432.html
Copyright © 2011-2022 走看看