____ 9.spring aop spring learning implementations 2: Aop implemented by custom class

(In this case an article on or in conjunction with, a method UserServiceImpl weaving lateral log)

1. Custom enhancements like to write:

public  class Diy { 

    public  void before () { 
        System.out.println ( "former method performed ~~~~~~~~~~~~~~~" ); 
    } 

    public  void After () { 
        System.out.println ( "methods performing ~~~~~~~~~~~~~" ); 
    } 

}

Write 2.spring configuration file:

<?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:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

   <!--1.user bean的注册-->
    <bean id="user" class= "com.xbf.service.UserServiceImpl" /> 

    <-! 2. registered custom class bean -> 
    < bean ID = "DIY" class = "com.xbf.diy.Diy" /> 

    <! - - 3.aop -> 
    < AOP: config > 
        ! <- cutting surface custom class -> 
        < AOP: Aspect REF = "DIY" > 

            ! <- entry point -> 
            < AOP: the pointcut ID = " the pointcut " expression the =" Execution (com.xbf.service.UserServiceImpl * * (..).) " /> 

            <-! cut pasta method ->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>

        </aop:aspect>

    </aop:config>



</beans>

3. The written test class :( interface to receive, and the same dynamic proxies)

public class DiyTest {

    @Test
    public void test(){

        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

        UserService user = (UserService)context.getBean("user");

        user.add();
    }
}

Summary: self-reinforced equivalent class definition cut surface, is to enhance the class methods implementation class weaving transverse (i.e. starting point) method.

 

Guess you like

Origin www.cnblogs.com/xbfchder/p/11272899.html