Relationship Between Spring notes (8) -------- (bean)
1) Inheritance:
If multiple beans have the same configuration information, Spring allows us to define a parent , Child Will automatically inherit the parent .
As follows:
The configuration of the two beans contains a large amount of duplicate information, which can be effectively eliminated by the parent and child beans:
Both Car1 and Car2 inherit from abstractcar. Spring will pass the configuration information of the parent bean to the Child bean. If the child bean provides the existing configuration information of the parent bean, then the child bean will overwrite the parent bean's
The function of the parent bean is mainly to simplify the configuration of the Child bean. Therefore, it is declared as abstract = "true", indicating that If you do not specify this attribute as true, the IOC container will instantiate a Bean named abstractcar.
2) Dependency
General Usage You can create a bean dependency on other beans. Spring is responsible for managing the relationship between these beans. when instantiating a bean, Spring ensures that the other beans on which the Bean depends have been initialized.
Here is a classic and special example:
For example, a system has a SystemSetting class,
public class SystemSetting {public static int SESSION_TIMEOUT=30;public static int REFRESH_CYCLE=60;.....}
There are some default values for system parameters.
The system has a management backend that can modify these values and save them to the database. However, there is a SysInit class that obtains values from the data during initialization and modifies the SystemSetting parameter.
public class SysInit {public SysInit(){SystemSetting.SESSION_TIMEOUT=100;SystemSetting.REFRESH_CYCLE=120;}}
Assume that the system has a cache refresh manager. It needs to create a cache refresh scheduled task based on the system parameter SystemSetting. REFRESH_CYCLE:
public class CacheManager {public CacheManager(){Timer timer = new Timer();TimerTask cacheTask = new CacheTask();timer.schedule(cacheTask, 0,SystemSetting.REFRESH_CYCLE);}}
In the above example, CacheManager depends on SystemSetting, and the value of SystemSetting is initialized by SysInit. Although CacheManager does not directly rely on SysInit, logically speaking, cacheManager wants to load and complete system parameter settings in SysInit and then start again to avoid calling true parameter values. If all three beans are configured, how can we ensure that SysInit is initialized before CacheManager?
You can use the depends-on attribute to specify the Bean's pre-dependent Bean. The pre-dependent Bean will be created before the Bean is instantiated:
In this way, the configuration is oh. If the front-end is dependent on multiple beans, you can separate them by commas, spaces, or semicolons.
3) Reference
If a bean references the id attribute value of another bean configuration, you can use the following Configuration:
You can use This is the XML Analyzer of IDE. You can find reference errors during development.