Detailed explanation of using Spring depends on
- 2021-11-02 00:55:37
- OfStack
Use of Spring depends-on
In XML < bean > Configuring the depends-on attributes in or using the annotation @ DependsOn on one class can make the generation of one Bean dependent on several other Bean.
Look at the following code:
<?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="son" class="com.tyyd.lifecallbacks.domain.Son" depends-on="mother"></bean>
<bean id="mother" class="com.tyyd.lifecallbacks.domain.Mother"></bean>
</beans>
The generation of son, Bean, depends on mother, Bean.
Spring Depends-On does not work
beans-realation.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- abstract="true" You cannot get this bean It means that it is just 1 Templates that can only be inherited -->
<bean id = "address" class="com.yuxishua.autowire.Address" p:city="Beijing" p:street="ChangAnLu" abstract="true">
</bean>
<!-- Inheritance bean Use the configuration of parent Property, but there is no java Meaning of inheritance -->
<bean id = "address2" parent="address" p:street="HanSenLU">
</bean>
<!-- Requirements person bean There must be 1 Associative car , It means this bean Dependency car This bean -->
<bean id = "person" depends-on="car" class="com.yuxishua.autowire.Person" p:name="Tom" p:address-ref="address2" >
</bean>
<bean id = "car" class="com.yuxishua.autowire.Car" p:brand="AuDi" p:price="30000">
</bean>
</beans>
package com.yuxishua.beansrelation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yuxishua.autowire.Address;
import com.yuxishua.autowire.Car;
import com.yuxishua.autowire.Person;
public class Main
{
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-realation.xml");
Person person = (Person) ctx.getBean("person");
Address address2 = (Address) ctx.getBean("address2");
Car car = (Car) ctx.getBean("car");
System.out.println(address2 );
System.out.println(person);
System.out.println(car);
}
}
package com.yuxishua.autowire;
public class Person
{
private String name;
private Address address;
private Car car;
@Override
public String toString()
{
return "Person [name=" + name + ", address=" + address + ", car=" + car
+ "]";
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
this.address = address;
}
public Car getCar()
{
return car;
}
public void setCar(Car car)
{
this.car = car;
}
}
package com.yuxishua.autowire;
public class Car
{
private String brand;
private double price;
public String getBrand()
{
return brand;
}
public void setBrand(String brand)
{
this.brand = brand;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
@Override
public String toString()
{
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
package com.yuxishua.autowire;
public class Address
{
private String city;
private String street;
@Override
public String toString()
{
return "Address [city=" + city + ", street=" + 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;
}
}
As far as the above code is concerned, the result is output
Person [name=Tom, address=Address [city=Beijing, street=HanSenLU], car=null]
Why is car not injected? Is it a problem with spring? Or what reason?
spring 4.0. 8