In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how SpringAOP is used in web applications. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Configure AOP declaratively (that is, use the xml configuration file)
1. How to use ProxyFactoryBean:
The ProxyFactoryBean class is an implementation class of FactoryBean that allows you to specify a bean as a target and provide a set of notifications and advisers to the bean (which will eventually be merged into an AOP agent) that, along with our previous ProxyFactory, are implementations of Advised.
The following is a simple example: a student and a teacher, the teacher will tell the students what to do.
Public class Student {public void talk () {System.out.println ("I am a boy");} public void walk () {System.out.println ("I am walking");} public void sleep () {System.out.println ("I want to sleep");}}
Teacher class
Public class Teacher {private Student student; public void tellStudent () {student.sleep (); student.talk ();} public Student getStudent () {return student;} public void setStudent (Student student) {this.student = student;}}
Let's create a notification class, which is the same notification type in SpringAOP as before and create the
Package cn.lyn4ever.aop;import org.aspectj.lang.JoinPoint;public class AuditAdvice implements MethodBeforeAdvice {@ Override public void before (Method method, Object [] objects, @ Nullable Object o) throws Throwable {System.out.println ("this method was notified" + method.getName ());}}
The notification class is then managed using spring's IOC, which is declared in the xml configuration file as follows:
Advice
Test class
Public static void main (String [] args) {GenericXmlApplicationContext context = new GenericXmlApplicationContext (); context.load ("application1.xml"); context.refresh (); Teacher teacher = (Teacher) context.getBean ("teacherOne"); teacher.tellStudent ();}
There is no problem with the running result.
This is done by creating a notification directly. Next, let's try to create a pointcut (because the above notifies all the methods in the class, then we use the pointcut to notify only some of them). Add the following to the xml configuration file.
Advice advisor
The aspectj expression in the figure above is miswritten, and there is a correct
two。 Use aop namespaces
The following namespaces were introduced into xml, and I cataloged other extra namespaces in order not to be affected. Then it is very common to inject the previous three bean.
In this configuration, we can also configure other types of notifications, but this method property must write methods in the notification class that we customize.
The following syntax is also supported when writing expression in aop:pointcut:
3. Use @ AspectJ style annotations
Although the annotation class is declared through annotations, you still need to configure a little bit of content in xml (it can also be configured by annotations, but there is a more convenient way to use it in springboot)
For convenience, only a HighStudent is written, and its method is called directly without relying on an external teacher instance to call
Package cn.lyn4ever.aop.aspectj;import cn.lyn4ever.aop.aopconfig.Teacher;import org.springframework.stereotype.Component;/** * declares that this is a SpringBean, which is managed by Spring * / @ Componentpublic class HighStudent {public void talk () {System.out.println ("I am a boy");} public void walk () {System.out.println ("I am walking") } / * this method adds a teacher as a parameter to configure args () * @ param teacher * / public void sleep (Teacher teacher) {System.out.println ("I want to sleep");}} in the following pointcut
Create a facet class
Package cn.lyn4ever.aop.aspectj;import cn.lyn4ever.aop.aopconfig.Teacher;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component / * declare facet classes, that is, including pointcuts and notifications * / @ Component / / declare to be managed by spring @ Aspect / / indicates that this is a facet class public class AnnotatedAdvice {/ * create pointcuts, of course, it can also be multiple * / @ Pointcut ("execution (* talk* (..)") Public void talkExecution () {} @ Pointcut ("bean (high*)") / / Why is high here, because this time we test bean is highStudent public void beanPoint () {} @ Pointcut ("args (value)") public void argsPoint (Teacher value) {} / * create notification, of course, multiple parameters of this annotation are the name of the pointcut method above. Note that some of the parameters of the notification method are the same as before, add JoinPoint. Or do not add * / @ Before ("talkExecution ()") public void doSomethingBefore (JoinPoint joinPoint) {System.out.println ("before: Do Something" + joinPoint.getSignature (). GetName () + "()") } / * surround notification please add the parameter ProceedingJoinPoint, which is a subclass of joinPoint * because if you want to release the method, you must add this * @ param joinPoint * @ param teacher * / @ Around ("argsPoint (teacher) & & beanPoint ()") public Object doSomethindAround (ProceedingJoinPoint joinPoint, Teacher teacher) throws Throwable {System.out.println ("Around: Before Do Something" + joinPoint.getSignature (). GetName ()); Object proceed = joinPoint.proceed () System.out.println ("Around: After Do Something" + joinPoint.getSignature (). GetName () + "()"); return proceed;}}
Scan comments on configuration in xml
Configure scan annotations using Java annotation configuration
@ Configuration / / declares that this is a configuration class @ ComponentScan ("cn.lyn4ever.aop.aspectj") @ EnableAspectJAutoProxy (proxyTargetClass = true) / / equivalent to public class BeanConfig {} in xml
Testing method
Package cn.lyn4ever.aop.aspectj;import cn.lyn4ever.aop.aopconfig.Teacher;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.GenericApplicationContext;import org.springframework.context.support.GenericXmlApplicationContext;public class AspectMain {public static void main (String [] args) {/ / xmlConfig (); javaConfig ();} private static void javaConfig () {GenericApplicationContext context = new AnnotationConfigApplicationContext (BeanConfig.class); HighStudent student = (HighStudent) context.getBean ("highStudent"); student.sleep (new Teacher ()) / / should be notified around System.out.println (); student.talk (); / / pre-notification System.out.println (); student.walk (); / / will not be notified System.out.println ();} private static void xmlConfig () {GenericXmlApplicationContext context = new GenericXmlApplicationContext (); context.load ("application_aspect.xml"); context.refresh (); HighStudent student = (HighStudent) context.getBean ("highStudent"); student.sleep (new Teacher ()) / / should be notified around System.out.println (); student.talk (); / / pre-notification System.out.println (); student.walk (); / / will not be notified System.out.println ();}}
Thank you for reading! This is the end of the article on "how to use SpringAOP in web applications". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.