Spring学习笔记(六)

    xiaoxiao2021-12-14  20

    1、AOP

    这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。

    面向对象的特点是继承、多态和封装。而封装就要求将功能分散到不同的对象中去,这在软件设计中往往称为职责分配。实际上也就是说,让不同的类设计不同的方法。这样代码就分散到一个个的类中去了。这样做的好处是降低了代码的复杂程度,使类可重用。但是人们也发现,在分散代码的同时,也增加了代码的重复性。比如说,我们在两个类中,可能都需要在每个方法中做日志。按面向对象的设计方法,我们就必须在两个类的方法中都加入日志的内容。也许他们是完全相同的,但就是因为面向对象的设计让类与类之间无法联系,而不能将这些重复的代码统一起来。也许有人会说,那好办啊,我们可以将这段代码写在一个独立的类独立的方法里,然后再在这两个类中调用。但是,这样一来,这两个类跟我们上面提到的独立的类就有耦合了,它的改变会影响这两个类。那么,有没有什么办法,能让我们在需要的时候,随意地加入代码呢?这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。 

    一般而言,我们管切入到指定类指定方法的代码片段称为切面,而切入到哪些类、哪些方法则叫切入点。

    这样看来,AOP其实只是OOP的补充而已。OOP从横向上区分出一个个的类来,而AOP则从纵向上向对象中加入特定的代码。

    2、AOP使用方式

    (1)基于注解方式进行AOP开发

    (2)基于XML配置方式进行AOP开发

    3、基于注解方式进行AOP开发实例

    (1)MyInterceptor.java

    /** * 声明把MyInterceptor类作为一个切面 */ @Aspect public class MyInterceptor { /** * 定义一个切入点 */ @Pointcut("execution(* com.spring.service.impl.PersonServiceBean.*(..))") private void qrd() {} /** * 定义前置通知 */ @Before("qrd()") public void qztz() { System.out.println("前置通知"); } /** * 定义后置通知 */ @AfterReturning(("qrd()")) public void hztz() { System.out.println("后置通知"); } /** * 定义最终通知 */ @After("qrd()") public void zztz() { System.out.println("最终通知"); } /** * 定义异常通知 */ @AfterThrowing(("qrd()")) public void yctz() { System.out.println("例外通知"); } /** * 定义环绕通知 */ @Around("qrd()") public Object hrtz(ProceedingJoinPoint pjp) throws Throwable { System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); return result; } } (2)PersonService.java

    public interface PersonService { public String findName(int id); public void save(String name); public void update(String name); }

    (3)PersonServiceBean.java

    public class PersonServiceBean implements PersonService { public String findName(int id) { return "Diana"; } public void save(String name) { System.out.println("PersonServiceBean save"); } public void update(String name) { throw new RuntimeException("异常"); } } (4)beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 声明自动为spring容器中那些配置@Aspect切面的bean创建代理织入切面 --> <aop:aspectj-autoproxy/> <bean id="myInterceptor" class="com.spring.interceptor.MyInterceptor"></bean> <bean id="personServiceBean" class="com.spring.service.impl.PersonServiceBean"></bean> </beans> (5)Test.java

    public class Test { @org.junit.Test public void test1() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personServiceBean"); personService.findName(1); } @org.junit.Test public void test2() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personServiceBean"); personService.save("xxx"); } @org.junit.Test public void test3() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personServiceBean"); personService.update("xxx"); } } (6)结果

    test1()运行结果:

    进入方法 前置通知 退出方法 最终通知 后置通知

    test2()运行结果:

    进入方法 前置通知 PersonServiceBean save 退出方法 最终通知 后置通知

    test3()运行结果:

    进入方法 前置通知 最终通知 例外通知

    4、基于XML配置方式进行AOP开发实例

    (1)MyInterceptor.java

    public class MyInterceptor { public void qztz() { System.out.println("前置通知"); } public void hztz() { System.out.println("后置通知"); } public void zztz() { System.out.println("最终通知"); } public void yctz() { System.out.println("例外通知"); } public Object hrtz(ProceedingJoinPoint pjp) throws Throwable { System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); return result; } }

    (2)PersonService.java

    public interface PersonService { public String findName(int id); public void save(String name); public void update(String name); }

    (3)PersonServiceBean.java

    public class PersonServiceBean implements PersonService { public String findName(int id) { return "Diana"; } public void save(String name) { System.out.println("PersonServiceBean save"); } public void update(String name) { throw new RuntimeException("异常"); } }

    (4)beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:config> <aop:aspect id="asp" ref="myInterceptor"> <aop:pointcut id="qrd" expression="execution(* com.spring.service.impl.PersonServiceBean.*(..))"/> <aop:before pointcut-ref="qrd" method="qztz"/> <aop:after-returning pointcut-ref="qrd" method="hztz"/> <aop:after pointcut-ref="qrd" method="zztz"/> <aop:after-throwing pointcut-ref="qrd" method="yctz"/> <aop:around pointcut-ref="qrd" method="hrtz"/> </aop:aspect> </aop:config> <bean id="myInterceptor" class="com.spring.interceptor.MyInterceptor"></bean> <bean id="personServiceBean" class="com.spring.service.impl.PersonServiceBean"></bean> </beans>

    (5)Test.java

    public class Test { @org.junit.Test public void test1() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personServiceBean"); personService.findName(1); } @org.junit.Test public void test2() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personServiceBean"); personService.save("xxx"); } @org.junit.Test public void test3() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personServiceBean"); personService.update("xxx"); } }

    (6)结果

    test1()运行结果:

    前置通知 进入方法 退出方法 最终通知 后置通知

    test2()运行结果:

    前置通知 进入方法 PersonServiceBean save 退出方法 最终通知 后置通知

    test3()运行结果:

    前置通知 进入方法 例外通知 最终通知

    转载请注明原文地址: https://ju.6miu.com/read-971192.html

    最新回复(0)