Inherited
The inheritance that is said here is not the same as the inheritance of Java, not the subclass of the parent class. But the thought is very similar, is the parent bean and the child bean
1, when the parent bean is an instance. It's itself a complete bean.
2, the parent bean is a template, abstract bean, can not be instantiated, just to be inherited.
When encountering a class to instantiate a lot of similar bean objects, as below, it seems to be not very concise
1 <bean id= "Address1" class= "com.guigu.spring.autowire.Address" 2 p:city= "Beijing" p:street= "Yihuan" ></bean>3 <bean id= "Address2" class= "com.guigu.spring.autowire.Address" 4 p:city= "Beijing" P: street= "Erhuan" ></bean>
Method ① two beans except P:street, the others are the same. You can take one as a parent bean, and the other to inherit the bean.
1 <bean id= "Address1" class= "com.guigu.spring.autowire.Address" 2 p:city= "Beijing" p:street= "Yihuan" ></bean>3 <bean id= "address2" p:street= "Erhuan" parent= "Address1" ></bean>4 // Address2 equivalent to: 5 // <bean id= "Address2" "class=" com.guigu.spring.autowire.Address "6 // p:city=" Beijing "p:street=" Erhuan "></bean>
Here, the second inherits the first Bean, with the parent property, just write your own unique property or the property you want to overwrite.
The ② method extracts the same attributes and makes an abstract bean, which is specifically used to inherit
1 <bean id= "Address1" class= "com.guigu.spring.autowire.Address" 2 p:city= "Beijing" p:street= "Yihuan" abstract = "true" ></bean>3 <bean id= "Address2" p:street= "Erhuan" parent= "Address1" ></bean>
In this way, Address2 also inherited the Address1. Address1 set abstract= "True", you cannot instantiate, or throw an exception.
It can also be written like this:
1 <bean id= "Address1" p:city= "Beijing" p:street= "Yihuan" abstract= "true" ></bean>2 <bean id= " Address2 "class=" com.guigu.spring.autowire.Address "p:street=" Erhuan "parent=" Address1 "></bean>
The difference with the above is to take the class attribute out. When a lot of beans that are not of the same class are going to inherit the parent bean, this is the case when the child Bean writes its own class.
The other thing is that this parent bean must not be instantiated because it does not have a class attribute and the instantiation runs abnormally. Then be sure to write the abstract attribute so that spring does not instantiate it.
Dependent---depends-on
Example with the Person class
1 public class Person {2 private string name, 3 private car car, 4 5 public string GetName () {6 Retu RN name; 7 } 8 public void SetName (String name) {9 this.name = name;10 }11 public Car Getcar () { retur N car;13 }14 public void Setcar (car car) { This.car = car;16 }17 @Override18 Public String toString () { return "person [name=" + name + ", car=" + Car + "]"; }21}
There is a property that car is not configured, and after that instantiation it is null.
1 <bean id= "person" class= "Com.guigu.spring.autowire.Person" 2 p:name= "Evan" ></bean>
Sometimes, we need to make certain attributes exist, just like required fields. Use depends-on to rely on
1 <bean id= "person" class= "Com.guigu.spring.autowire.Person" 2 p:name= "Evan" depends-on= "Car" >< /bean>
This means that instantiating a person must depend on car, not configuring car will throw an exception
1 <bean id= "car" class= "Com.guigu.spring.autowire.Car" p:brand= "Audi" p:price= "200000" ></bean>2 <bean id= "person" class= "Com.guigu.spring.autowire.Person" 3 p:name= "Evan" depends-on= "Car" ></ Bean>
In this way, the dependent car is present and can be instantiated normally.
Relationships between beans: inheritance, dependency