Spring——AOP三种实现方式

AOP三种实现方式

  • Spring API接口
  • 自定义加载器
  • 注解

需要导入一个依赖包:

<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
    <version>1.9.4version>
dependency>

还需要导入一个约束:

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

接口类:

public interface UserService {
    public void add();
    public void del();
}

实现类:

public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void del() {
        System.out.println("删除了一个用户");
    }
}

切面类:

public class BeforeLog implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object target) throws Throwable {
        System.out.println("~~~执行前~~~");
    }
}
public class AfterLog implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
        System.out.println("~~~执行后~~~");
    }
}

测试类:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userServiceImpl", UserService.class);
        userService.add();
        userService.del();
    }
}

注册Bean:

	
    <bean id="userServiceImpl" class="com.test.service.UserServiceImpl"/>
    <bean id="beforeLog" class="com.test.log.BeforeLog"/>
    <bean id="afterLog" class="com.test.log.AfterLog"/>

1. Spring API接口

    <aop:config>


        <aop:pointcut id="pointcut" expression="execution(* com.test.service.UserServiceImpl.*(..))"/>

        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    aop:config>

2. 自定义加载器

    <aop:config>
        <aop:aspect ref="AspectBean">

            <aop:pointcut id="pointcut" expression="execution(* com.test.service.UserServiceImpl.*(..))"/>

            <aop:before method="beforeLog" pointcut-ref="pointcut"/>
            <aop:after method="afterLog" pointcut-ref="pointcut"/>
        aop:aspect>
    aop:config>

3. 注解

@Aspect
public class AnnoBean {
    @Before("execution(* com.test.service.UserServiceImpl.*(..))")
    public void  before(){
        System.out.println("~~~方法执行前~~~");
    }

    @After("execution(* com.test.service.UserServiceImpl.*(..))")
    public void  after(){
        System.out.println("~~~方法执行后~~~");
    }
}

<aop:aspectj-autoproxy/>
<bean id="annoBean" class="com.test.anno.AnnoBean"/>

你可能感兴趣的:(spring,java,aop,ssm)