Introduction to spring bean inheritance and automatic assembly
Basic concepts of spring bean
As we all know, spring is a large factory, and the bean in the spring container is the product of the factory For those products that the spring container can produce, it depends on the configuration in the configuration file.
For us, what we do with the spring framework is two things: develop beans and configure beans. For spring mining construction, all it needs to do is create a bean instance according to the configuration file and call the method of the bean instance to complete "dependency injection".
Definition of bean
The < beans... / > element is the root element of the spring configuration file, and the < beans... / > element is < beans.. / > The < beans... / > element can contain multiple < bean... / > sub elements. Each < bean... / > element can define a bean instance. Each bean corresponds to a Java instance in the spring container. Two attributes need to be specified when defining a bean.
ID: determines the unique identifier of the bean. The container's management, access and dependency on the bean are all completed through this attribute. The ID attribute of the bean is unique in the spring container.
Class: Specifies the concrete implementation class of the bean. Note that the interface cannot be used here. Usually, spring will directly use the new keyword to create an instance of the bean. Therefore, the class name of the bean implementation class must be provided here.
The following is a simple configuration for defining a bean
Automatic assembly of beans
Byname automatically matches the bean name (ID) and the setter property name of the current bean. If it fails to match, it will not match
Bytype matches the property type of the current bean according to the bean type. If the IOC container has more than one bean matching type, an exception will be thrown
Bean inheritance (parent)
Use the parent attribute to specify the bean to inherit, and you can also modify the inherited attribute value
Abstract bean (template)
A bean whose Abstract attribute is true cannot be initialized by the IOC container and can only be used for inherited configuration
If a bean does not specify a class attribute, it must be an abstract bean
Bean dependencies
Use the dependencies on attribute to specify
If you need to rely on multiple beans, use commas to separate them
Scope of the bean
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.