Spring AOP使用范例,了解AOP

Spring AOP使用范例,了解AOP,第1张

直接上代码:

按照spring创建流程正常创建,再对照我的pom.xml表,最后缺少补啥就行

工程大致架构:

 

DemoApplication:
package com.fzf.spring.aop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
HelloController:
package com.fzf.spring.aop.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return "hello " + name;
    }
    @GetMapping("/hello1")
    public String hello1(@RequestParam("name") String name, @RequestParam("age") String age) {
        return "hello " + name +"  "+ "age " + age;
    }
}
MyAdvice:
package com.fzf.spring.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAdvice {

    private Logger logger=LoggerFactory.getLogger(MyAdvice.class);
    /**
     * 定义切面,
     * 不限包名+com.fzf.spring.aop.controller+不限类+不限函数+不限参数
     */
    @Pointcut(value = "execution(* com.fzf.spring.aop.controller.*.*(..))")
    public void myPointcut(){

    }

    /**
     * @Around("myPointcut")是应了 myPointcut()这个切点
     * @param pjp
     * @return
     */
    @Around("myPointcut()")
    public Object myLogger(ProceedingJoinPoint pjp) throws Throwable {
        /**
         * 获取类名
         */
        String className =pjp.getTarget().getClass().toString();
        /**
         * 获取方法名
         */
        String methodName =pjp.getSignature().getName();
        /**
         * 不知道是几个,以及类型
         */
        Object[] array=pjp.getArgs();

        ObjectMapper objectMapper=new ObjectMapper();
        logger.info("调用前:"+className+":"+methodName+"传递的参数为:"+objectMapper.writeValueAsString(array));
        /**
         * 调用整个目标函数去执行
         */
        Object obj = pjp.proceed();
        logger.info("调用后:"+className+":"+methodName+"返回值为:"+objectMapper.writeValueAsString(obj));

        return obj;
    }

}

pom.xml:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.6.7
		 
	
	com.fzf.spring.aop
	aopdemo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.boot
			spring-boot-starter-aop
			2.4.4
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				2.4.4
			
		
	


结果图:

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/736146.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存