Spring AOP-3, simple example

Source: Internet
Author: User

This example uses the previous Spring-spring mvc Framework getting started. Of course, you can also use your own.

1. Because @ Aspect is used to mark this class as a class for setting attention (partition class...), a corresponding jar file is required. The corresponding file in my resources, the name is called aspectj-1.7.0.jar, the compressed package contains a Libs directory with the corresponding jar file, * the jar tool can not be added. In addition, an interceptor class is required. The involved package is com.springsource.org. aopalliance-1.0.0.jar (this is what the Spring Aop framework specifies and is not required for a separate aspectj), which is also in my resources. Therefore, four additional databases must be added:

Com.springsource.org. aopalliance-1.0.0.jar

Aspectjrt. jar

Aspectjweaver. jar

Org. aspectj. matcher. jar

2. Create a class with the following code:

[Java]

Import org. aspectj. lang. annotation. Aspect;
Import org. aspectj. lang. annotation. Before;
Import org. aspectj. lang. annotation. Pointcut;
Import org. springframework. stereotype. Component;
 
@ Component
@ Aspect
Public class SystemArchitecture {
@ Pointcut ("within (xyz. sample. baremvc. * Controller )")
Public void testController (){
}
@ Before ("testController ()")
Public void testAdvice ()
{
System. out. println ("Hello AOP world ");
}
}
Note the following points:
1. We add @ Component. We hope that this class will be automatically scanned when Spring performs Component scanning, because @ Aspect does not belong to @ Component (unlike @ Service and @ Controller), you need to add @ Component.

2. @ Pointcut indicates the classes and methods we are interested in. Here, the classes we are interested in are xyz. sample. under the baremvc package (within indicates the type of interest), and the class ends with those of Controller. Therefore, the class we are interested in is the HomeController class, other classes such as AppConfig and CaseInsensitiveComparator are not included. PointCut can be used for classes, methods, and method parameters, this is because Pointcut is the core part of AOP, so it is complicated. We will consider various examples later. We will not consider these examples here. Note that the function after @ Pointcut annotation is equivalent to an identifier and used for reference by Advice.

3. @ Before indicates the recommended type, that is, the action is taken Before the Join point ("Before"), followed by testController () it indicates that the corresponding action is for those Join points. This is actually the method defined above, which is in the HomeController class. Here there are two methods: home (), the other is public String compare (..).

Therefore, the above Aspect adds attention to HomeController. When HomeController executes various methods, this Aspect executes the defined Advice, which is a Before-type Advice, therefore, it will be executed before the HomeController class method is executed. Therefore, after entering: http: // localhost: 8080/baremvc in the address bar of the browser, the output on the Console is:

Hello AOP world

HomeController: Passing through...

3. Add an Aspect proxy in the context of Spring. The following is the code:

[Html]
Ersion = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: mvc = "http://www.springframework.org/schema/mvc"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation ="
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
<Context: component-scan base-package = "yuxd. sample"> </context: component-scan>
<Mvc: annotation-driven> </mvc: annotation-driven>
<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "suffix" value = ". jsp"> </property>
<Property name = "prefix" value = "WEB-INF/views/"> </property>
</Bean>
<Aop: aspectj-autoproxy/>

</Beans>
Here we add <aop: aspectj-autoproxy/> to enable the classes marked with @ Aspect to be automatically followed by the proxy (the classes to be switched)
The example is simple, right? Leave a message if you have any questions.

Related Article

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.