zoukankan      html  css  js  c++  java
  • SpringAOP

    说明

    昨天进行了两个方法的静态代理(方法抽象),那么更多方法的静态代理会是怎样呢

    现在我们在昨天实践的基础上再进行一次静态代理,昨日实践链接:

    Spring AOP - 静态代理实践

    今日对应目录,所有操作对应以下操作进行:

    下面我们开始今天的实践~

    一、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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 开启扫描 -->
        <context:component-scan base-package="com.nenu"/>
    
    </beans>

    二、UserService接口和UserServiceImpl类

    1.UserService接口:

    package com.nenu.service;
    
    public interface UserService {
        public void add();
        public void other();
    }

      

    2.UserServiceImpl类:

    package com.nenu.service.impl;
    
    import com.nenu.service.UserService;
    import org.springframework.stereotype.Component;
    
    @Component
    public class UserServiceImpl implements UserService {
    
        @Override
        public void add() {
            System.out.println("UserServiceImpl add……");
        }
    
        @Override
        public void other() {
            System.out.println("UserServiceImpl other.....");
        }
    
        public UserServiceImpl(){}
    
    }

    三、server包(配置非核心业务)

    1.ServerManager接口

    package com.nenu.server;
    
    public interface ServerManager {
        public void before();
        public void after();
    }

    2.TransactionManager类:

    package com.nenu.server;
    
    public class TransactionManager implements ServerManager{
        public void before(){
            System.out.println("TransactionManager Before");
        }
        public void after(){
            System.out.println("TransactionManager After");
        }
    }

      

    3.OtherManager类:

    package com.nenu.server;
    
    public class OtherManager implements ServerManager{
        public void before(){
            System.out.println("OtherManager Before......");
        }
        public void after(){
            System.out.println("OtherManager After......");
        }
    }

    四、配置代理(核心业务)

    UserServiceStaticProxy类:

    package com.nenu.proxy;
    
    import com.nenu.server.ServerManager;
    import com.nenu.service.UserService;
    
    public class UserServiceStaticProxy implements UserService {
        //目标对象 - 代理谁
        private UserService target;
    
        //要加入记录增强,比如事物时间,加入事务控制
        private ServerManager serverManager;
    
        //通过构造方法来传入具体要代理的对象
        public UserServiceStaticProxy(UserService target, ServerManager serverManager){
            this.target = target;
            this.serverManager = serverManager;
        }
    
        @Override
        public void add() {
            //非核心业务1
            serverManager.before();
            //核心活,目标对象来做
            target.add();
            //非核心业务2
            serverManager.after();
        }
    
        @Override
        public void other() {
            //非核心业务1
            serverManager.before();
            //核心活,目标对象来做
            target.other();
            //非核心业务2
            serverManager.after();
        }
    }

    五、测试

    1.ProxyTest类:

    package com.nenu;
    
    import com.nenu.proxy.UserServiceStaticProxy;
    import com.nenu.server.OtherManager;
    import com.nenu.server.ServerManager;
    import com.nenu.server.TransactionManager;
    import com.nenu.service.UserService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class ProxyTest {
        @Test
        public void proxyTest(){
            //1.配置容器
            ApplicationContext applicationContext =
                    new ClassPathXmlApplicationContext("applicationContext.xml");
            
            //2.取出容器中bean
            UserService target =
                    (UserService) applicationContext.getBean("userServiceImpl");
            
            //3.创建一个代理对象
    //3-1 创建非核心业务增强对象 ServerManager serverManager1 = new TransactionManager(); ServerManager serverManager2 = new OtherManager(); //3-2 创建代理对象 UserServiceStaticProxy proxy = new UserServiceStaticProxy(target, serverManager1); proxy = new UserServiceStaticProxy(proxy, serverManager2); //4.调用 proxy.add(); proxy.other(); } }

    2.测试

    如上图,在昨天的基础上OtherManager类的方法代理就添加成功了

    成功!!!

  • 相关阅读:
    jQuery源码dom ready分析
    jQuery的deferred对象详解(二)
    jQuery的deferred对象详解(一)
    javascript线程解释(setTimeout,setInterval你不知道的事)---转载
    前端工程精粹(二):静态资源管理与模板框架
    拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限。
    Page.User.Identity.Name获取不到结果
    水晶报表(web)表格信息展示
    Office导入导出组件权限配置汇总
    CSS hack样式兼容模式收藏
  • 原文地址:https://www.cnblogs.com/yangf428/p/12319394.html
Copyright © 2011-2022 走看看