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 of invalid bean injection in spring and what is the difference between new and creating objects

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 the relevant knowledge of "how to solve invalid bean injection in spring and what is the difference between creating objects in new", Xiaobian shows you the process of operation through actual cases, the method of operation is simple and fast, and practical. I hope that this article "how to solve invalid bean injection in spring and what is the difference between creating objects in new" can help you solve the problem.

The difference between invalid bean injection and new creation object

Be careful! If you directly new, the autowire in the class will not take effect.

The following code is used in the project

Then at run time, it is found that the values of the objects capitalDetailDOMapper and excelRecordDOMapper injected through @ Autowired are null

Public class ExcelListener extends AnalysisEventListener {@ Autowiredprivate CapitalDetailDOMapper capitalDetailDOMapper;@Autowiredprivate ExcelRecordDOMapper excelRecordDOMapper;... }

At first I thought it was caused by ExcelListener without adding bean scan annotations.

So after adding it,

@ Componentpublic class ExcelListener extends AnalysisEventListener {@ Autowiredprivate CapitalDetailDOMapper capitalDetailDOMapper;@Autowiredprivate ExcelRecordDOMapper excelRecordDOMapper;... }

As a result, at run time, two mapper are still null.

Check the code discovery

In the usage scenario of the ExcelListener class, it is the object created by ExcelListener excelListener= new ExcelListener ()

You can see that manually creating a management object through new is not the same as creating a management object through a spring container. Manually new objects are not annotated internally with @ autowire.

If you want the autowire to take effect, you need to manage the object through the container

The modification is as follows, first add the annotation @ Component to ExcelListener

Scope ("prototype") @ Component ("excelListener") public class ExcelListener extends AnalysisEventListener {@ Autowiredprivate CapitalDetailDOMapper capitalDetailDOMapper;@Autowiredprivate ExcelRecordDOMapper excelRecordDOMapper;... }

In this way, the bean will be loaded into the spring container, where Scope represents the scope of bean, and the object created by spring by default is a singleton

Prototype is a non-singleton. Here I mainly want to create different ExcelListener instances in different threads.

You can get bean from the container or directly @ autowire

ExcelListener excelListener= SpringBeanUtils.getBean ("excelListener")

SpringBeanUtils is the implementation class that implements the BeanFactoryPostProcessor interface.

You can get an instance of bean from the spring container. If the bean is a non-singleton, you can get a new bean each time.

Component@Getterpublic class SpringBeanUtils implements BeanFactoryPostProcessor {private static ConfigurableListableBeanFactory beanFactory;public static T getBean (String beanName) {return (T) beanFactory.getBean (beanName);} public static T getBean (Class beanClz) {return beanFactory.getBean (beanClz);} @ Overridepublic void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {SpringBeanUtils.beanFactory = beanFactory;}}

Attention! If class An is singleton, then class B, a member of class A, is declared to be non-singleton, which is also invalid.

Because there is only one instance of class A, there is only one instance of class B. Unless you can create an instance of class An again, you can create another instance object of class B.

Bean, compare the difference between spring and new.

We're going to use DataSource in the spring configuration file, right?

This bean will be referenced by multiple bean through the ref= "dateSource" reference when we want to modify this, just modify the reference, not a lot.

Mainly decoupling

For example, if you have a class An and you want to call the method new of An in several classes, you have to new A () in each class.

For spring, it is enough to configure a bean for injection.

One day you won't need category An and change it to B. Just change the class in bean to B and get it done.

If you use new, you have to change several places.

This is the end of the content about "how to solve the difference between invalid bean injection in spring and creating objects in new". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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