In Spring container, in addition to establishing dependencies through <ref>, there are some special relationships between the two beans.
1 Inheritance
In object-oriented programming principle, when multiple classes have the same methods and properties, the parent class can be introduced to eliminate duplicate code. In Spring container, if multiple beans have the same configuration information, we can define a parent bean, so that the child bean will automatically inherit the configuration information of the parent bean.
<!-- Parent Bean--><bean id="abstractBook" p:name="veil" abstract="true"></bean><!-- Child Bean--><bean id="book1" p:press="Chongqing Publishing House" parent="abstractBook"/><bean id="book2" p:press="Shanghai Translation Publishing House" parent="abstractBook"/>
Generally speaking, the function of the parent bean is to simplify the configuration of the child bean, so it is set to an abstract class (abstract="true"); if the parent bean is not set to an abstract class here, the Spring container will instantiate the parent bean.
2 Pre-dependence
Generally speaking, <ref> is used to establish dependencies between beans. Spring containers are responsible for managing these relationships. When instantiating a bean, the container ensures that all beans depend on to the bean have completed the initialization work.
But in some cases, the dependencies between beans are not that obvious.
Suppose in this scenario, a system sets some system parameters (such as password validity period, whether to enable monitoring, etc.), and these startup parameters are used to control the system's operating logic. We use a Setting class to represent these parameters:
public class Settings { /** * Password expiration time (unit: days) */ public static int PASS_TIMEOUT = 30; /** * Whether to enable monitoring*/ public static boolean IS_MONITOR = false;}Here we set default values for these parameters. The system also has an administrative backend, through which administrators can adjust these system parameters and save them to the database. Therefore, when the application starts, these system parameters need to be loaded from the database:
public class System { public System() { init(); } /** * Initialization*/ private void init() { //Suppose these values come from the database Settings.PASS_TIMEOUT = 20; Settings.IS_MONITOR = true; }}The system has a password expiration manager, which will create a timed task to detect whether the password expires based on the [number of days when the password expires] in the system parameters:
public class PassManager { int timeout; public PassManager() { timeout = Settings.PASS_TIMEOUT; timerTask(); } /** * Timed task to detect whether the password expires*/ private void timerTask() { } public int getTimeout() { return timeout; }}Although PassManager does not rely directly on Settings, logically, PassManager expects System to load initialized system parameters before starting.
In Spring, you can explicitly specify the pre-dependence bean of a bean through the depends-on property to ensure that the pre-dependence bean of this bean has been loaded before instantiation.
<bean id="system"/><bean id="manager" depends-on="system"/>
If the prefix depends on multiple beans, the bean name can be configured by commas, spaces, or semicolons.
3 Quote ID
Suppose a Bean needs to refer to the id value (beanName) of another Bean. This is generally used to obtain another Bean through the getBean(beanName) method in the bean during runtime.
It can be configured like this:
<bean id="author"/><bean id="book" p:authorId="author"/>
Added authorId attribute in Book:
/** * author Bean ID */private String authorId;
Although it can be set in this literal form, there is no real reference relationship between the two. Therefore, the configuration error will be found only when the specific call is called.
Spring provides the <idref> element tag, which references the name of another bean through <idref>. This way, when the container starts, the correctness of the reference relationship will be checked, and incorrect configuration information can be found in advance.
<bean id="author10"/><bean id="book10" > <property name="authorId"> <idref bean="author10"/> </property></bean>
If a configuration error occurs, a BeanDefinitionStoreException will be thrown when the Spring container starts, and the IDE's XML parser will also find reference errors in advance, so it is recommended to use the <idref> element tag to reference the ID.
Summarize
The above is the special relationship between Spring Beans introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!