Bean inheritance and internal bean injection in Java spring framework

Bean definition inherits from bean definition. Bean definition can contain a lot of configuration information, including constructor parameters, attribute values, such as initialization method, static factory method name and other container specific information.

The child bean definition inherits configuration data from the parent definition. The definition of a child can override some values or add others as needed.

The spring bean definition has nothing to do with inheritance, but the concept of inheritance is the same as that of Java classes. You can define a parent bean as a template and other child beans can inherit the required configuration from the parent bean.

When XML based configuration metadata is used, it indicates that a child bean definition uses the parent bean specified by the current attribute as the value of this attribute.

For example, let's use the eclipse IDE, and then follow the steps below to create a spring application:

The following is the "HelloWorld" bean we defined. There are two properties message1 and message2, and the configuration file beans XML. Next, the "helloindia" bean has been defined as a child bean of "HelloWorld" and uses the parent attribute. This sub bean inherits the message2 attribute, overrides the message1 attribute, and introduces one more attribute, message3.

This is HelloWorld Contents of java file:

This is hello India Contents of java file:

Here is mainapp Contents of java file:

After creating the source code and bean configuration file, let's run the application. If all goes well, this will print the following information:

If you see here, we didn't create a "helloindia" bean through message2 at the same time, but it passed because of the inheritance defined by the bean.

Bean definition template: you can create a bean definition template that can be defined by other sub beans without too much effort. When defining a bean definition template, you should not specify a class attribute, and you should specify the following abstract attribute with the true value:

The parent bean cannot instantiate itself because it is incomplete and it is explicitly marked as abstract. When a definition is abstract, it is only used as a pure template bean definition as the parent definition of a child definition.

Inject internal beans as you know, Java internal classes are defined within the scope of other classes. Similarly, internal beans are defined within the scope of other beans. Therefore, the < bean / > element within the < property / > or < constructor Arg / > element is called an internal bean and it is shown below.

For example, we use the eclipse IDE and then create a spring application,

This is texteditor Contents of java file:

Here is another related class file, spellchecker Java content:

Here is mainapp Contents of java file:

The following is the configuration file beans The XML file is configured to inject based on setter, but internal beans are used:

Create the source code and bean configuration file to complete, let's run the application. If all goes well, this will print the following information:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>