Introduction to Spring AOP instances

Source: Internet
Author: User

Copy from: http://hi.baidu.com/guxing820/blog/item/a7e6e7ccbc717b1601e9288f.html

Thanks for author!

 

 

If you are studying Spring's AOP, I think this article can help you get started!

I want to study the dynamic proxy and reflection mechanisms of Java before Spring AOP, and understand these two concepts and usage as well.

The basis of the Spring framework, because the Spring framework uses a large number of dynamic proxy and reflection mechanisms.

Of course, this article mainly explains Spring's AOP. Next we will start to discuss it!

AOP (Aspect-Oriented Programming), do not think that only Spring has the concept of AOP, because you can find it in many places. If you know what the filters in the Servlet are, you can understand Aspect-oriented programming. It is not difficult to understand it. If you are afraid that you still cannot understand it, let us give another example, for example, if you go to the train station, there will be an item inspection at the door. People just need to put things in the machine for a moment to check. The inspection machine can be understood as only one and there are many items, as long as the item has passed the check, the machine will automatically check it. This is for the aspect! It can be understood that the checking machine is a public class for checking items. As long as the items pass through, they will be checked. This function is not required in the items. This is automatically added when the items pass through!

Maybe the example is not very good. I don't know if I understand Aspect-Oriented Programming now!

For better understanding, I will give you a simple configuration file example, which can be easily explained and understood by everyone!

<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<! -- [Basic configuration file] -->

<Bean id = "proxybean" class = "org. springframework. aop. framework. ProxyFactoryBean">
<Property name = "proxyInterfaces">
<Value> org. bling. spring. aop. BuyInterface </value>
</Property>
<Property name = "target">
<Ref local = "targetbean"/>
</Property>
<Property name = "interceptorNames">
<List>
<Value> beforeadvisor </value>
<Value> afteradvisor </value>
</List>
</Property>
</Bean>

<! -- [Class] -->
<Bean id = "targetbean" class = "org. bling. spring. aop. BuyInterfaceImpl"> </bean>

<! -- [Advisor] -->
<Bean id = "beforeadvisor" class = "org. springframework. aop. support. RegexpMethodPointcutAdvisor">
<Property name = "advice">
<Ref local = "beforeadvice"/>
</Property>
<Property name = "pattern">
<Value> org \. bling \. spring \. aop \. BuyInterface \. buy </value>
</Property>
</Bean>

<Bean id = "afteradvisor" class = "org. springframework. aop. support. RegexpMethodPointcutAdvisor">
<Property name = "advice">
<Ref local = "afteradvice"/>
</Property>
<Property name = "pattern">
<Value> org \. bling \. spring \. aop \. BuyInterface \. buy </value>
</Property>
</Bean>

<! -- [Advice] -->
<Bean id = "beforeadvice" class = "org. bling. spring. aop. Before"> </bean>
<Bean id = "afteradvice" class = "org. bling. spring. aop. After"> </bean>

</Beans>

The above is an example of a simple configuration file. There are four annotations. First, let's take a look at the bottom [Advice] (translated as notification). This is our own notification, for example, check the machine in the above example. The class name is named Before for convenience) and After (here I add a check machine when I go out, you can choose whether or not as needed), the class name is defined by yourself, but note that Before. since this java class is checked before getting started, we need to implement the org. springframework. aop. the MethodBeforeAdvice interface can be understood as Spring's rule. Of course, After. java class also implements an org. springframework. aop. afterReturningAdvice interface. In this way, the Spring framework recognizes the two custom classes. While implementing the interfaces, some interface-defined methods must be implemented. Therefore, Befor E. in java, there will be a public void before (Method arg0, Object [] arg1, Object arg2) throws Throwable Method, After. in java, there is a public void afterReturning (Object returnValue, Method method, Object [] args, Object target) throws Throwable Method, which must be implemented! In this way, our notification is complete!

Next, let's take a look at [Advisor]. This is a class that handed over custom notifications to Spring. This class is org. springframework. aop. support. regexpMethodPointcutAdvisor. This is understood as a cut point, which is actually the Pointcut concept. We inject the notification we have defined, because this class has an advice attribute, which is a little deeper, it can be understood that the definition of this advice attribute is an interface that inherits at least one of our two notification method interfaces. Here, let's take a good look! There is also a pattern attribute in this class. It is not difficult to understand the regular expression. Here we will tell Spring which method of the interface should be notified of this notification, note: escape (\.)! Repeat writing the method name!

After registering our custom notifications, we should look at the [Class]. This class is a class we want to notify, but we simply registered it in the Spring framework, nothing special about this class is that it implements a custom interface, because the Spring framework advocates interface-oriented programming.

Next, let's continue. The last basic configuration file is left, which truly implements the Aspect-Oriented part. The bean ID is defined by itself, but the class = "org. springframework. aop. framework. proxyFactoryBean ", which has several attributes. Here we list several attributes used in this example. The other attributes will be further explained later. First, let's look at the proxyInterfaces attributes, this is the interface that declares the implementation of the class we are notified, because this is an interface, because only one bean is to be notified, use list for multiple interfaces, of course, one can also use list, continue to look at the target attribute. This is to inject our target bean into and continue the interceptorNames attribute. Here is our notice. Here I add two notifications, one is before and the other is after!

So our first Spring AOP instance is complete!

The complete code of each class is provided below for your reference. The configuration file code above is no longer repeated!

* ************************* Before. java ******************

Package org. bling. spring. aop;

Import java. lang. reflect. Method;

Import org. springframework. aop. MethodBeforeAdvice;

Public class Before implements MethodBeforeAdvice {

Public void before (Method arg0, Object [] arg1, Object arg2) throws Throwable {
System. out. println ("Hello, welcome! ");
}
}

* ************************** After. after java *************

Package org. bling. spring. aop;

Import java. lang. reflect. Method;

Import org. springframework. aop. AfterReturningAdvice;

Public class After implements AfterReturningAdvice {

Public void afterReturning (Object returnValue, Method method, Object [] args, Object target)
Throws Throwable {
System. out. println ("Welcome next time! ");
}
}

* *************************** Buy. java **************************

Package org. bling. spring. aop;

Public interface BuyInterface {
Public void buy ();
}

* ****************** BuyInterfaceImpl. java ****************

Package org. bling. spring. aop;

Public class BuyInterfaceImpl implements BuyInterface {

Public void buy (){
System. out. println ("I bought something at the store! ");
}
}

* ********************* MainTest. java ************

Package org. bling. spring. aop;

Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. FileSystemXmlApplicationContext;

Public class MainTest {

Public static void main (String [] args ){
ApplicationContext ac = new FileSystemXmlApplicationContext ("src/applicationContext. xml ");
BuyInterface buy = (BuyInterface) ac. getBean ("proxybean ");
Buy. buy ();
}
}

This article is original. If you have any mistakes, please correct them! More communication

 

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.