以下内容是模仿杨开振<<深入浅出springboot 2.x>>的4.2章节内容。
开始前,需要先修改pom.xml,加入以下内容
<dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.9.1version> dependency>
后面是需要逐一天增加或者修改的文件内容
- Note.java
- NoteService.java
- NoteServiceImpl.java
- NoteAspect.java
- NoteController.java
- 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;
//如果这里使用Component注解,后面的App.java中就不需要使用@Bean注解 @Component @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 FilterRegistrationBeanfilterRegistrationBean() { FilterRegistrationBean bean = new FilterRegistrationBean<>(); bean.addUrlPatterns("/*"); bean.setFilter(new Myfilter()); return bean; } }
上面的例子中,如果不想在App中通过@Bean来生成NoteAspect的bean,也可以在NoteAspect的类的前面添加@Component
aop编程,在某些方面挺好用,例如记录日志,或者是设计一些底层的框架。
从设计思路和某些方面来说,不错!