Spring's support for AOP

Source: Internet
Author: User

1. AOP Concept

First, AOP is a complement to OOP. It considers the problem of "crosscutting nature". Crosscutting issues can be understood as problems in our same layer class (for example, service layer).

The idea is to take a separate service that traverses the various corners of the system, pull it out and put it in one place, then wait until it runs, then put it in, and consider the "horizontal" thing. Pulling out the crosscutting concerns will make the code much less, more concise, and more reusable.

Demonstrate the basic concepts in AOP:


  


The basic concept is not much to say, from the realization of SPRINGAOP can be seen in the process:

? Finding crosscutting concerns (finding crosscutting issues, specifically implemented as advice)

? Writing pointcuts (Pointcut), which is the scope of the method that specifies advice

? The Joinpoint of the advice (Weave) target object (TargetObject) is woven into the

Weaving (Weave) is dynamic, the default approach is the dynamic agent of the JDK, of course, you can also use the Cglib proxy.

Can be simply expressed as:


  


2. Spring Support for AOP-adoptionAnnotationWay
Dependent package configuration:

*spring_home/dist/spring.jar

*spring_home/lib/log4j/log4j-1.2.14.jar

*spring_home/lib/jakarta-commons/commons-logging.jar

*spring_home/lib/aspectj/*.jar--(to use the relevant annotations provided by ASPECTJ)

Source Code

Interface Usermanager

Package Com.bjpowernode.spring;public interface Usermanager {public void AddUser (string userName, string password);}

Class Usermanagerimpl

Package Com.bjpowernode.spring;public class Usermanagerimpl implements Usermanager {public void AddUser (String username , String password) {System.out.println ("---------usermanagerimpl.add ()--------");}}

*class Securityhandler (to Modularize crosscutting concerns, build related classes)

Package Com.bjpowernode.spring;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.aspectj.lang.annotation.Pointcut; @Aspectpublic class Securityhandler {/** * Defines the name of the Pointcut,pointcut as Addaddmethod (), this method does not return a value and parameter the method is an identity, does not call */@Pointcut ("Execution (* add* (..)) ") private void Addaddmethod () {};/** * defines advice, which indicates which pointcut subscriptions our advice applies to joinpoint on/@Before ("Addaddmethod ()")//@ After ("Addaddmethod ()") private void CheckSecurity () {System.out.println (""); System.out.println ("-------checksecurity-------");}}

For all method names with the Add method, the CheckSecurity () service is added before execution.


Spring configuration file-applicationcontext.xml

<?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 "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.S Pringframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/tx Http://www.springframe Work.org/schema/tx/spring-tx-2.0.xsd "><!--enable ASPECTJ support for annotation-<aop:aspectj-autoproxy/> < ; Bean id= "Usermanager" class= "Com.bjpowernode.spring.UserManagerImpl"/> <bean id= "Securityhandler" class= " Com.bjpowernode.spring.SecurityHandler "/></BEANS> 

classclient-Client

Package Com.bjpowernode.spring;import Org.springframework.beans.factory.beanfactory;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Client {public static void main ( String[] args) {Beanfactory factory = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); System.out.print (Factory); Usermanager Usermanager = (usermanager) factory.getbean ("Usermanager"); Usermanager.adduser ("Zhang San", "123");}}

Operation Result:

-------checksecurity----------------usermanagerimpl.add ()--------


3. Spring support for AOP-using the "configuration file"Way

Achieve the same effect as above.

* Depending on the package configuration, use the "annotation mode" above

* Direct View Source:

Interface Usermanager is the same as above

Class Usermanagerimpl the same as above

Class Securityhandler (Modularity of crosscutting concerns, building related classes):

Package Com.bjpowernode.spring;public class Securityhandler {private void checksecurity () {System.out.println (""); System.out.println ("-------checksecurity-------");}}

* Spring configuration file-applicationcontext.xml

<?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 "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www. Springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/tx Http://www.springfram Ework.org/schema/tx/spring-tx-2.0.xsd "><bean id=" Usermanager "class=" Com.bjpowernode.spring.UserManagerImpl "/><bean id=" Securityhandler "class=" Com.bjpowernode.spring.SecurityHandler "/><aop:config><aop:aspect id =" Securityaspect "ref=" Securityhandler "><!--method with Add <aop:pointcut id=" Addaddmethod "expression=" Execution (* add* (..)) " />--><!--Com.bjpowernode.All methods for all classes under Spring package <aop:pointcut id= "Addaddmethod" expression= "Execution (* com.bjpowernode.spring.*.* (..))" />--><!--com.bjpowernode.spring All add and Del methods for all classes--><aop:pointcut id= "Addaddmethod" expression= " Execution (* com.bjpowernode.spring.*.add* (..)) | | Execution (* com.bjpowernode.spring.*.del* (..)) " /><!--Advice, with a pointcut--><aop:before method= "checksecurity" pointcut-ref= "Addaddmethod"/></ Aop:aspect></aop:config></beans>

Mainly in the Aspect festival, advice and Poincut are linked together


The classclient-client is also the same as above


The result of the operation is correct:

-------checksecurity----------------usermanagerimpl.add ()--------

4. Summary

To do AOP, the point is to find aspect (facets) and cut in at "Run time." Weaving process, using the JDK dynamic proxy by default. Dynamic and flexible, but relatively slow, static relatively fast (can be precompiled), but not flexible. Spring's AOP can provide declarative services for Pojo objects, such as declarative transactions; The Sessionbean inside the EJB can also provide declarative transactions.

Spring's support for 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.