Spring AOP Schema Aop:config

Source: Internet
Author: User
I. Using aop:config tags to implement AOP

First look at an example, as follows

Interface code:

Package Com.lei.demo.aop.schema;

Public interface Ihello {public
    void SayHello ();
}

Interface implementation:

Package Com.lei.demo.aop.schema;

public class HelloService implements Ihello {public

    void SayHello () {
        System.out.println ("-----Hello World!--- --");
    }

}

The next step is to implement AOP, which is to cut into notifications when calling the SayHello method. 1. The first method configures Aop:pointcut and Aop:aspect in Aop:config

Define a facet support class Helloaspect.java

Package Com.lei.demo.aop.schema;
 /* Slice support class
 *
/public class Helloaspect {

    //Pre-notification public  
    void Beforeadvice () {  
        System.out.println ("===========before advice");  
    }
    
    Post final notification public  
    void Afterfinallyadvice () {  
        System.out.println ("===========after finally Advice");}
}

 

XML configuration: Spring-aop-schema.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmln  
        S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.or G/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGF Ramework.org/schema/aop/spring-aop-3.2.xsd "> <!--target Class--<bean id=" HelloService "class="  Com.lei.demo.aop.schema.HelloService "/> <bean id=" Helloaspect "class=" Com.lei.demo.aop.schema.HelloAspect "
        /> <!--Configure Facets--<!--Aop:advisor, which are ordered and must be placed after aop:pointcut--<aop:config> <aop:pointcut id= "hellopointcut" expression= "Execution (* com.lei.demo.aop.schema). *.*(..))" /> <aop:aspect ref= "heLloaspect "> <!-The following two methods are used to define pointcuts pointcut-ref and pointcut--> <aop:before pointcut-ref=" hell Opointcut "method=" Beforeadvice "/> <aop:after pointcut=" Execution (* com.lei.demo.aop.schema). *.*(..))" method= "Afterfinallyadvice"/> </aop:aspect> </aop:config> </beans>

In the above configuration method= "Beforeadvice", method= "Afterfinallyadvice" corresponding to the method in Helloaspect

Test, App.java

Package Com.lei.demo.aop.schema;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    private static applicationcontext ctx;

    public static void Main (string[] args) {
        
        ctx = new Classpathxmlapplicationcontext ("Spring-aop-schema.xml");
        
        Ihello HelloService = Ctx.getbean ("HelloService", ihello.class);
        
        Helloservice.sayhello ();
    }

}

Run App.java with the following results:

===========before Advice

-----Hello World!-----

===========after finally advice

2. The second method configures Aop:pointcut and aop:advisor in Aop:config

Implement the Org.aopalliance.intercept.MethodInvocation interface,

Helloaroundadvice.java as follows:

Package Com.lei.demo.aop.schema;

Import Java.lang.reflect.Method;

Import Org.aopalliance.intercept.MethodInterceptor;
Import org.aopalliance.intercept.MethodInvocation;

public class Helloaroundadvice implements Methodinterceptor {public

    Object invoke (Methodinvocation arg0) throws Throwable {

        System.out.println ("++++++before advice");
        Arg0.proceed ();
        System.out.println ("++++++after advice");
        
        return null;
    }

}

XML configuration: Spring-aop-schema.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmln  
        S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.or G/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGF Ramework.org/schema/aop/spring-aop-3.2.xsd "> <!--target Class--<bean id=" HelloService "class=" C Om.lei.demo.aop.schema.HelloService "/> <bean id=" Helloaspect "class=" Com.lei.demo.aop.schema.HelloAspect " /> <bean id= "Helloarroundadvice" class= "Com.lei.demo.aop.schema.HelloAroundAdvice"/> <!--with Cut slices--<aop:config> <aop:pointcut id= "hellopointcut" expression= "Execution (* Com.lei.demo.aop.sch EMA: *.*(..))"
   />     <aop:advisor advice-ref= "Helloarroundadvice" pointcut-ref= "Hellopointcut"/> </aop:config> &l T;/beans>

Still running App.java, the results are as follows:

++++++before Advice

-----Hello World!-----

++++++after Advice

Second, the AOP to join the transaction, the introduction of Tx:advice

When integrating hibernate, transactions need to be introduced, and the following configuration references Spring 4.0.3 and Hibernate 4.3.5

Spring-hibernate.xml as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
    context= "Http://www.springframework.org/schema/context" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/be Ans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/sche Ma/context/spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org /schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/sch Ema/tx/spring-tx-4.0.xsd > <!--Hibernate4-<!--loading resource files that contain variable information must be loaded at the top of the spring configuration file, which is the first One load-&LT;CONTEXT:PROPERTY-PLAceholder location= "classpath:persistence-mysql.properties"/> <bean id= "sessionfactory" class= "or G.springframework.orm.hibernate4.localsessionfactorybean "> <property name=" dataSource "ref=" DataSource "/&gt
        ; <property name= "Packagestoscan" > <list> <!--can add multiple packages--< value>com.lei.demo.entity</value> </list> </property> <property name= " Hibernateproperties "> <props> <prop key=" Hibernate.hbm2ddl.auto "&GT;${HIBERNATE.HB
                m2ddl.auto}</prop> <prop key= "Hibernate.dialect" >${hibernate.dialect}</prop> <prop key= "Hibernate.show_sql" >${hibernate.show_sql}</prop> </props> </prope Rty> </bean> <!--database Map--<bean id= "DataSource" class= "Org.springframework.jdbc.datas Ource. DrivermaNagerdatasource "> <property name=" driverclassname "value=" ${jdbc.driverclassname} "/> <property na Me= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.user}"/> <property name= " Password "value=" ${jdbc.pass} "/> </bean> <!--Configure Hibernate transaction Manager--<bean id=" transaction Manager "class=" Org.springframework.orm.hibernate4.HibernateTransactionManager "> <property name=" Sessi Onfactory "ref=" Sessionfactory "/> </bean> <!--configuration Transaction exception encapsulated--<bean id=" persistenceexception Translationpostprocessor "Class=" Org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor "/> <!--declarative container transaction management, Transact Ion-manager specifies that the transaction manager is transactionmanager--> <tx:advice id= "Txadvice" transaction-manager= "TransactionManager"
         > <tx:attributes> <tx:method name= "add*" propagation= "REQUIRED"/>   <tx:method name= "get*" propagation= "REQUIRED"/> <tx:method name= "*" read-only= "true"/> </tx:attributes> </tx:advice> <aop:config expose-proxy= "true" > <!--only implement the business logic layer <aop:pointcut id= "txpointcut" expression= "Execution (* com.lei.demo.service). *.*(..))" /> <!--advisor definition, pointcuts and notifications for Txpointcut, Txadvice and <aop:advisor pointcut-ref= "Txpointcut" Advi ce-ref= "Txadvice"/> </aop:config

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.