Spring从入门到精通—基于XML配置和使用AOP

1、Spring AOP半自动编程

核心步骤: 1. 创建一个接口以及它的实现类 2. 编写切面类,实现MethodInterceptor接口的invoke方法 3. 配置Spring的配置文件,xml文件中的配置重要是:【重要】    1).配置目标类的bean    2).配置切面类的bean    3).配置代理对象,其中代理对象中主要的配置如下'       a.配置接口       b.配置目标类       c.配置切面类       d.还可以用<property name="optimize" value="true"/>,指明使用cglib的代理对象

1.1首先编写一个接口

package com.xzy.service;

public interface UserSeviceBase {
    /**
     * 增加用户
     */
    public void addUser();

    /**
     * 删除用户
     * @param id
     */
    public void deleteUser(int id);

    /**
     * 更新用户
     * @param id
     */
    public void updateUser(int id);

}

1.2 编写接口的实现类:

package com.xzy.service;

public class UserSeviceImpl implements  UserSeviceBase {
    @Override
    public void addUser() {
        System.out.println("增加了1个用户");
    }

    @Override
    public void deleteUser(int id) {
       System.out.println("删除了id为"+id+"的用户");
    }

    @Override
    public void updateUser(int id) {
      System.out.println("id为"+id+"的用户更新了");
    }
}

1.3 编写一个切面类:让这个切面类实现MethodInvocation接口的invoke方法

package com.xzy.Aspect;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.util.Date;

//切面类
public class MyAspect implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("执行操作前日志......" + new Date());

        Object obj = methodInvocation.proceed();

        System.out.println("执行操作后日志......" + new Date());
        return obj;
    }
}

1.4 在Spring的配置文件Application.xml中注册代理对象

<!--注册目标对象UserService-->
<bean id="userService" class="com.xzy.service.UserSeviceImpl"></bean>
<!--注册切面类-->
<bean id="myAspect" class="com.xzy.Aspect.MyAspect"></bean>
<!--注册代理对象-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
      <!--接口-->
     <property name="interfaces" value="com.xzy.service.UserSeviceBase"/>
      <!--目标对象-->
      <property name="target" value="#{userService}"/>
      <!--切面类-->
      <property name="interceptorNames" value="myAspect"/>
    <!--配置使用cglib,当没有配置的时候默认是false,也就是使用JDk的Proxy-->
      <property name="optimize" value="true"/>
</bean>

1.5 测试:

package com.xzy;

import com.xzy.service.UserSeviceBase;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Unit test for simple App.
 */
public class AppTest {

    @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");

        //从Spring容器中拿代理对象
        UserSeviceBase userSevice= (UserSeviceBase) context.getBean("proxyService");

        userSevice.deleteUser(23);

        userSevice.updateUser(3);
    }
}

测试结果:

2、Spring AOP 全自动编程

主要步骤: 1).实现切面类 2).在bean配置文件中吧切点和切面关联起来

2.1 实现一个切面类,继承MethodInterceptor接口实现它的invoke方法

package com.xzy.Aspect;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.util.Date;

//切面类
public class MyAspect implements MethodInterceptor {


    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("执行操作前日志......" + new Date());

        Object obj = methodInvocation.proceed();

        System.out.println("执行操作后日志......" + new Date());
        return obj;
    }
}

接着在Spring配置文件Application.xml中配置aop的schema文件,如下红框的配置:

接着在配置文件中添加如下内容:
<!--配置UserService-->
<bean id="userService" class="com.xzy.service.UserSeviceImpl"></bean>
<!--配置切面类-->
<bean id="myAspect" class="com.xzy.Aspect.MyAspect"></bean>

<!--配置全自动AOP代理-->
<aop:config >
  <!--配置切点expression:表达式,就是用来切入的业务类-->
  <aop:pointcut  id="myPointcut" expression="execution(* com.xzy.service.*.*(..))"/>
   <!--配合通知  通知要关联切点和切面类
          advice-ref:切面类
          pointcut-ref:切点
    -->
   <aop:advisor advice-ref="myAspect"  pointcut-ref="myPointcut"></aop:advisor>
</aop:config>

对于配置全自动AOP代理的一点说明,如下图所示<aop:config>标签中重要的三种元素:切点:<aop:pointcut>、切面:<aop:aspect>、通知:<aop:advisor>

其中通知:``又有以下5种:
测试:
 @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");

        //从Spring容器中拿代理对象
        UserSeviceBase userSevice= (UserSeviceBase) context.getBean("userService");

        userSevice.deleteUser(23);

        userSevice.updateUser(3);
    }

测试结果:

写在最后        通过实际的操作可以明显感受到第二种方式实现AOP是省事儿又简单的一种方式,然而第二种方式也是Spring中使用到AOP是常用到的一种配置方式。既然有全自动xml的配置方式,那么一定就会有对应对一套使用注解配置对方式。下一节就讲讲如何用注解配置使用Spring AOP

留言区

还能输入500个字符