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 invalid instantiation bean in spring

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

Share

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

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!

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