I started to learn the AOP function and successfully output Hello world. Haha, the first step is to succeed.
In this example, we first write a simple output World for the class, and then use AOP. When the program is running, let the instance of this class output "Hello World! ".
Code List 1: messagewriter class
package com.enyes.heart.java.aop.simple;public class MessageWriter{ public void writeMessage() { System.out.print("World"); }}
The messagewriter class provides clear functions at a glance. It only has one output method to print the world to the console. We are going to add a notification to this class so that its writemessage () method will input "Hello world! ".
To completely output Hello world, we need to output hello before this method, and output it after this method is complete !. AOP terminology: We need to enclose the notification (around advice), that is, the notification that surrounds the connection point. In this instance, the connection point is the call to the writemessage () method. The following messagedecorator class implements surrounded notifications.
Code List 2: messagedecorator class.
package com.enyes.heart.java.aop.simple;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MessageDecorator implements MethodInterceptor{ public Object invoke(MethodInvocation invocation) throws Throwable { System.out.print("Hello "); Object obj = invocation.proceed(); System.out.println("!"); return obj; } }
The methodinteceptor interface is the AOP standard interface that implements the surrounded notification for the method call connection point. The methodinvocation object indicates the method call currently notified. We use this class to control when to call a method. Therefore, we can perform logical operations before a method call or after a method call. In the code list above, we simply output hello, then call the target method, and then input !.
Finally, the messagedecorator notification is woven into the code. Messagewriter is the target object, and then creates a proxy for it. Then, the proxy factory is woven into the messagedecorator notification.
Code List 3: Notifications are woven by messagedecorator.
package com.enyes.heart.java.aop.simple;import org.springframework.aop.framework.ProxyFactory;public class HelloWorldWeaver{ public static void main(String[] args) { MessageWriter target = new MessageWriter(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvice(new MessageDecorator()); proxyFactory.setTarget(target); MessageWriter proxy = (MessageWriter)proxyFactory.getProxy(); target.writeMessage(); System.out.println(""); proxy.writeMessage(); }}
Here, the final part is that we use the proxyfactory class to create a proxy for the target object and weave the notification at the same time. We call addadvice () to send the messagedecorator notification to proxyfactory, and then set the target object to be woven through settarget. After the target object is set and a notification is added, we use the getproxy () method to obtain a proxy and then use the proxy to call the target method. Finally, we compare the results of the two different methods. As follows.
-------------------------------
WorldHello World!
--------------------------------
The jar package supports the following instructions:
1. aopalliance-1.0.jar AOP standard jar
2.org. springframework. aop-3.1.2.RELEASE.jar spring3.1.2 release version of aopjar, you can download the complete jar package on the spring official website.
3.org. springframework. core-3.1.2.RELEASE.jar spring core package, AOP depends on this jar package.
4. commons-logging.jar spring uses this package to print logs.
5.com.springsource.net. SF. cglib-2.1.3.jar cglib proxy. If the package is missing, the error message is as follows:
Exception in thread "main" org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:67)at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:104)at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:98)at com.enyes.heart.java.aop.simple.HelloWorldWeaver.main(HelloWorldWeaver.java:15)
Note: Non-cglib-2.1_3.jar package, if you use this package, the error message is as follows, then you go to find the ASM package...
Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/Typeat net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:180)at net.sf.cglib.core.KeyFactory.<clinit>(KeyFactory.java:66)at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)at org.springframework.aop.framework.Cglib2AopProxy.createEnhancer(Cglib2AopProxy.java:229)at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:171)at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:146)at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:98)at com.enyes.heart.java.aop.simple.HelloWorldWeaver.main(HelloWorldWeaver.java:15)Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Typeat java.net.URLClassLoader$1.run(URLClassLoader.java:200)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:188)at java.lang.ClassLoader.loadClass(ClassLoader.java:307)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)at java.lang.ClassLoader.loadClass(ClassLoader.java:252)at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)... 8 more
In addition, jar can be quickly searched for and downloaded through the findjar website. Without Baidu, you can use point downloading in csdn. This is perfect for people like me who have no points.