In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what to do about the failure of Spring parent variable injection. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Spring parent variable injection failed
Yesterday encountered an Action Service injection failure, in other words, it should be said that there is no injection at all, originally a very simple problem, but due to the inheritance of multiple Action in the project, it finally led to this seemingly strange problem.
Write down the process below.
Received a colleague's question, "there is an Action request that has been calling the report pointer, and the service has been caused by null!"
After a preliminary look at the code and configuration, no problems were found. At first, it was suspected that Action did not have a get method, and then it was still invalid. Then step by step to do a variety of variable name replacement, always the same problem, this process has been concerned that the java code does ignore the page request, through the page request found that the real logic of the code is the page request a subclass Action method, and this method calls a method of the parent class, at this time the parent class in the Service has been unable to inject, for the above mentioned requirements In fact, you need to do Spring injection for the subclass as well as the Spring injection for the parent class, so this requires this configuration:
After the above settings, the Action method of the subclass is requested, and when the parent method is called in the subclass method, there will be no problem that the parent class will not be injected.
Spring extracts the parent class through the technique of injecting common attributes into the parent class by XML configuration
When using frameworks such as Spring + Hibernate, or SSH2, it is now very popular to have only one basic DAO in development. Then, after looking at multiple copies of this code, a DAO attribute is declared in each business class, and in the Bean configuration, DAO is injected into each business class separately. Examples of specific situations are as follows:
BaseDAO Code:
Public class BaseDAO {public String service () {return "Success!";}}
Services Code:
/ / the first business class public class ServiceA {public String service () {return baseDAO.service ();} protected BaseDAO baseDAO; public void setBaseDAO (BaseDAO baseDAO) {this.baseDAO = baseDAO;}} / / the second business class public class ServiceB {public String service () {return baseDAO.service ();} protected BaseDAO baseDAO; public void setBaseDAO (BaseDAO baseDAO) {this.baseDAO = baseDAO;}}
The Bean configuration for Spring is as follows:
This practice is now the mainstream. This is not to say that there is something wrong, or the old saying: this must not be beautiful, who makes people sometimes single-minded?
The way you can think of is to use a parent class to contain some business logic and attributes common to the business layer. So you can put the above code and configuration.
The Services code is rewritten as follows:
/ / public class BaseService {protected BaseDAO baseDAO; public void setBaseDAO (BaseDAO baseDAO) {this.baseDAO = baseDAO;}} / / the first business class public class ServiceA extends BaseService {public String service () {return baseDAO.service ();} / / the second business class public class ServiceB extends BaseService {public String service () {return baseDAO.service ();}}
The Bean configuration of Spring is rewritten as follows:
Is this not a lot more concise? Especially when the actual project has too much Bean. Then, the result we expected will not be achieved here, because there will be the following error:
Exception:
Org.springframework.web.util.NestedServletException: Request processing failed; nested exception is
Java.lang.NullPointerException
.
Root cause:
Java.lang.NullPointerException:.
The error code is the line of code that calls baseDAO in each business. This means that the injection failed. After going through the Bean injection details of Spring, you can quickly find the parent property that should be set for the subclass Bean configuration. So you can change the settings here.
The Bean configuration of Spring is rewritten as follows:
If you run it again at this time, you won't make a mistake. The principle is that in the Bean configuration of a subclass of Spring, the function of the parent attribute is to specify its parent class and inherit the injection property of the parent class. Not only that, the subclass can also modify or override the property value of the parent class. For example, the subclass in the above code modifies the baseDAO to the attribute of the parent class:
For collection properties such as List of the parent class, the subclass can inherit the value of the parent class and add a new value based on it:
Extract the parent class in listValue1 listValue2 listValue3 listValue4 Annotation mode
The above method is configured in the XML configuration file. Annotation, which is now popular in Spring3, is actually more convenient. A complete example is as follows:
BaseDAO Code:
@ Componentpublic class BaseDAO {public String service () {return "Success!";}}
Services Code:
/ / public class BaseService {@ Autowired protected BaseDAO baseDAO;} / / the first business class @ Componentpublic class ServiceA extends BaseService {public String service () {return baseDAO.service ();}} / / the second business class @ Componentpublic class ServiceB extends BaseService {business () {return baseDAO.service ();}}
Action layer code:
Controller@RequestMapping (value = "/ testaction") public class TestAction {@ Autowired private ServiceA service; @ RequestMapping (value = "/") public @ ResponseBody String home (Model model) {return service.service ();}}
There is no need to configure the parent attribute subclass at all, the parent class can be extracted perfectly, and the common properties of the parent class can be used smoothly. As for the principle, did not look at the source code processing, it is estimated that and the above XML configuration is similar, but here to increase the detection of the parent class.
On "Spring parent variable injection failure" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.