The java aop

1.AOP: aspect orientied programming Aspect Oriented Programming. It is laterally programming.

Adding new features in the case 2. Oriented Programming is not changing the original code.

3. oriented in the spring section programmed with two applications:

  a) declarative transaction

  b) Custom Programming aop

4.spring provides related concepts aop and api:

Section (Aspect): a modular concern, this concern that cuts across multiple objects.

Connection point (Joinpoint): during program execution in a particular point in Spring AOP, a join point always represents a method execution.

Notification (Advice): action performed on a particular section of the connection point.

Entry point (Pointcut): matching connection point of the assertion.

Audience (Target Object): one or more aspects, an object is notified.

AOP proxy (AOP Proxy): AOP agent or agents may be CGLIB JDK dynamic proxy.

Weaving (Weaving): the aspects with other application types or objects, and create an object to be notified.

The type of notification

  a) Pre-notification (Before advice): Advice to be executed before a join point, but before this notice can not stop the execution flow connection point (unless it throws an exception).

  b) post-notification (After returning advice): notification performed after normal completion of a connection point; for example, a method without throwing an exception, the normal return.

  c) exception notification (After throwing advice): Advice to be executed when an exception is thrown exit (whether normal or exceptional return) in the method.

  d) surrounding the notification (Around Advice): enclosing a connection point notification method call.

6. Use spring provided api development of aop

  a) new java project

  b) introducing jar package

aopalliance.jar

aspectjweaver.jar

commons-logging.jar

spring-aop-4.1.6.RELEASE.jar

spring-aspects-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-tx-4.1.6.RELEASE.jar

  c) preparation of related classes

LogAdvice.java

/ ** 
 * 
 * log to achieve a common business, which implements the pre-notification provided by spring 
 * / 
public  class LogAdvice the implements the MethodBeforeAdvice { 

    @Override 
    public  void before (Method, Method, Object [] args, Object target) throws the Throwable { 
        System.out.println (. target.getClass () getSimpleName () + ":" + method.getName () + "method is performed" ); 
    } 

}

UserServiceImpl.java

/ ** 
 * The service approach, often have some common features, 
 * such as: transaction log, permissions, cache, etc. 
 * 
 * / 
public  class UserServiceImpl the implements UserService { 
    @Override 
    public  void the Add () { 
        System.out. the println ( "the Add" ); 
    } 
    @Override 
    public  void Delete () { 
        System.out.println ( "Delete" ); 
    } 
    @Override 
    public  void Update () { 
        System.out.println ( "Update" ); 
    } 
}

  d) write configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    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">
    <!- This is a public service->
    <bean the above mentioned id = "logAdvice" class = "cn.sxt.aop.LogAdvice" /> 
    <-! This is the real object -> 
    <bean the above mentioned id = "userService" class = "cn.sxt.service.impl.UserServiceImpl "/> 
   <! - use spring aop configuration of the public service and real objects linked  
           before this action needs to be completed by the dynamic proxy, spring dynamic proxy 
           good package, and only need to be done by specifying configuration
     - > 
    <AOP: config> 
        <AOP: "(. * * * cn.sxt.service.impl (..).) Execution" = the pointcut expression The ID = "the pointcut" /> 
        <AOP: the advice-REF = Advisor "logAdvice "= REF-the pointcut" the pointcut "/> 
    </ AOP: config> 
</ Beans>

  e) Test

public class AopTest {
    @Test
    public void testBeforeAdvice(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        UserService us = (UserService)ac.getBean("userService");
        us.add();
    }
}

7. spring provides a non-invasive implemented aop

Code notice

/ ** 
 * 
 * achieve a common log service 
 * / 
public  class LogAdvice {
     public  void log () { 
        System.out.println ( "----- ---- log information" ); 
    } 
}

Configuration

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    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">
    <!- This is a public service ->
    <bean the above mentioned id = "logAdvice" class= "cn.sxt.aop.LogAdvice" /> 
    <-! This is the real object -> 
    <bean the above mentioned id = "userService" class = "cn.sxt.service.impl.UserServiceImpl" /> 
   <! - use spring aop configuration of the public service and real objects linked 
           before this action needs to be completed by the dynamic proxy, spring dynamic proxy 
           good package, and only need to be done by specifying configuration
     ->
    <AOP: config> 
        <AOP: "(. * * * cn.sxt.service.impl (..).) Execution" = the pointcut expression The ID = "the pointcut" /> 
        <AOP: Aspect REF = "logAdvice"> 
            < ! - pre-configured notification -> 
            <AOP: before Method = "log" the pointcut-REF = "the pointcut" /> 
        </ AOP: Aspect> </aop:config> 
</beans>
   

8. aop achieved by annotations

Notice

/ ** 
 * 
 * achieve a common log service 
 * / 
@Aspect 
public  class LogAdvice {
     @Before ( "Execution (cn.sxt.service.impl *. *. * (..))" )
     public  void log () { 
        System.out.println ( "----- ---- log information" ); 
    } 
}

Configuration, a autoconfiguration

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    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">
    <!- This is a public service ->
    <bean the above mentioned id = "logAdvice"class= "cn.sxt.aop.LogAdvice" /> 
    <-! This is the real object -> 
    <bean the above mentioned id = "userService" class = "cn.sxt.service.impl.UserServiceImpl" /> 
      <-! use annotations to achieve AOP ->
       <AOP: AspectJ-the autoproxy /> 
</ Beans>

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11267582.html
AOP