One, in thespringThere are inheritance and dependencies between beans in the IOC container.
It is important to note that this inheritance and dependency refers to the relationship between bean configurations, not to inheritance and dependency between classes in the actual sense, they are not a concept.
Inheritance relationships between beans.
1. The inherited ban is called a parent bean, and beans that inherit from the parent bean are called child beans. A parent bean can be declared abstract by specifying an abstract attribute, and a child bean can specify a reference to the parent bean by specifying a parent attribute.
2. The child bean inherits the configuration from the parent bean, including the bean’s property configuration, and can override the configuration inherited from the parent bean. Such as:
<bean class="com.linuxidc.spring.bean.Employee" id="employee" p:empName="emp01" p:age= "23"/>
<bean class="com.linuxidc.spring.bean.Employee" id="employee2" p:empName="emp02" parent="employee"/>Copy the code
Employee2 inherits the Age attribute of Employee and overrides the empName attribute.
3. If you want the parent bean to be used only as a template, you can set the abstract property of <bean> to true and the IOC container will not instantiate the bean. If obtained directly, an exception BeanIsAbstractException is raised.
4. Not all properties of the parent bean are inherited, and properties like Abstract and Autowire are not.
5. You can also ignore the class property of the parent bean and have the child bean specify its own class, sharing the same property configuration. Such as:
<bean class="com.linuxidc.spring.bean.Employee" id="employee" p:empName="emp01" p:age= "23"/>
<bean class="com.linuxidc.spring.bean.Employee2" id="employee22" p:address="123mutouren" parent="employee"/>Copy the code
Dependencies between beans
1. Specify the pre-dependency of the bean through the depends-on attribute of <bean>. A pre-dependent means that when the IOC instantiates a bean in the configuration file at initialization time, the pre-dependent bean is instantiated before the bean is instantiated.
2. Examples:
/ * *
* @authorsolverpeng * @create 2016-07-18-15:07* /
public class First {
publicFirst() { System.out.println("I'm instantiated! I am the First. ""); }}Copy the code
/ * *
* @authorsolverpeng * @create 2016-07-18-15:08* /
public class Second {
publicSecond() { System.out.println("I'm instantiated! I am the Second"); }}Copy the code
E1: The IP address is not addedDepends -on attribute.
<bean class="com.linuxidc.spring.bean.First" id="first"/>
<bean class="com.linuxidc.spring.bean.Second" id="second"/>Copy the code
Console output:
I’m instantiated! I am the Second
I’m instantiated! I am the First
<bean class="com.linuxidc.spring.bean.Second" id="second"/>
<bean class="com.linuxidc.spring.bean.First" id="first"/>Copy the code
Console output:
I’m instantiated! I am the Second
I’m instantiated! I am the First
Conclusion: In the absence of depends-on, the default IOC container instantiation order is based on the order in which beans are instantiated in the configuration file.
E2: Add the depends-on attribute
<bean class="com.linuxidc.spring.bean.First" id="first" depends-on="second"/>
<bean class="com.linuxidc.spring.bean.Second" id="second"/>Copy the code
Console output:
I’m instantiated! I am the Second
I’m instantiated! I am the First
3. If the lead depends on multiple beans, you can specify multiple dependencies with commas or Spaces. And instantiated in sequence. Such as:
<bean class="com.linuxidc.spring.bean.First" id="first" depends-on="third second"/>
<bean class="com.linuxidc.spring.bean.Second" id="second"/>
<bean class="com.linuxidc.spring.bean.Third" id="third"/>Copy the code
Console output:
I’m instantiated! I am a Third
I’m instantiated! I am the Second
I’m instantiated! I am the First