Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that mybatis interceptor can't inject spring bean?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to solve the problem that the mybatis interceptor can not be injected into spring bean". In the daily operation, I believe many people have doubts about how to solve the problem that the mybatis interceptor cannot be injected into spring bean. The editor has consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful to answer the doubt that "the mybatis interceptor can not be injected into spring bean". Next, please follow the editor to study!

The company wants to integrate rabbitmq and mybatis interceptor to do a data synchronization function.

There is nothing wrong with most of the links in the integration process, but the mybatis interceptor is encountered.

@ Intercepts (@ Signature (type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) @ Componentpublic class MyBatisInterceptor implements Interceptor

Some dao objects in the persistence layer cannot be injected through annotations, causing the dao to be fetched to a null value.

The purpose of fetching the dao object is to query whether the current code execution environment is a production or test environment.

Because the producer code does not need to be executed in the test environment, this differentiated data is stored in the database.

At first, it was impossible for me to pass @ Autowired because

So I continued to look for ways, so I found a way to use ApplicationContext to get the spring container in an attempt to go astray.

It turns out that each execution of ClassPathXmlApplicationContext will cause the container to reload, but my MyBatisInterceptor with @ Component will also reload, causing the isGrey to empty.

Even caused the RabbitmqConfig class to reload.

This makes the listener easy to reload all the time, and the breakpoint keeps entering. It is useful here that the message listener, which is also a singleton bean object, will also be reloaded.

So it will cause bug to appear during testing, because the system environment information sysEnvInfo only needs to be loaded once.

So ApplicationContext this way is also not feasible, I do not recommend.

After thinking about it, I found that the concrete method of implementing logic in the mybatis interceptor is produceSqlData. I can get the data by using the SpringBeanUtils.getBean method.

RabbitTemplate is defined in the RabbitmqConfig code, and the class RabbitmqConfig is an @ Configuration annotation.

However, the data obtained is obtained through @ Bean, and SpringBeanUtils.getBean cannot get the sqlSessionFactory.

The dao object in this, so directly

An error will also be reported, indicating that sysEnvInfoDao is not defined.

So I thought about it, @ Configuration can be successful in @ Autowired, so why don't I refer to rabbitTemplate and turn sysEnvInfo into a singleton object of bean?

So I configured SysEnvInfo as a singleton bean object like rabbitTemplate, annotated with @ Bean

Get it through SpringBeanUtils.getBean in MyBatisInterceptor, which is feasible in practice. There is no problem of repeatedly loading the container, and you can get spring bean in the interceptor.

At this point, the study on "how to solve the problem that the mybatis interceptor cannot be injected into spring bean" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report