zoukankan      html  css  js  c++  java
  • 【皇甫】☀初识AOP

    新知识,新起点,下面介绍一下aop所要准备架包和各个层

    特点:

    创建好的各个层:

    所需架包:

    具体步骤:

    No.1  搭建分层架构 entity

    1 1 public class User implements Serializable{
    2 2     private Integer id; // 用户ID
    3 3     private String username; // 用户名
    4 4     private String password; // 密码
    5 5     private String email; // 电子邮件
    6 6 }
    entity 具体代码

    No.2 创建数据访问层接口和实现类

    public interface IDao {
        public void save(User user);
    }
    访问层接口
    1 public class UserDao implements IDao {
    2     public void save(User user) {
    3         System.out.println("save success!");
    4     }
    5 }
    实体类

    No.3  创建创建业务逻辑层接口和实现类

    1 public interface IUserBiz {
    2    public void save(User user);
    3 }
    业务逻辑层
     1 public class UserBiz implements IUserBiz{
     2     private IDao dao;
     3     public void save(User user) {        
     4         dao.save(user);
     5     }
     6     //dao 属性的setter访问器,会被Spring调用,实现设值注入
     7     public void setDao(IDao dao) {
     8         this.dao = dao;
     9     }
    10 }
    实体类

    No.4  创建前置和后置增强处理类

    1 public class LoggerBefore implements MethodBeforeAdvice {
    2     private static final Logger log =    Logger.getLogger(LoggerBefore.class);
    3     public void before(Method arg0, Object[] arguments, Object target)
    4             throws Throwable {
    5         log.info("前置内容AAA");
    6     }
    7 }
    前置增强
    1 public class LoggerAfter  implements AfterReturningAdvice{
    2     public void afterReturning(Object returnValue, Method method, Object[] arguments,
    3             Object target) throws Throwable {
    4         System.out.println("后置增强代码");
    5     }
    6 }
    后置增强

    No.5  在Spring配置文件中完成业务对象和DAO的定义和装配,并定义前置增强和后置增强组件

    1 <bean id="dao" class="cn.happy.dao.impl.UserDao"/>
    2    <bean id="biz" class="cn.happy.biz.impl.UserBiz">
    3       <property name="dao" ref="dao"></property>
    4    </bean>
    5    <!-- 定义前置增强组件 -->
    6    <bean id="loggerBefore" class="cn.happy.aop.LoggerBefore"/>
    7    <!-- 定义后置增强组件 -->
    8    <bean id="loggerAfter" class="cn.happy.aop.LoggerAfter"/>
    具体相关代码

    No.6  织入处理

    定义切入点,常见的方法有以下几点:

    public * addNewUser(entity.User)

    public void *(entity.User)

    public void addNewUser(..)

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

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

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans 
     7         http://www.springframework.org/schema/beans/spring-beans.xsd
     8          http://www.springframework.org/schema/aop
     9          http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    10         ">
    11      
    12    <bean id="dao" class="cn.happy.dao.impl.UserDao"/>
    13    <bean id="biz" class="cn.happy.biz.impl.UserBiz">
    14       <property name="dao" ref="dao"></property>
    15    </bean>
    16    <!-- 定义前置增强组件 -->
    17    <bean id="loggerBefore" class="cn.happy.aop.LoggerBefore"/>
    18    <!-- 定义后置增强组件 -->
    19    <bean id="loggerAfter" class="cn.happy.aop.LoggerAfter"/>
    20        <!-- 针对AOP的配置 -->
    21    <aop:config>
    22       <aop:pointcut id="pointcut" expression="execution(public void save2(cn.happy.entity.User))"/>
    23       <!-- 将增强处理和切入点结合在一起,在切入点处插入增强处理,完成"织入"-->
    24       <aop:advisor pointcut-ref="pointcut" advice-ref="loggerBefore"/>
    25        <aop:advisor pointcut-ref="pointcut" advice-ref="loggerAfter"/>
    26    </aop:config>
    27   
    28 </beans> 
    完整配置

    No.7  测试

    用于到测试了,阿西巴

    1 public static void main(String[] args) {
    2         
    3         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    4         IUserBiz biz=(IUserBiz)ctx.getBean("biz");
    5         User user=new User();
    6         biz.save(user);
    7         System.out.println("success!");
    8     }
    测试

    **********************************************************************************************************

    AOP(面向对象编程)在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。AOP在其他领域也有其他含义。 

    附*

    **********************************************************************************************************************

    希望以上内容可以帮到大家

  • 相关阅读:
    Mosaic 前端微服务框架
    使用skipper 扩展fabio 的路由&&http proxy 功能
    Introducing Makisu: Uber’s Fast, Reliable Docker Image Builder for Apache Mesos and Kubernetes
    lua-resty-shell 多任务执行
    openresty 使用lua-resty-shell 执行shell 脚本
    ncm 让跨项目配置一致性简单的工具
    lapis 项目添加prometheus 监控集成grafana
    使用prometheus+ grafana+nginx-module-vts 模块监控openresty
    两天快速开发一个自己的微信小程序
    笔记本如何查看mac地址
  • 原文地址:https://www.cnblogs.com/wangxiangxiang/p/5955778.html
Copyright © 2011-2022 走看看