Traditional Spring AOP

Source: Internet
Author: User

Get the target class from the spring container, perform AOP configuration, and let spring Create the proxy class. Fully automated process. The implementation process of traditional spring AOP


Pre-work: Add an AOP namespace



Target classes and their interfaces:


Public interface UserService {public void AddUser ();p ublic void UpdateUser ();


public class Userserviceimpl implements UserService {@Overridepublic void AddUser () {System.out.println ("Spring AOP add u Ser ");} @Overridepublic void UpdateUser () {System.out.println ("Spring AOP Update User");}}



Slice class:

public class Myaspect implements methodinterceptor{@Overridepublic Object invoke (methodinvocation mi) throws Throwable { System.out.println ("front"); Object obj = Mi.proceed (); System.out.println ("after"); return obj;}}


The focus of this blog post is on XML configuration:

<?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" Xsi:sche malocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-bean S.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop.xs D "><!--1 Create target class--><bean id=" Userserviceid "class=" COM.CANYUGAN.C_AOP. Userserviceimpl "></bean><!--2 Create a slice class (notification)--><bean id=" Myaspectid "class=" COM.CANYUGAN.C_AOP. Myaspect "></bean><!--3 Spring Traditional AOP development, make notification reference to target class Pointcut * Add AOP Namespace * AOP programming <aop:config>* &LT;AOP:POINTC Ut> is used to declare pointcuts and to determine which methods on the target class will be enhanced. ID: pointcut name expression: Used to write pointcut expressions (aspectj pointcut expressions) Execution (* com.itheima.c_aop.*.* (..)) Fixed return value type package class name Method name parameter list * <aop:advisor> special facets, only one pointcut and one notification advice-ref: a notification reference pointcut-reF: A pointcut reference--><aop:config><aop:pointcut expression= "Execution (* com.canyugan.c_aop.*.* (..))" Id= " Mypointcut "/><aop:advisor advice-ref=" Myaspectid "pointcut-ref=" Mypointcut "/></aop:config></ Beans>




Traditional Spring AOP

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.