SpringAOP需要解决的问题

Redis快速入门教程+个人实战案例:Redis+SpringAOP解决数据缓存的问题 不想上班了~必看redis使用StringHashList注意:在 Redis 2.4 版本以前的 LPUSH 命令,都只接受单个 value 值。SetSortedSet好了,上面就是基本操作方式了,那下面直接来个小案例这个案例是我自己的一个小经验第一步:添加2个元注解第二步编写切面最后一步!!!!这一步最重要!!!你需要吧元注解加在需要判断缓存和清空缓存的方法上。最后,虽然说不想上班(好像确实... 阅读详情

1. 传统编码的事务问题

sql的事务,存在于Connection(连接中),默认情况下都是自动提交,我们每一次执行一条sql语句都会获取一个Connection连接,而在正常情况下,我们的业务可能会操作多条sql语句,这些sql语句要么同时生效,要么同时回滚。
也就是说事务的控制不能在dao层中,必须要在service中进行。
如何保证多条sql同时生效,或同时回滚?
保证执行多条sql语句的连接Connection,只有一个
怎么实现:
将Connection与线程绑定,需要连接的时候在线程中拿

/**
 * 获取连接的工具类
 * 从线程上获取一个连接
 * 如果线程中没有连接,就从数据源中拿,并且绑定到线程中
 */
@Component
public class ConnectionUtils {
    ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    @Autowired
    private DataSource dataSource;
    /**
     * 从线程中获取连接的方法,
     * 也是将连接与线程绑定
     * @return
     */
    public Connection getThreadConnection(){
        try{
            Connection connection = tl.get();
            if(connection == null){
            	//创建一个连接放在线程中
                connection = dataSource.getConnection();
                tl.set(connection);
            }
            return connection;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    public void removeConnection(){
        tl.remove();
    }
}

dao中执行sql语句必须使用本线程中绑定的连接(Connection)

runner.query(connectionUtils.getThreadConnection(), "select * from user", new BeanListHandler<User>(User.class));

service中事务的控制

public void zz(String userName1, String userName2, float addmoney) {
        try{
            transactionManager.beginTransaction();
            User user1 = dao.findUserByName(userName1);
            User user2 = dao.findUserByName(userName2);
            user1.setMoney(user1.getMoney()-addmoney);
            dao.updateUser(user1);
            user2.setMoney(user2.getMoney()+addmoney);
//            int i = 1/0;
            dao.updateUser(user2);
            transactionManager.commit();
        }catch(Exception e){
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            transactionManager.close();
        }
    }

具体代码流程,
需要两个工具类ConnectionUtils(负责从线程中获取绑定的连接,如果没有就创建一个连接放入线程中),TransactionManager(线程中的Connection的事务进行封装)

/**
 * 获取连接的工具类
 * 从线程上获取一个连接
 * 如果线程中没有连接,就从数据源中拿,并且绑定到线程中
 */
@Component
public class ConnectionUtils {
    ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    @Autowired
    private DataSource dataSource;
    /**
     * 从线程中获取连接的方法,
     * 也是将连接与线程绑定
     * @return
     */
    public Connection getThreadConnection(){
        try{
            Connection connection = tl.get();
            if(connection == null){
                connection = dataSource.getConnection();
                tl.set(connection);
            }
            return connection;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    public void removeConnection(){
        tl.remove();
    }
}
@Component
public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;
    public void beginTransaction(){
        try{
            connectionUtils.getThreadConnection().setAutoCommit(false);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    public void commit(){
        try {
            connectionUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public void rollback(){
        try {
            connectionUtils.getThreadConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void close(){
        try {
            connectionUtils.getThreadConnection().close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

业务流程

  1. service层
    调用transactionManager.beginTransaction();
    时beginTransaction方法调用
    connectionUtils.getThreadConnection().setAutoCommit(false);
    创建一个连接,将连接放入线程中,并将事务设置为手动提交
  2. dao层
    service在调用dao层的时候,dao层执行sql语句的时候也会在线程中获取连接Connection,此时获取的时service层绑定的连接。
    connectionUtils.getThreadConnection();
    这个方法的作用就是,第一次调用时看看线程中有没有Connection,没有的话就从连接池中获取一个连接,然后绑定到线程中
  3. 如果程序发生异常的话在service层中就应该回滚事务,

新的问题,当我们把事务交给service后,出现的问题。

几乎每一个方法都有重复代码

public List<User> findAll() {
        System.out.println("----------------UserServiceImpl-----------------");
        List<User> all;
        try{
            transactionManager.beginTransaction();//1.
            all = dao.findAll();
            transactionManager.commit();//2.
            return all;
        }catch(Exception e){
            transactionManager.rollback();//3.
            throw new RuntimeException(e);
        }finally {
            transactionManager.close();//4.
        }
    }

将代码抽取,使用动态代理技术对方法进行增强。
使用代理对象去创建service

@Component("beanFactory")
public class BeanFactory {
    @Autowired
    private IUserService userService;
    @Autowired
    private TransactionManager transactionManager;
    public IUserService getUserSerivce(){

        return (IUserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object o;
                try{
                    transactionManager.beginTransaction();
                    o = method.invoke(userService, args);
                    transactionManager.commit();
                    return o;
                }catch (Exception e){
                    transactionManager.rollback();
                    throw new RuntimeException(e);
                }finally {
                    transactionManager.close();
                }
            }
        });
    }
}

代理后我们的service又回到了最开始的状态

public List<User> findAll() {
        System.out.println("----------------UserServiceImpl-----------------");
        return dao.findAll();
    }
YOLOv5入门实践(2)——手把手教你利用labelimg标注数据集 YOLOv5入门实践第2步,手把手教你使用labelimg标注数据集 阅读详情

相关推荐

领域驱动设计整理——实体和值对象设计

实体引言在领域驱动设计里,实体的设计可以说是通用语言的核心,也是最开始在模型划分中需要考虑的。怎么样设计实体和怎么样划分限界上下文同样重要。实体的概念就是要保证通用语言的完整性。领域驱动让设计实体的关注点从数据的属性和表的关联转化到了富有行为的领域概念上。 实体是具有可变性的,这是一个和值对象比较明显的区分,也即实体是可以持续得变化,持续得修改,并且具有唯一的标识。在设计实体的时候需要跳出CRUD的

马猴烧酒└(┐卍^o^)卍ドゥルルル~楪詩月「YUZURIHA SHIZUKI」 2万+

AOP解决什么问题?是什么?好处是什么?如何在Spring容器中配置?代理模式是什么?哪些分类?

1. 1.问题: 代码混乱:越来越多的非业务需求(日志和验证等)加入后, 原有的业务方法急剧膨胀. 每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点. 代码分散: 以日志需求为例, 只是为了满足这个单一需求, 就不得不在多个模块(方法)里多次重复相同的日志代码. 如果日志需求发生变化, 必须修改所有模块. 解决: AOP AOP(Aspect Ori...

专注编程和人工智能! 5620

E28 LoRa模块透传 定点传输 RSSI测试与MicroPython应用

测试说明 测试模块数:3 测试模式:配置模式和传输模式 如果没有检测到串口,首先检查是否安装串口驱动CH341SER,下载链接,提取码:vaw2 工作模式 模式(0-3) M2 M1 M0 模式介绍 备注 0 传输模式 1 0 0 串口打开,无线打开,连续透明传输 空速支持手动配置和自适应波特率自动调节;连续模式双方波特率必须一致 1 RSSI 1 0 1 串口打开,无线打开,RSSI功能打开 模块每100ms输出RSSI强度值 2 测距模式 1 1 0 保留 - 3 配置模式

GniLAY1022的博客 4253

Spring之AOP系列--使用场景/原理

本文介绍Spring AOP的使用场景、原理、配置、使用方式。

IT利刃出鞘的博客 1万+

日前日内多阶段多时间尺度源荷储协调调度(matlab代码)

多阶段多时间尺度的协调调度的优势是考虑新能源出力的波 动性与随机性,需求响应负荷的不确定性会对电网制定的日前调度计划准确性造成影响,也就是能够更加精准的进行调度和分析,优化结果的可用性更强。在这方面论文里面,金力的《考虑特性分布的储能电站接入的电网多时间尺度源储荷协调调度策略》就考虑了日前日内实时三个阶段调度,每个阶段考虑了不同负荷需求响应策略,同时考虑了负荷波动性的影响,具有很强的参考意义。 目录 一、模型 二、程序运行 三、程序链接 一、模型 负荷需求响应考虑电价型和激励型两种,电.

zhangxd212489的博客 7920

ESP32-C3入门教程 WiFi篇①——WiFi Scan 扫描附近AP无线热点 | 堵塞等待扫描结果,回调事件通知扫描结果

文章目录一、快速运行二、运行效果三、程序流畅四、关键函数4.1 扫描所有有效的AP4.2 获取上次扫描中找到的AP列表4.3 获取上次扫描中找到的AP数 一、快速运行 示例项目中,选择WiFi—>scan menuconfig配置ESP32C3-Specific—>Rec 0 芯片选择ESP32-C3(Built-in USB JTAG) 快速运行ESP-IDF Build, Flash and Monitor(左下角) 二、运行效果 三、程序流畅 esp_netif_init,初始

5420

AOP介绍

AOP(Action oriented programming),面向切面编程,它的作用是可以让我们“专心做事”,专心处理业务逻辑代码。不用关心:日志记录、事务控制及权限控制等。问题一:Java EE程序员在编写具体的业务逻辑处理方法时,只需关心核心的业务逻辑处理,既提高了工作效率,又使代码变更简洁优雅。 问题二:在日后的维护中由于业务逻辑代码与其它共有代码分开存放,而且共有代码是集中存放,从而使维护工作变得简单轻松。切面其实就是共有功能的实现,如日志切面、权限切面、事务切面等。在实际应用中通常是一个存放共

weixin_54877075的博客 1223

Google Earth Engine(GEE)——使用归一化燃烧指数 (NBR) 绘制地表烧伤严重程度图

归一化燃烧指数(Normalized Burn Ratio, NBR)是一种遥感指标,用于评估野火或烧伤地面后的植被恢复情况。NBR能增强较大范围的火灾区域,如大于200公顷。它是通过比较远红外波段 (SWIR) 和近红外波段 (NIR) 的反射率来计算的。SWIR波段测量烧伤地面的反射率,NIR波段测量没有受到烧伤影响的植被的反射率。NBR的计算公式为:NBR =(NIR-SWIR)/(NIR + SWIR)。其中,NIR为近红外波段,SWIR为短波红外波段。

此星光明博客 3483

【Spring】之AOP详解

对AOP进行一个详细的介绍,并对起代理模式进行阐述,最后对SpringAOP的源码进行分析。

weixin_73060900的博客 1198

【华为OD机试真题 新系统】1009、企业内部门的最大层级 | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS)

【华为OD机试真题 新系统】1009、企业内部门的最大层级 | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2026e卷,od机试,2026E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,2026华为od,如何备战

KJ.JK 1435

微信小程序 - 最新详细实现用户保存下载视频到手机相册,将视频临时路径、在线播放地址(网络路径)URL存储到手机,弹出访问手机相册权限询问,支持视频格式mp4、avi、mov、wmv、flv、webm

微信小程序,视频,手机相册,保存,另存为,小程序保存到相册权限,writePhotosAlbum,一键保存视频到手机相册功能(视频来源为链接),第一次要获取用户授权,授权使用相册权限,小程序视频下载,如何把视频下载到相册,下载一个视频到本地相册,显示下载进度,可取消下载,视频下载到本地,微信小程序视频如何下载,保存视频到相机,页面播放视频并下载到本地,安卓系统,苹果ios保存到相册报错,权限拒绝后打不开,添加相册授权弹框拒绝后再也出不来了,实现保存视频到手机相册功能,将mp4在线视频下载并存储到用户的手机中

🏆 前端领域优质创作者 7644

Spring的AOP的主要作用和具体应用场景

总的来说,AOP在Spring中的应用,能够带来更好的代码结构、更高的代码复用性和更方便的维护方式,使开发人员能够更专注于业务逻辑,而将横切关注点抽象出来交给AOP来管理。

qq_46130027的博客 1024

C++ PCL点云处理实战专栏

工业级点云处理技术学习资源,涵盖电力、建筑、隧道等领域的实战案例。内容分为三阶段:代码原理(算法认知)、动态库(项目验证)和完整代码(定制开发)。

weixin_45196569的博客 1134

基于SpringAOP解决若伊框架分页失效问题

最近由于项目需要,通过改造ruoyi来满足项目需求,但是测试的是否发现系统自带的分页功能失效,网上也有相应的解决教程,问题虽然可以解决但没办法满足生产需求。于是利用spring的动态代理、AOP和注解对业务代码进行增强,以达到分页效果,灵感一部分来自某个帖子,后面会将帖子的链接附上。时间匆忙,抽个半小时总结下,顺便对Spring加深了解。上述定义一个分页注解,将方法添加该注解后springAOP会对方法进行处理。通过上述代码增强,AfterReturning是对返回结果进行业务逻辑处理,比如分页等。

Steverovo的博客 756

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

用springAOP解决权限问题

1.Service层 public interface PersonService { void savePerson(); void updatePerson(); } public class PersonServiceImpl implements PersonService { @PrivilegeInfo(name="savePerson") public void sav

weixin_38104426的博客 905

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

springAop切面(aspect)失效的问题解决

springAop切面(aspect)失效的问题解决

qq_19891197的博客 2339

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

SpringAop日志找不到方法问题解决

错误截图: 显示没有找到该方法,于是我找到对应的类和对应的方法: 这里我用了反射来获取方法名和参数: 错误打印的结果显示方法名获取没有错误,于是我查看参数的类型是否有错 结果一个都对不上… int类型反射得到的class: Integer反射得到的Class: …终于知道之前错误里的Ljavexxxx是哪里来的了… 由于model是一个接口 model反射的Class得到的是他的子...

m0_47333020的博客 548

SpringAOP切面方法执行两次的问题,及解决办法。

今天学习了Spring的aop相关内容,在学习中我是先使用的配置文件的方式配置切面和切入点。一开始没有任何问题,后来再使用注解的方式配置的时候就出大问题了。 如图“开始1”和“结束”字样都是我的切面方法中打印的,“正在添加”是目标方法打印的,也就是说我的切面方法执行了两次。 这下让我彻底蒙了,我查了所有配置都没有问题。 这是我的切面类 import org.aspectj.lang....

zongzi_13595345262的博客 6749

SpringAop源码(七)- 嵌套调用问题分析与解决

目录 一、本类嵌套Aop问题分析 二、本类嵌套Aop问题解决 1、依赖注入自己 2、BeanFactoryAware(或者ApplicationContextAware) 3、exposeProxy设置为true 之前分析了整个Aop的实现原理和流程。Spring框架的Transactional,Async等都是基于Spring Aop进行实现的。那么很多时候我们在同一个Bea...

it_lihongmin的博客 604

利用SpringAOP + 注解解析方法参数,并解决方法参数不能为Null的问题

利用SpringAOP + 注解方式解析方法参数,并解决方法参数不能为Null的问题,以及SpringAOP的使用注意事项

Equent的博客 2808

SpringAOP是啥,怎么用?

SpringAOP是啥,怎么用? 一、面试问题:什么是SpringAOP? SpringAOP的全称是(Aspect Oriented Programming)中文翻译过来是面向切面编程,个人理解:主要意思是我们的传统架构至上而下,后期想要从中间切入写代码添加功能,比如日志、权限、事务等,这时候我们就需要写使用代理模式写,而springaop就是专门把这一块写代码的地方给抽象了出来称为切面,从而解决的代码混乱的问题,它的底层实现也是基于代理模式(动态代理)完成的。 二、SpringAOP的应用场景有

Object_de_Father的博客 19万+

完整剖析SpringAOP的自调用

摘要 spring全家桶帮助java web开发者节省了很多开发量,提升了效率。但是因为屏蔽了很多细节,导致很多开发者只知其然,不知其所以然,本文就是分析下使用spring的一些注解,不能够自调用的问题。因为本身这类文章很多,所以有些地方不会详述,直接引用其他文章。 问题 使用了Spring中哪些注解不能进行自调用 为什么代理了就不能自调用 Spring常用的 @Cache, @Async,@T...

方丈的寺院 1087

springAOP advice方法执行两次 解决方法

项目中遇到某个Aspect类中全部advice方法都会执行两次的情况,现在问题记录,供大家参考; 首先将问题所在标出来讲述问题, @Aspect @Component public class AopTest {     @Pointcut("execution(** com.bean.Person.eat(..) )")     public void eat(){}     

编程那件小事儿 5546

SpringAOP不生效的情况

在使用SpringAOP的过程中不知道大家有没有遇到这样一个问题,就是我对一个方法做了AOP的增强,但是在内部方法的调用过程中,却发现它并没有按照预期设定的那样执行AOP的增强。 下面我通过一个例子来说明这种情况。 首先先在数据库中建一个表,这个表只有两列,如图。 然后写一个DataService类,在这个类中,主要实现一个insert方法,在insert方法上加了一个@Transactio...

linovce的博客 3395

SpringBoot——解决Cache缓存同类中调用失败问题

这份文档从构建一个键值数据库的关键架构入手,不仅带你建立起全局观,还帮你迅速抓住核心主线。除此之外,还会具体讲解数据结构、线程模型、网络框架、持久化、主从同步和切片集群等,帮你搞懂底层原理。相信这对于所有层次的Redis使用者都是一份非常完美的教程了。你的支持,我的动力;祝各位前程似锦,offer不断!!!数据结构、线程模型、网络框架、持久化、主从同步和切片集群等,帮你搞懂底层原理。相信这对于所有层次的Redis使用者都是一份非常完美的教程了。

2401_83642079的博客 1166

SpringAOP中,使用this调用方法的问题

SpringAOP实际上会自动为我们创建一个Proxy,使得调用者能无感知地调用指定方法,本质上就是一个动态代理。我们只有访问这些代理对象的方法,才能获得AOP实现的功能,所以通过this引用是无法去正确使用 AOP 功能的。

qq_30436011的博客 537
上一篇: spring注解
下一篇: 动态代理技术
RestartJava
博客等级 码龄7年 1粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址