Spring aspect-oriented programming (AOP)

Source: Internet
Author: User
Tags throwable


is essentially a proxy, just made a configuration file form.


base class

Package _10springaop;public class Person {public int id;public String name;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
DAO Interface

Package _10springaop;public interface Persondao {public String Saveperson (person person);
DAO Interface Implementation

Package _10springaop;public class Persondaoimpl implements Persondao {@Overridepublic String Saveperson (person person) { TODO auto-generated method stub//test SPRINGAOP error handling//int a = 1/0; System.out.println ("Save Person"); return "Saveperson";}}
Slice class

Package _10springaop;import Org.aspectj.lang.proceedingjoinpoint;public class Mytransaction {public void BeginTransaction () {System.out.println ("BeginTransaction");} public void commit (Object var) {System.out.println ("commit"); System.out.println ("var is" + var);} public void Finalmethod () {System.out.println ("the Finally Method"); public void Throwmethod (Throwable ex) {System.out.println ("ex is" + ex.tostring ());} public void Aroundmethod (Proceedingjoinpoint point) throws throwable{//gets the name of the target method System.out.println ("method is" + Point.getsignature (). GetName ());//Call target method//point.proceed (); System.out.println ("End");}}
Test Class

Package _10springaop;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {//begintransaction//method Is Saveperson//save Person//commit//var is saveperson//the finally Method//end//aroundmethod method is not called at the time// Begintransaction//method is saveperson//end//does not call, then the target method and the subsequent post method, and so on all do not call public static void main (string[] args) {// TODO Auto-generated Method Stubapplicationcontext context = new Classpathxmlapplicationcontext (" Applicationcontext.xml ");//If it is Persondao, there is an implementation interface to   produce a proxy object//If it is a persondaoimpl, no implementation interface   produces the target object Persondao Persondao =  (_10springaop.persondao) Context.getbean ("Persondao");p Ersondao.saveperson (new Person ());}}
configuration file

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5 . xsd "><!--Spring AOP namespaces need to be added yourself http://www.springframework.org/schema/beans/spring-beans-2.5.xsd htt P://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd Spring AOP also requires two jar packages Aspectjrt.jar Aspectjweaver.jar import target class, import facets--<bean id= "Persondao" Clas s= "_10springaop.persondaoimpl" ></bean><bean id= "mytransaction" class= "_10SpringAOP.MyTransaction" ></bean><aop:config><aop:pointcut expression= "Execution (* _10springaop.persondaoimpl.* (..)) " Id= "perform"/><aop:aspect ref= "mytransaction" ><aop:before method= "BeginTransaction" pointcut-ref= " Perform "/><!--remember to add returning--><aop:after-returning method=" commit "pointcut-ref=" perform "returning=" var "/><aop:after method=" Finalmethod "pointcut-ref=" perform "/><!--remember to add throwsing--&GT;&LT;AOP: After-throwing method= "Throwmethod" pointcut-ref= "perform" throwing= "ex"/><aop:around method= "AroundMethod" pointcut-ref= "perform"/></aop:aspect></aop:config></beans>



Spring aspect-oriented programming (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.