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

How to use the function of aop without using spring framework

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use the functions of aop without using the spring framework, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The AOP mechanism of the Spring framework allows developers to extract common functions from business processes and write functional codes separately. In the process of business process execution, the Spring framework will automatically cut the independently written functional code into the appropriate location of the process according to the requirements of the business process.

Spring provides two ways of using AOP to configure using xml

Use annotations

It should be noted here that Spring AOP currently only supports method-level aspects, and the member's interception is not implemented. In addition, spring aop is only an integration framework and is not involved in the specific development of aop.

What if you want to take advantage of the more power of aop, or use the functionality of aop in a framework that doesn't use spring?

Introduction to AspectJ

Spring aop integrates AspectJ (an aspect-oriented extensible framework that can be seamlessly integrated with the java programming language)

An example of using AspectJ

Eclipse Marketplace installation plug-in AJDT

Create an Aspect project

Create an AspectJ test class

Create a section Aspect file

.aj file

Run the java program for HelloAspectJDemo, and the result is:

Do not use spring's aop function to achieve log output the first public class TimeBook {undefined private Logger logger = Logger.getLogger (this.getClass (). GetName ()); / / related programs for auditing data public void doAuditing (String name) {undefined logger.log (Level.INFO, name + "start auditing data..."); System.out.println ("Audit Program"); logger.log (Level.INFO, name + "Audit data end...") }} / / TestHelloWorld.javapackage com.gc.test;import com.gc.action.TimeBook;public class TestHelloWorld {undefined public static void main (String [] args) {undefined TimeBook timeBook = new TimeBook (); timeBook.doAuditing ("Zhang San");}} second: the related program public void doAuditing (String name) {undefined System.out.println ("audit program") to output public class TimeBook implements TimeBookInterface {undefined / / audit data through interface-oriented programming. }} / / TimeBookProxy.javapackage com.gc.action;import org.apache.log4j.Level;import org.apache.log4j.Logger;import com.gc.impl.TimeBookInterface;public class TimeBookProxy {undefined private Logger logger = Logger.getLogger (this.getClass (). GetName ()); private TimeBookInterface timeBookInterface; / / is programmed against the previous interface TimeBookInterface in this class, not against the specific class public TimeBookProxy (TimeBookInterface timeBookInterface) {undefined this.timeBookInterface = timeBookInterface } / / actual business processing public void doAuditing (String name) {undefined logger.log (Level.INFO, "start audit data" + name); timeBookInterface.doAuditing (name); logger.log (Level.INFO, "audit data end" + name);}} public class TestHelloWorld {undefined public static void main (String [] args) {undefined TimeBookProxy timeBookProxy = new TimeBookProxy (new TimeBook ()); timeBookProxy.doAuditing ("Zhang San") }} third: use java's proxy mechanism for log output public class LogProxy implements InvocationHandler {undefined private Logger logger = Logger.getLogger (this.getClass (). GetName ()); private Object delegate; / bind proxy object public Object bind (Object delegate) {undefined this.delegate = delegate; return Proxy.newProxyInstance (delegate.getClass (). GetClassLoader (), delegate.getClass (). GetInterfaces (), this) } / / programming public Object invoke (Object proxy,Method method,Object [] args) throws Throwable {undefined Object result = null; try {undefined / / Log output logger.log (Level.INFO,args [0] + "start auditing data...") before and after the method call; result = method.invoke (delegate, args); logger.log (Level.INFO,args [0] + "Audit data ending...") } catch (Exception e) {undefined logger.log (Level.INFO,e.toString ());} return result;}} / / TimeBookInterface.javapackage com.gc.impl;// programming public interface TimeBookInterface {undefined public void doAuditing (String name) against the interface;} / / TimeBook.javapublic class TimeBook implements TimeBookInterface {undefined / / related programs for auditing data public void doAuditing (String name) {undefined System.out.println ("audit program") }} / / TestHelloWorld.javapublic class TestHelloWorld {undefined public static void main (String [] args) {undefined / / realizes the reuse of log classes LogProxy logProxy = new LogProxy (); TimeBookInterface timeBookProxy = (TimeBookInterface) logProxy.bind (new TimeBook ()); timeBookProxy.doAuditing ("Zhang San");}} the above is how to use the functions of aop without using spring framework. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

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

12
Report