Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of aspect-oriented programming notification in Sping aop

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the method of Sping aop aspect-oriented programming notification". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

It is recommended to have a look at java static proxy and dynamic proxy first.

First, create two interfaces:

API TestServiceInter:

Package com.hubin.aop;public interface TestServiceInter {public void sayHello ();}

API TestServiceInter2:

Package com.hubin.aop;public interface TestServiceInter2 {public void sayBye ();}

Second, create the proxied class Test1Service that implements the above two interfaces

Package com.hubin.aop;/** * is proxied, which is equivalent to doing things myself * @ author Administrator * * / public class Test1Service implements TestServiceInter,TestServiceInter2 {private String name; public String getName () {return name;} public void setName (String name) {this.name = name } public void sayHello () {/ / TODO Auto-generated method stub System.out.println ("hi" + name);} public void sayBye () {/ / TODO Auto-generated method stub System.out.println ("bye" + name);}}

Third, create a pre-notification MyMethodBeforeAdvice class

Package com.hubin.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice / * pre-notification class Something similar to what the intermediary company did before realizing what I told you * @ author Administrator * * / public class MyMethodBeforeAdvice implements MethodBeforeAdvice {/ * method: called method name * args: parameter passed to method * target: target object * / public void before (Method method, Object [] args Object target) throws Throwable {/ / TODO Auto-generated method stub System.out.println ("advance notification is called") }}

4. Create the post notification class MyMethodAfterAdvice:

Package com.hubin.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;/** * post notification * @ author Administrator * * / public class MyMethodAfterAdvice implements AfterReturningAdvice {@ Override public void afterReturning (Object returnValue, Method method, Object [] args, Object target) throws Throwable {/ / TODO Auto-generated method stub System.out.println ("post notification is called");}}

5. Create a surround notification class MyMethodAroundAdvice:

Package com.hubin.aop;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MyMethodAroundAdvice implements MethodInterceptor {@ Override public Object invoke (MethodInvocation arg0) throws Throwable {/ / TODO Auto-generated method stub System.out.println ("before calling surround notification"); arg0.proceed (); / / if this sentence is not called, the method of the proxied class will not run System.out.println ("after calling surround notification") Return null;}}

6. Create an exception notification MyMethodThrowsAdvice:

Package com.hubin.aop;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;/** * exception object * @ author Administrator * * / public class MyMethodThrowsAdvice implements ThrowsAdvice {public void afterThrowing (Method MJO object [] obj,Object target,Exception e) {System.out.println ("something's wrong" + e.getMessage ());}}

7. Create a configuration file beans.xml (the file location is in package com/hubin/aop/)

Com.hubin.aop.TestServiceInter com.hubin.aop.TestServiceInter2 myMethodBeforeAdvice myMethodAfterAdvice myMethodAroundAdvice myMethodThrowsAdvice

8. Create a random class for testing

Package com.hubin.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {/ * * @ param args * / public static void main (String [] args) {/ / TODO Auto-generated method stub ApplicationContext ac=new ClassPathXmlApplicationContext ("com/hubin/aop/beans.xml"); TestServiceInter ts= (TestServiceInter) ac.getBean ("proxyFactoryBean"); ts.sayHello () / ((TestServiceInter2) ts) .sayBye ();}}

IX. Operation result:

Pre-notification is called before hi king sledgehammer calls surround notification post notification is called before bye king sledgehammer invokes surround notification post notification is called "what is the method of Sping aop aspect-oriented programming notification" is introduced here, thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 218

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report