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

一篇文章教帶你了解Java Spring之自動(dòng)裝配

 更新時(shí)間:2021年09月18日 09:24:18   作者:可愛(ài)多一點(diǎn)@  
今天小編就為大家分享一篇關(guān)于Spring中的自動(dòng)裝配,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

在Spring中有三種裝配的方式:

  • 在xml中顯示的配置
  • 在java中顯示配置
  • 隱式的自動(dòng)裝配bean

1.Bean的自動(dòng)裝配

自動(dòng)裝配是Spring滿足bean依賴的一種方式,Spring會(huì)在上下文中自動(dòng)尋找,并自動(dòng)給bean裝配屬性。

1.1 autowire="byName" 實(shí)現(xiàn)自動(dòng)裝配

byname會(huì)自動(dòng)在容器上下文中查找,和自己對(duì)象set方法后面的值對(duì)應(yīng)的bean id。

需要保證所有bean的id唯一,并且這個(gè)bean需要和自動(dòng)注入的屬性的set方法的值一致。

People.java

package org.example;
public class People {
    private Cat cat;
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

cat.java

package org.example;
public class Cat {
    public void shut(){
        System.out.println("喵喵喵……");
    }
}

Dog.java

package org.example;
public class Dog {
    public void shut(){
        System.out.println("汪汪汪……");
    }
}

applicationContext.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="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byName">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

測(cè)試類

package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
    public static void main( String[] args ) {
        //獲取ApplicationContext對(duì)象
        ApplicationContext application=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //通過(guò)ApplicationContext獲得TestHello對(duì)象
        //getBean()方法中的參數(shù)即為配置文件中Bean的id的值
        People people=(People) application.getBean("people");
        people.getCat().shut();
        people.getDog().shut();
    }
}

1.2 autowire="byType" 實(shí)現(xiàn)自動(dòng)裝配

byType:會(huì)自動(dòng)在容器上下文中查找,和自己對(duì)象屬性類型相同的bean。

需要保證所有bean的class唯一,并且這個(gè)bean需要和自動(dò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="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byType">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

2.注解實(shí)現(xiàn)自動(dòng)裝配

JDK1.5支持的注解,Spring2.5就支持注解了。

2.1 配置注解

只需在applicationContext.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:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--開(kāi)啟注解的支持-->
    <context:component-scan base-package="org.example"/>
</beans>

2.2 @Autowired注解

直接在屬性上使用即可,也可以在set方式上使用。使用@Autowired 可以不用編寫(xiě)set方法了,前提是你這個(gè)自動(dòng)裝配的屬性在IOC(Spring)容器中存在,且符合byname。

People.java

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
//set方法可以省略
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.3 @Resource注解

People.java

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class People {
//如果沒(méi)有(name="cat")那么就會(huì)找不到
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cat1" class="org.example.Cat"></bean>
    <bean id="cat2" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.4小結(jié)

@Autowired和@Resource的區(qū)別:

  • 都是用來(lái)自動(dòng)裝配的,都可以放在屬性字段上
  • @Autowired通過(guò)byType的方式實(shí)現(xiàn),而且必須要求這個(gè)對(duì)象存在
  • @Resource默認(rèn)通過(guò)byname的方式實(shí)現(xiàn),如果找不到名字,則通過(guò)byType實(shí)現(xiàn)。如果兩個(gè)都找不到的情況就會(huì)報(bào)錯(cuò)。
  • 執(zhí)行順序不同:@Autowired通過(guò)byType;@Resource默認(rèn)通過(guò)byname的方式實(shí)現(xiàn)。

3.介紹一個(gè)idea中做筆記的小技巧

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • springboot項(xiàng)目中沒(méi)有識(shí)別到y(tǒng)ml文件解決辦法

    springboot項(xiàng)目中沒(méi)有識(shí)別到y(tǒng)ml文件解決辦法

    這篇文章主要給大家介紹了springboot項(xiàng)目中沒(méi)有識(shí)別到y(tǒng)ml文件解決辦法,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • 使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼

    使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼

    這篇文章主要介紹了使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼,主要基于JWT的身份認(rèn)證,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 聊聊在Servlet中怎么上傳文件

    聊聊在Servlet中怎么上傳文件

    很多朋友不清楚在Servlet中怎么上傳文件,談到這個(gè)問(wèn)題,首先需要我們掌握開(kāi)發(fā)servlet的步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-05-05
  • Java ThreadPoolExecutor 線程池的使用介紹

    Java ThreadPoolExecutor 線程池的使用介紹

    Executors 是一個(gè)Java中的工具類. 提供工廠方法來(lái)創(chuàng)建不同類型的線程池,這篇文章主要介紹了Java ThreadPoolExecutor 線程池的使用介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫(kù)

    Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫(kù)

    本篇文章主要介紹了Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫(kù),非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • maven+springboot打成jar包的方法

    maven+springboot打成jar包的方法

    這篇文章主要介紹了maven+springboot打成jar包的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • Spring?Boot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫(kù)流程詳解

    Spring?Boot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫(kù)流程詳解

    在現(xiàn)在的后端開(kāi)發(fā)中,只要是使用關(guān)系型數(shù)據(jù)庫(kù),相信SSM架構(gòu)(Spring?Boot?+?MyBatis)已經(jīng)成為首選,本文就以Spring?Boot?+?MyBatis為例,使用MySQL作為數(shù)據(jù)庫(kù),完成數(shù)據(jù)庫(kù)初始化功能,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-05-05
  • Java使用Condition實(shí)現(xiàn)精準(zhǔn)喚醒線程詳解

    Java使用Condition實(shí)現(xiàn)精準(zhǔn)喚醒線程詳解

    這篇文章主要為大家詳細(xì)介紹了Java如何使用Condition實(shí)現(xiàn)精準(zhǔn)喚醒線程效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-02-02
  • 一文帶你掌握J(rèn)ava?ReentrantLock加解鎖原理

    一文帶你掌握J(rèn)ava?ReentrantLock加解鎖原理

    這篇文章將為大家詳細(xì)介紹一下Java中ReentrantLock?加鎖和釋放鎖的原理,以及和?Synchronized?的對(duì)比。文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助
    2022-12-12
  • Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式

    Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式

    這篇文章主要介紹了Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04

最新評(píng)論