Bean Configuration in Spring (1) - Content Summary
Bean Configuration in Spring (2) - IOC and DI
Bean Configuration in Spring (3) -- Configuration of Beans in Spring's IOC Container (by Full Class Name (Reflection) -- Based on XML Files
Bean Configuration in Spring (4) - Dependency Injection
Bean configuration in Spring (5) - literal value
Bean Configuration in Spring (6) - Referencing Other Beans
Bean configuration in Spring (7) - Detailed injection parameters: null values and cascading attributes
Bean Configuration in Spring (8) - Collection Properties
Bean Configuration in Spring (9) - Bean Automatic Assembly in XML Configuration
Bean Configuration in Spring (10) -- Inheriting Bean Configuration and Dependent Bean Configuration
Bean Configuration in Spring (11) - Bean Scope
Bean Configuration in Spring (12) - Using External Property Files
Bean Configuration in Spring (13) - Spring Expression Language: SpEl
Bean Configuration in Spring (14) - Bean Life Cycle in IOC Containers
Bean Configuration in Spring (15) -- Configuring Beans in Spring's IOC Container (Creating Beans by Factory Method) -- An XML-based approach
Bean Configuration in Spring (16) -- Configuring Beans in Spring's IOC Container (via FactoryBean) -- An XML-based approach
Bean Configuration in Spring (17) -- Bean Configuration in Spring's IOC Container -- Bean Configuration Based on Annotation
-
Spring allows the configuration of inherited beans. The inherited beans are called parent beans. The beans that inherit the parent beans are called child beans.
-
The child Bean inherits the configuration from the parent Bean, including the property configuration of the Bean
-
Son beans can also override configurations inherited from parent beans
-
If you want to use the parent Bean as a template, you can set the abstract attribute of the Bean to true, so that Spring will not instantiate the Bean, but can only be used for inherited configuration.
-
Not all attributes in a bean element are inherited. For example: autowire, abstract, etc.
-
You can also ignore the class attribute of the parent Bean and let the child Bean specify its own class and share the same attribute configuration. But abstract must be set to true at this time.
-
Spring allows users to set Beans with Pre-Dependent Beans by depend-on attributes, and Pre-Dependent Beans are created before this Bean is instantiated.
-
If the prefix depends on multiple beans, you can configure the name of the Bean by comma, space, or way
Code demonstration
Address.java
package com.atguigu.spring.bean; public class Address { private String address; private String location; public void setAddress(String address) { this.address = address; } public void setLocation(String location) { this.location = location; } @Override public String toString() { return "Address [address=" + address + ", location=" + location + "]"; } }
Car.java
package com.atguigu.spring.bean; public class Car { private String name; private String number; public Car(String name, String number) { super(); this.name = name; this.number = number; } @Override public String toString() { return "Car [name=" + name + ", number=" + number + "]"; } }
Person.java
package com.atguigu.spring.bean; public class Person { private Address address; private Car car; public void setAddress(Address address) { this.address = address; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [address=" + address + ", car=" + car + "]"; } }
application.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car1" class="com.atguigu.spring.bean.Car"> <constructor-arg value="baoma"></constructor-arg> <constructor-arg value="Fujian A7878"></constructor-arg> </bean> <bean id="address1" class="com.atguigu.spring.bean.Address"> <property name="address" value="mq"></property> <property name="location" value="fj"></property> </bean> <!-- bean Configuration inheritance: use bean Of parent Property specifies which to inherit bean Value --> <bean id="address2" class="com.atguigu.spring.bean.Address" parent="address1"> <property name="address" value="fz"></property> <property name="location" value="fj"></property> </bean> <!-- abstract bean: bean Of abstract Attribute is true Of bean,In this way bean Can't be ioc Instances can only be used for inherited configurations If one bean Of class If the property has no specified value, the bean Must be an abstraction bean --> <bean id="address3" abstract="true"> <property name="address" value="mq"></property> <property name="location" value="zz"></property> </bean> <bean id="address4" class="com.atguigu.spring.bean.Address" parent="address3"> </bean> <!--Test Requirements Configuration person There must be an association. car!Let me put it another way person this bean Dependence on car this bean --> <bean id="person1" class="com.atguigu.spring.bean.Person" depends-on="car1"> <property name="address" ref="address1"></property> </bean> </beans>
Main.java
package com.atguigu.spring.bean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); Person person1 = (Person) applicationContext.getBean("person1"); Address address4 = (Address) applicationContext.getBean("address4"); Address address2 = (Address) applicationContext.getBean("address2"); System.out.println(person1); System.out.println(address4); System.out.println(address2); } }