1. 程式人生 > >springboot的aop編程

springboot的aop編程

group sources tid factor set tap 設計思路 exc figure

以下內容是模仿楊開振<<深入淺出springboot 2.x>>的4.2章節內容。

開始前,需要先修改pom.xml,加入以下內容

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> 
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <
version>1.9.1</version> </dependency>

後面是需要逐一天增加或者修改的文件內容

  1. Note.java
  2. NoteService.java
  3. NoteServiceImpl.java
  4. NoteAspect.java
  5. NoteController.java
  6. App.java

Note.java

package study.spring.iocaop;

public class Note {
    private String logDay;
    private String keyWords;
    private
String contents; private String title; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getLogDay() { return logDay; } public void setLogDay(String logDay) { this.logDay = logDay; }
public String getKeyWords() { return keyWords; } public void setKeyWords(String keyWords) { this.keyWords = keyWords; } public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } }

NoteService.java

package study.spring.iocaop;

public interface NoteService {
     public void add(Note note);
     public void print(Note note) throws Exception;
}

NoteServiceImpl.java

package study.spring.iocaop;

import org.springframework.stereotype.Component;

@Component
public class NoteServiceImpl implements NoteService {

    @Override
    public void add(Note note) {
        System.out.println(note.getTitle());
    }

    @Override
    public void print(Note note) throws Exception {
        System.out.println("Title:"+note.getTitle());
        System.out.println("day:"+note.getLogDay());
        System.out.println("keyword:"+note.getKeyWords());
        System.out.println("content:"+note.getContents());
        throw new Exception("異常測試");
    }

}

NoteAspect.java

package study.spring.iocaop;

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

@Aspect
public class NoteAspect {

    @Pointcut("execution(* study.spring.iocaop.NoteServiceImpl.print(..))")
    public void pointCut(){
        
    }
    
    /**
     * 在切入函數中獲取方法的參數
     * @param point
     * @param note
     */
    @Before("pointCut() && args(note)")
    public void before(JoinPoint point,Note note){
        for(Object obj:point.getArgs()){
            
             System.out.println("aop:"+obj.getClass().getName());
             System.out.println("aop-target:"+point.getTarget().getClass().getName());
             System.out.println(point.getThis().toString());
        }
    }
    
    @After("pointCut()")
    public void after(){
        System.out.println("aop:after note");
    }
    
    @AfterReturning("pointCut()")
    public void afterReturning(){
        System.out.println("aop:afterReturning  note");
    }
    
    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        System.out.println("aop:afterThrowing note");
    }
}

NoteController.java

package study.spring.iocaop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NoteController {
    @Autowired
    NoteService  noteService;
    
    @RequestMapping("/note/print")
    @ResponseBody
    public Note printNote() throws Exception{
        Note note=new Note();
        note.setTitle("在上海奮鬥!");
        note.setLogDay("2023-12-31");
        note.setKeyWords("努力,科技,希望");
        note.setContents("奮鬥中......");
        noteService.print(note);
        return note;
    }
}

App.java

package study;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import study.config.Myfilter;
import study.spring.iocaop.NoteAspect;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@ServletComponentScan
public class App  extends SpringBootServletInitializer 
{
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder.sources(App.class);
        return builder;
    }
    //這個定理aop bean,否則無法產生切入效果
    @Bean(name="noteAspect")
    public NoteAspect initNoteAspect(){
        return new NoteAspect();
    }
    
    
    @Bean
    public FilterRegistrationBean<Myfilter> filterRegistrationBean() {
        FilterRegistrationBean<Myfilter> bean = new FilterRegistrationBean<>();
        bean.addUrlPatterns("/*");
        bean.setFilter(new Myfilter());
        return bean;
    }
}


aop編程,在某些方面挺好用,例如記錄日誌,或者是設計一些底層的框架。

從設計思路和某些方面來說,不錯!

springboot的aop編程