Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the invalid instantiation bean in spring

Shulou Source: shulou.com Published: 2022-06-01 12:16:32 09月15日 Update

This article mainly explains "how to solve the instantiation of bean in spring". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the instantiation of bean in spring".

Invalid instantiation bean in spring

Encountered invalid Spring instantiation during Struts2 and Spring integration

The code in Action is as follows: public class UserAction extends ActionSupport {@ Resource private UserService userService; public String execute () {/ / userService.saveUser (new Object ()); System.out.println (userService); System.out.println ("struts2spring integration succeeded"); return "success";}} configuration in applicationContext.xml is as follows

When you instantiate UserService through annotations, you always get null. Finally, after searching, we found the reason why Struts2-Spring-plugin.jar was not imported.

Spring instantiated bean order problem, resulting in injection failure

We can manage bean easily through Spring, and we only need to add a note to the class to inject bean, which is called DI. I ran into a small problem today. Let's sum it up.

The problem is as follows: public abstract class TestBean {public String str; public TestBean () {this.str = initStr ();} protected abstract String initStr ();} public class TestSon extends TestBean {@ Resource public String str; @ Override protected String initStr () {return this.str;}}

But it turns out that this str is always null.

Reason

When instantiating TestBean, it is not possible to confirm that str has been instantiated, so the object is created first, and then the value of str is injected. So when the object is created, the object created according to the constructor has not yet injected the value of str, so it can only be null.

Solve

We need to confirm that we assign a value to the str in the parent class after str has been injected, so we need the subclass to implement the interface InitializingBean and afterPropertiesSet ().

Public class TestSon extends TestBean implements InitializingBean {@ Resource public String str; @ Override protected String initStr () {return this.str;} @ Override public void afterPropertiesSet () throws Exception {super.str = this.str;}}

The problem was solved successfully. Successful injection

Thank you for your reading, the above is the content of "how to solve the instantiation of bean in spring". After the study of this article, I believe you have a deeper understanding of how to solve the problem of instantiation of bean in spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Examples problems successes objects situations times learning content reasons comments that is through subclasses that is ideas interfaces articles methods more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL NVidia Microsoft Xiaomi