面向切面编程(AOP)

来源:互联网 发布:java web开发需求 编辑:程序博客网 时间:2024/06/08 06:51

AOP原理

将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决

采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。

 案例:使用Spring AOP实现系统日志功能

      AOP思路:分别编写业务逻辑代码和“增强”代码,运行时再组装


 

 

步骤一:搭建分层架构之entity

public class User implements Serializable{

   private Integerid; // 用户ID

   private Stringusername; // 用户名

   private Stringpassword; // 密码

   private String email; // 电子邮件

}

步骤二:创建数据访问层接口和实现类

public interface IDao {

   public void save(User user);

}

实现类

public class UserDao implements IDao {

   public void save(User user) {

      System.out.println("save success!");

   }

}

步骤三:创建业务逻辑层接口和实现类

public interface IUserBiz {

  public void save(User user);

}

实现类

public class UserBiz implements IUserBiz{

    private IDaodao;

   public void save(User user) {   

      dao.save(user);

   }

   //dao 属性的setter访问器,会被Spring调用,实现设值注入

   public void setDao(IDao dao) {

      this.dao = dao;

   }

}

步骤四:创建前置和后置增强处理类

前置增强

import java.lang.reflect.Method;

import org.apache.log4j.Logger;

importorg.springframework.aop.MethodBeforeAdvice;

public class LoggerBefore implements MethodBeforeAdvice {

   private static final Logger log = Logger.getLogger(LoggerBefore.class);

   public void before(Method arg0, Object[]arguments, Object target)

        throws Throwable {

      log.info("前置内容AAA");

   }

}

后置增强

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class LoggerAfter  implements AfterReturningAdvice{

   public void afterReturning(ObjectreturnValue, Method method, Object[] arguments,

        Objecttarget) throws Throwable {

      System.out.println("后置增强代码");

   }

}

步骤五:在Spring配置文件中完成业务对象和DAO的定义和装配,并定义了前置增强和后置增强组件

<beanid="dao"class="cn.happy.dao.impl.UserDao"/>

  <beanid="biz"class="cn.happy.biz.impl.UserBiz">

      <propertyname="dao"ref="dao"></property>

  </bean>

  <!-- 定义前置增强组件-->

  <beanid="loggerBefore"class="cn.happy.aop.LoggerBefore"/>

  <!-- 定义后置增强组件-->

   <beanid="loggerAfter"class="cn.happy.aop.LoggerAfter"/>

步骤六:织入处理

定义切入点,常见写法有:

public * addNewUser(entity.User)

public void *(entity.User)

public void addNewUser(..)

* com.bdqn.service.*.*(..)

* com.bdqn.service..*.*(..) 

完整配置如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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-4.1.xsd

        ">

    

  <beanid="dao"class="cn.happy.dao.impl.UserDao"/>

  <beanid="biz"class="cn.happy.biz.impl.UserBiz">

      <propertyname="dao"ref="dao"></property>

  </bean>

  <!-- 定义前置增强组件-->

  <beanid="loggerBefore"class="cn.happy.aop.LoggerBefore"/>

  <!-- 定义后置增强组件-->

  <beanid="loggerAfter"class="cn.happy.aop.LoggerAfter"/>

       <!-- 针对AOP的配置 -->

  <aop:config>

      <aop:pointcutid="pointcut"expression="execution(publicvoid save2(cn.happy.entity.User))"/>

      <!-- 将增强处理和切入点结合在一起,在切入点处插入增强处理,完成"织入"-->

      <aop:advisorpointcut-ref="pointcut"advice-ref="loggerBefore"/>

       <aop:advisorpointcut-ref="pointcut"advice-ref="loggerAfter"/>

  </aop:config>

 

</beans>

步骤七:测试

public static void main(String[] args) {

     

      ApplicationContextctx = new ClassPathXmlApplicationContext("applicationContext.xml");

      IUserBizbiz=(IUserBiz)ctx.getBean("biz");

      Useruser=new User();

      biz.save(user);

      System.out.println("success!");

   }