Self-study Spring AOP

Source: Internet
Author: User
Tags abstract definition

I am a novice programmer is also the first time to write a blog this article is my combination of online information and some books to learn if there is something wrong, please leave a message to inform

This article describes the two knowledge points of AOP

1: Agent

There are two kinds of agents I write first: Java static proxy

1: Create an interface

Package com.proxy;/** * * @ClassName: iperson* @Description: Abstract definition of this role * @author * @date October 24, 2017 pm 6:40:43* */public in Terface IPerson {public void show ();

  

2: Create class implementation interface

Package com.proxy;

public class Lishi implements IPerson {

public void Show () {
System.out.println ("La la La");
}

}

  

3: Create proxy class implementation interface (requires incoming interface as parameter in construction method)

Package com.proxy;

/**
*
* @ClassName: Lishiproxy
* @Description: proxy class
* @author
* @date October 24, 2017 6:41:33
*
*/
public class Lishiproxy implements IPerson {
Private IPerson IP;

Public Lishiproxy (IPerson IPerson) {
Super ();
This.ip = IPerson;
}

public void Show () {
Ip.show ();
}

}

  

4: Test class calls the proxy class for testing

Package Com.proxy;import Org.junit.test;public class Lishitest {@Testpublic void showls () {Lishi ls = new Lishi ();//an Agent can Sufficient to implement multiple implementation classes IPerson Lip = new Lishiproxy (LS); lip.show ();}}

  

2:java Dynamic Agent

First steps.

1: Create an interface

Package Com.proxy2;public interface Iperson2 {public void eat ();

  

2: Create class implementation interface

Package Com.proxy2;public class Ruan implements Iperson2 {public void Eat () {System.out.println ("Cheerleader");}

  

3: Create proxy class Implementation interface Invocationhandler (constructor method requires incoming interface as parameter)

Package Com.proxy2;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;public class Proxy2 Implements Invocationhandler {private Iperson2 iperson2;public Proxy2 (Iperson2 iperson2) {super (); This.iperson2 = Iperson2;}    Execute public object invoke (object proxy, method, object[] args) throws Throwable {//incoming is the executed person return Method.invoke (IP Erson2, args);}}

  

Note Override a method

4: Test implements the proxy class for testing

Package Com.proxy2;import Java.lang.reflect.proxy;import Org.junit.test;public class Ruantest {@Testpublic void eat () { Iperson2 ip2 = new Ruan ();p roxy2 Proxy2 = new Proxy2 (IP2); Iperson2 iperson2 = (Iperson2) proxy.newproxyinstance (ruan.class . getClassLoader (), Ruan.class.getInterfaces (), Proxy2); Iperson2.eat ();}}

  

2: Notice

Three types of notifications 1: Surround notifications

2: Front-facing notification

3: Post notification

Three kinds of notifications I wrote it together, sent it out once, and I'm going to write it down.

1: Create an interface

Package Com.proxy3;public interface IPerson3 {public void Seelp ();p ublic void Cadd ();}

  

2: Create class to Implement interface target

Package Com.proxy3;public class Siming implements IPerson3 {public void Seelp () {System.out.println ("Hee Hee-hee hehe, ha ha ha haha");} public void Cadd () {System.out.println ("hahaha, notification Filtering");}}

  

3: Create a proxy class notification

Package Com.proxy3;import Java.lang.reflect.method;import Org.springframework.aop.methodbeforeadvice;public class Beforeadvice implements Methodbeforeadvice {public void before (method, object[] args, Object target) throws Throwab Le {System.out.println ("Pre-Notification");}}

  

4: Configure the Applicationcontext.xml file

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.3.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPR Ing-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.3.xsd "><!--target instantiation--><bean id=" siming "class=" com.proxy3.Siming "></bean><!-- Pre-notification--><bean id= "Beforeadvice" class= "Com.proxy3.BeforeAdvice" ></bean><!--post notification--> <bean id= "Afteradvice" class= "Com.proxy3.AfterAdvice" ></bean><!--surround notification--><bean id= "Methods" class= "Com.proxy3.Methods" ></bean><!--a method to go back to trigger the notification--><bean id= "Mybefore" class= " Org.springframework.aop.support.RegexpMethodPointcutAdvisor "><property name=" advice "ref=" Beforeadvice " ></property><property name= "pattern" value= ". *add.*" ></property></bean><!--Configure the hybrid agent- -><bean id= "myproxy" class= "Org.springframework.aop.framework.ProxyFactoryBean" ><!--reference target-->< Property name= "Target" ref= "siming" ></property>< all interfaces implemented by!--target--><property name= "Proxyinterfaces" ><list><value>com.proxy3.IPerson3</value></list></property><!--Configuration Notifications- <property name= "Interceptornames" ><list><idref bean= "Methods"/><idref bean= "MyBefore"/> <idref bean= "Beforeadvice"/><idref bean= "Afteradvice"/&GT;&LT;/LIST&GT;&LT;/PROPERTY&GT;&LT;/BEAN&GT;&LT;/beans> 

This configuration file has a front and rear notification surrounding the configuration of the notification is interesting to see what has been written in detail

5: Use

Package Com.proxy3;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Proxy3test {@Testpublic void Seelp ( {ApplicationContext AC = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); IPerson3 IP3 = (IPerson3) Ac.getbean ("myproxy");//Ip3.seelp (); Ip3.cadd ();}}

  

These are the results of my self-study. For the first time, if the writing is not good, please forgive me.

Self-study Spring AOP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.