Resolve the relationship between spring beans through an instance
This article mainly introduces how to resolve the relationship between spring beans through examples. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
Two relationships: Inheritance and dependency
1、 Inheritance relationship
Address. java
package com.gong.spring.beans.autowire; public class Address { private String city; private String street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [city=" + city + ",street=" + street + "]"; } }
beans-relation. 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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.gong.spring.beans.autowire.Address" p:city="武汉" p:street="络南街道"></bean> <!-- 使用parent指定指定哪个bean的配置,子Bean可以覆盖父bean的配置 --> <bean id="address2" class="com.gong.spring.beans.autowire.Address" parent="address" p:street="珞狮街道"></bean> </beans>
Main. java
package com.gong.spring.beans.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); //2.从容器中获取Bean实例 Address address = (Address) ctx.getBean("address"); System.out.println(address.toString()); Address address2 = (Address) ctx.getBean("address2"); System.out.println(address2.toString()); } }
Output:
Address2 inherits the city configuration of address, so city = Wuhan.
Of course, we can also use abstract to indicate that a bean is an abstract bean. Abstract beans can be used as a template and cannot be instantiated. Meanwhile, if a bean does not declare a class, the bean is also an abstract bean, and abstract = "true" must be specified.
<bean id="address" class="com.gong.spring.beans.autowire.Address" abstract="true" p:city="武汉" p:street="络南街道"></bean>
In this case, an error will be reported when instantiating
Address address = (Address) ctx.getBean("address");
Using an abstract bean as a parent bean, you can instantiate its child bean:
Address address2 = (Address) ctx.getBean("address2"); System.out.println(address2.toString());
2、 Dependency
Car. java
package com.gong.spring.beans.autowire; public class Car { public Car() { } public Car(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car [name=" + name + "]"; } }
Student. java
package com.gong.spring.beans.autowire; import java.util.List; import java.util.Map; public class Student { private String name; private int age; private double score; private Car car; private Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getscore() { return score; } public void setscore(double score) { this.score = score; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Student [name=" + name + ",age=" + age + ",score=" + score + ",car=" + car + ",address=" + address + "]"; } }
beans-relation. 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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="com.gong.spring.beans.autowire.Car" p:name="baoma"></bean> <bean id="address" class="com.gong.spring.beans.autowire.Address" p:city="武汉" p:street="络南街道"></bean> <!-- 要求配置Student时,要依赖于Car--> <bean id="student" class="com.gong.spring.beans.autowire.Student" p:name="tom" p:age="12" p:score="99.00" autowire="byName" depends-on="car"></bean> </beans>
Spring allows the user to set the bean pre dependency bean through the dependencies on attribute. The pre dependency bean will be created before the bean is instantiated. If the prefix depends on multiple beans, you can configure the bean name by commas and spaces.
Main. java
package com.gong.spring.beans.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); //2.从容器中获取Bean实例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); } }
Output:
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.