Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the problem of spring inheritance

Shulou Source: shulou.com Published: 2022-06-03 10:53:56 10月04日 Update

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.

Tags: Subclasses annotations results instances tests configurations problems code containers objects files help assembly output clarity no use content this properties articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi vpn Docker Microsoft