1. 程式人生 > 實用技巧 >Spring--->AOP

Spring--->AOP

第一種

1、User Service介面

package com.xian.dao;

public interface UserService {
    public void add();
    public void delete();
    public void insert();
    public void update();

}

2、介面實現類(重寫全部的方法)

package com.xian.service;

import com.xian.dao.UserService;

public class UserServiceImpl implements UserService {

    
public void add() { System.out.println("增加使用者"); } public void delete() { System.out.println("刪除使用者"); } public void insert() { System.out.println("插入使用者"); } public void update() { System.out.println("更新使用者"); } }

3、前部環繞

package com.xian.service;

import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class Log implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("執行了"+o.getClass().getName()+"的"+method.getName()+"方法"); } }

4、後部環繞

package com.xian.service;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
        System.out.println("執行了"+target.getClass().getName()+"的"+method.getName()+"方法"+"返回值:"+returnValue);
    }
}

5、配置Spring-beans.xml

<?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">

   <bean id="log" class="com.xian.service.Log"></bean>
    <bean id="afterLog" class="com.xian.service.AfterLog"/>
    <bean id="userService" class="com.xian.service.UserServiceImpl"/>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.xian.service.UserServiceImpl.*(..))"/>
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

6、test

import com.xian.dao.UserService;
import com.xian.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void Test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userservice = (UserService) context.getBean("userService");
        userservice.add();
    }
}

第二種 自定義

上面User Service和UserServiceImpl不變

1、編寫 環繞要執行的程式碼

public class DiyPoint {
    public void before(){
        System.out.println("---before---");
    }
    public void after(){
        System.out.println("---after---");
    }
}

2、配置Bean

<?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">

    <bean id="diy" class="com.xian.dao.DiyPoint"/>
    <bean id="userService" class="com.xian.service.UserServiceImpl"/>

    <!--aop的配置-->
    <aop:config>
        <!--第二種方式:使用AOP的標籤實現-->
       <aop:aspect ref="diy">
           <aop:pointcut id="diyPointCut" expression="execution(* com.xian.service.UserServiceImpl.*(..))"/>
           <aop:before pointcut-ref="diyPointCut" method="before"></aop:before>
           <aop:after pointcut-ref="diyPointCut" method="after"></aop:after>
       </aop:aspect>
    </aop:config>
</beans>

3、Test

public class Test {
    @org.junit.Test
    public void Test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userservice = (UserService) context.getBean("userService");
        userservice.add();
    }
}

第二種明顯沒有第一種強

第三種 註解開發

之前的還是不變,直接配置註解

1、首先需要環繞的類

@Aspect
public class Annotation {
    @Before("execution(* com.xian.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("---before---");
    }
    @After("execution(* com.xian.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("---after---");
    }
    @Around("execution(* com.xian.service.UserServiceImpl.*(..))")
    public void arround(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("環繞前");
        System.out.println("簽名前:"+jp.getSignature());
        Object process=jp.proceed();
        System.out.println("環繞後");
        System.out.println(process);
    }
}

2、配置Bean

<bean id="annotationPointcut" class="com.kuang.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>

test就完事了

AOP的主要功能在日誌,安全方面應用較多