欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Spring depends-on的使用詳解

 更新時(shí)間:2021年07月22日 17:16:36   作者:王哲曉  
這篇文章主要介紹了Spring depends-on的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Spring depends-on的使用

通過(guò)在XML中的<bean>里配置depends-on屬性或者在一個(gè)類(lèi)上使用注解@DependsOn,可以使一個(gè)Bean的產(chǎn)生依賴(lài)于其他幾個(gè)Bean。

請(qǐng)看如下代碼:

<?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這個(gè)Bean的產(chǎn)生依賴(lài)于mother這個(gè)Bean。

Spring Depends-On 不起作用

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" 則不能獲得這個(gè)bean 說(shuō)明它只是一個(gè)模板,只能被繼承 -->
    <bean id = "address" class="com.yuxishua.autowire.Address" p:city="Beijing" p:street="ChangAnLu" abstract="true">
        
    </bean>
<!--     繼承bean的配置使用parent 屬性,但是沒(méi)有java 繼承的意思 -->
    <bean id = "address2" parent="address" p:street="HanSenLU">
        
    </bean>
<!--     要求person bean 必須有一個(gè)關(guān)聯(lián)的car ,意思就是說(shuō)這個(gè)bean依賴(lài)car這個(gè)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;
    }
}

就上面的代碼,結(jié)果輸出

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

car 為什么沒(méi)有注入呢,是spring版本的問(wèn)題嗎?還是什么原因?

spring為4.0.8

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java 中程序內(nèi)存的分析

    詳解Java 中程序內(nèi)存的分析

    這篇文章主要介紹了詳解Java 中程序內(nèi)存的分析的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring中@Autowired與@Resource的區(qū)別詳析

    Spring中@Autowired與@Resource的區(qū)別詳析

    @Autowired與@Resource都可以用來(lái)裝配bean,都可以寫(xiě)在字段上,或?qū)懺趕etter方法上,下面這篇文章主要給大家介紹了關(guān)于Spring中@Autowired與@Resource區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 以Java代碼的方式總結(jié)幾個(gè)典型的內(nèi)存溢出案例

    以Java代碼的方式總結(jié)幾個(gè)典型的內(nèi)存溢出案例

    作為程序員,多多少少都會(huì)遇到一些內(nèi)存溢出的場(chǎng)景,如果你還沒(méi)遇到,說(shuō)明你工作的年限可能比較短,或者你根本就是個(gè)假程序員!哈哈,開(kāi)個(gè)玩笑.今天分享給大家Java內(nèi)存溢出的相關(guān)案例,希望大家在日常工作中,盡量避免寫(xiě)這些low水平的代碼,需要的朋友可以參考下
    2021-06-06
  • Java?Stream?API詳解與使用示例詳解

    Java?Stream?API詳解與使用示例詳解

    Java?Stream?API?是一個(gè)功能強(qiáng)大的工具,適用于處理集合和數(shù)據(jù)流,本文全面介紹了?Java?Stream?API?的概念、功能以及如何在?Java?中有效地使用它進(jìn)行集合和數(shù)據(jù)流的處理,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Java幾個(gè)實(shí)例帶你進(jìn)階升華上篇

    Java幾個(gè)實(shí)例帶你進(jìn)階升華上篇

    與其明天開(kāi)始,不如現(xiàn)在行動(dòng),本文為你帶來(lái)幾個(gè)Java書(shū)寫(xiě)的實(shí)際案例,對(duì)鞏固編程的基礎(chǔ)能力很有幫助,快來(lái)一起往下看看吧
    2022-03-03
  • Spring AOP詳解面向切面編程思想

    Spring AOP詳解面向切面編程思想

    Spring是一個(gè)廣泛應(yīng)用的框架,SpringAOP則是Spring提供的一個(gè)標(biāo)準(zhǔn)易用的aop框架,依托Spring的IOC容器,提供了極強(qiáng)的AOP擴(kuò)展增強(qiáng)能力,對(duì)項(xiàng)目開(kāi)發(fā)提供了極大地便利
    2022-06-06
  • java?Date和SimpleDateFormat時(shí)間類(lèi)詳解

    java?Date和SimpleDateFormat時(shí)間類(lèi)詳解

    這篇文章主要介紹了java?Date和SimpleDateFormat時(shí)間類(lèi)詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 解決JavaWeb讀取本地json文件以及亂碼的問(wèn)題

    解決JavaWeb讀取本地json文件以及亂碼的問(wèn)題

    今天小編就為大家分享一篇解決JavaWeb讀取本地json文件以及亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • 詳解如何在Spring Boot中實(shí)現(xiàn)容錯(cuò)機(jī)制

    詳解如何在Spring Boot中實(shí)現(xiàn)容錯(cuò)機(jī)制

    容錯(cuò)機(jī)制是構(gòu)建健壯和可靠的應(yīng)用程序的重要組成部分,它可以幫助應(yīng)用程序在面對(duì)異?;蚬收蠒r(shí)保持穩(wěn)定運(yùn)行,Spring Boot提供了多種機(jī)制來(lái)實(shí)現(xiàn)容錯(cuò),包括異常處理、斷路器、重試和降級(jí)等,本文將介紹如何在Spring Boot中實(shí)現(xiàn)這些容錯(cuò)機(jī)制,需要的朋友可以參考下
    2023-10-10
  • java中String.matches方法使用

    java中String.matches方法使用

    String.matches()方法用于檢測(cè)字符串是否符合特定的正則表達(dá)式,詳細(xì)介紹了如何使用String.matches()配合不同的正則表達(dá)式來(lái)匹配各種特定格式的字符串,感興趣的可以了解一下
    2024-09-09

最新評(píng)論