In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to understand the problem of spring inheritance, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
The problem of spring inheritance
Why is the output 0?
Because the subclass inherits the parent class, the instance object mainly calls the parent class properties and methods on the left, so the output is dominated by the object on the left.
Spring injects classes with inherited relationships through configuration files through annotations
You only need to annotate the subclass, and no addition on the parent class will automatically inject
Package com.jeremy.spring.genericityDI;public class BaseRepository {}
BaseService:
Package com.jeremy.spring.genericityDI;import org.springframework.beans.factory.annotation.Autowired;public class BaseService {@ Autowired-// tells the IOC container to automatically assemble Bean protected BaseRepository baseRepository;-// with dependencies. Here is the subclass inheritance dependency public void add () {System.out.println ("add.") System.out.println (baseRepository);}} create a new generic class
User:
Package com.jeremy.spring.genericityDI;public class User {} create new subclasses of BaseRepository and BaseService
UserRepository:
Package com.jeremy.spring.genericityDI;import org.springframework.stereotype.Component;@Componentpublic class UserRepository extends BaseRepository {}
UserService:
Package com.jeremy.spring.genericityDI;import org.springframework.stereotype.Service;@Servicepublic class UserService extends BaseService {} configure the automatic assembly of annotated Bean test code and results in the configuration file of Spring
Test the code:
Test public void test () {ApplicationContext actx=new ClassPathXmlApplicationContext ("Bean-genericity-di.xml"); UserService userService= (UserService) actx.getBean ("userService"); userService.add ();}
Test results:
Add.
Com.jeremy.spring.genericityDI.UserRepository@16546ef
From the result, although the subclass does not establish a dependency, the userRepository instance is instantiated, which proves the dependency of the parent class, and the subclass can inherit.
In fact, it can also be explained here that even if the parent class is not managed by the IOC container, the parent class relationship will be inherited if the @ Autowired annotation is added when the relationship is established.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.