Inheritance and abstract code examples of beans in spring
When we apply spring, we must use abstract classes in general design. How to configure these abstract beans in spring. See below:
If the configuration information between two beans is very similar, inheritance can be used to reduce duplicate configuration.
Inheritance means that the child bean definition can inherit some configuration information from the parent bean definition, overwrite specific configuration information, or add some configurations. Using inherited configuration can save a lot of configuration work. In practical application, the general configuration will be configured as a template for child beans to inherit.
Use abstract attribute
As mentioned earlier, the general configuration will be configured as a template, and the template does not need to be instantiated. It is only used as the template defined by the sub bean. ApplicationContext pre initializes all singleton beans by default. Using the abstract attribute, you can prevent the template bean from being pre initialized. A bean whose Abstract attribute is true is called an abstract bean. The container ignores all abstract bean definitions and does not initialize the abstract bean during pre initialization. If the abstract attribute is not defined, it defaults to false. The following configuration file defines an abstract bean, which is used as a template:
It can be seen from the configuration file that the definition of abstract bean is almost the same as that of ordinary bean. Only the abstract attribute is added to true, but the execution result of the main program is significantly different. The following main program uses ApplicationContext as the spring container. ApplicationContext pre initializes all singleton beans by default. The main program is as follows:
The program execution results are as follows:
Spring instantiation dependent bean: SteelAxe instance
The container does not instantiate the chinesetemplate bean and ignores all beanos declared as abstract. If the abstract attribute definition is cancelled, the program execution results are as follows:
Spring instantiation dependent bean: SteelAxe real ~ J
Spring instantiates the calling bean: Chinese instance
Spring performs dependency injection
It can be seen that an abstract bean is a bean template. The container will ignore the definition of the abstract bean, so it will not instantiate the abstract bean. However, abstract beans do not need to be instantiated, so they can have no class attribute. The following configuration files are also valid:
Note: an abstract bean cannot be instantiated. You can neither obtain an abstract bean through getBean nor let the ref attribute value of other beans point to an abstract bean. Therefore, any attempt to instantiate an abstract bean will lead to an error.
Define sub bean
We call the bean that specifies the value of the parent attribute as a sub bean; The parent refers to the template of the child bean, which is called the parent bean. The child bean can inherit the implementation class, constructor parameters and attribute values from the parent bean, or add new values. If the properties of init method, destroy method and factory method are specified, they will override the definition of the parent bean. The child bean cannot inherit the following properties from the parent bean: dependencies on, autowire, dependency check, singleton, lazy init. These properties will be obtained from the sub bean definition or take default values. The child bean is defined by setting the parent attribute, and the parent attribute value is the parent bean ID. Modify the above configuration file as follows and add the sub bean definition:
The definition of a sub bean is not much different from that of an ordinary bean, only the parent attribute is added. A child bean can have no class attribute. If there is a class attribute in the parent bean definition, its class attribute can be omitted from the child bean definition, but the child bean will adopt the same implementation class as the parent bean.
The test procedure is modified as follows:
The program execution results are as follows:
Spring instantiation dependent bean: stee1axe instance
Spring instantiates the calling bean: Chinese instance
Spring performs dependency injection
How fast the steel axe cuts firewood
In addition, the child bean inherits the implementation class from the parent bean definition and depends on the bean. However, the child bean can also override the definition of the parent bean. See the following configuration file:
Chinese subclasses are as follows:
At this time, the dependency of the child bean is no longer the dependency defined by the parent bean. Note that the parent class Lee Chinese cannot be an abstract class. (Note: Abstract = "true") does not necessarily mean that this class must be an abstract class. If it is not an abstract class, it can also be defined as an abstract bean in spring. If your class is an abstract class, you cannot use the class of the parent bean at this time. You must define a class in the child bean to initialize the child bean.)
The test procedure is modified as follows:
According to the above test procedure, the results are as follows:
Spring instantiation dependent bean: SteelAxe instance
Spring instantiation dependent bean: stoneaxe instance
Spring instantiates the calling bean: Chinese instance
Spring performs dependency injection
It's so slow to chop firewood with a stone axe
Note: the child bean definitions in the above example do not have class attribute, because there is class attribute in the parent bean definition, and the class attribute of the child bean can be inherited from the parent bean definition. However, it should be noted that when inheriting class from the parent bean, the parent bean must not be an abstract class, because the abstract class cannot create an instance; If the class attribute is not specified in the parent bean definition, the class attribute must be specified in the child bean definition, otherwise an error will occur; If the parent bean definition specifies the class attribute and the child bean definition also specifies the class attribute, the child bean will override the class attribute of the parent bean definition.
The inheritance of beans in spring is quite different from that in Java. The former is the continuation of parameters between instances, and the latter is a general to special refinement. The former is the relationship between objects, and the latter is the relationship between classes.
a. The child bean and parent bean in spring can be of different types, but in Java inheritance, the child class is a special parent class;
b. Bean inheritance in spring is the relationship between instances, which is mainly reflected in the continuation of parameters, while inheritance in Java is the relationship between classes, which is mainly reflected in the continuation of methods and attributes.
c. Spring sub beans can not be used as parent beans and are not polymorphic. Subclass instances in Java can be used as parent instances.
summary
That's all for the inheritance and abstract code examples of beans in spring. I hope it will be helpful to you. If you are interested, you can refer to other topics on this site. Thank you for your support!