基于Spring depends-on的使用详解

时间:2021-07-08 01:31:23

Spring depends-on的使用

通过在XML中的<bean>里配置depends-on属性或者在一个类上使用注解@DependsOn,可以使一个Bean的产生依赖于其他几个Bean。

请看如下代码:

?
1
2
3
4
5
6
7
<?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>

son这个Bean的产生依赖于mother这个Bean。

Spring Depends-On 不起作用

beans-realation.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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" 则不能获得这个bean 说明它只是一个模板,只能被继承 -->
    <bean id = "address" class="com.yuxishua.autowire.Address" p:city="Beijing" p:street="ChangAnLu" abstract="true">
        
    </bean>
<!--     继承bean的配置使用parent 属性,但是没有java 继承的意思 -->
    <bean id = "address2" parent="address" p:street="HanSenLU">
        
    </bean>
<!--     要求person bean 必须有一个关联的car ,意思就是说这个bean依赖car这个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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
        
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 + "]";
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
    }
}

就上面的代码,结果输出

Person [name=Tom, address=Address [city=Beijing, street=HanSenLU], car=null]

car 为什么没有注入呢,是spring版本的问题吗?还是什么原因?

spring为4.0.8

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qushaming/article/details/102605829