最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一篇文章教帶你了解Java Spring之自動裝配

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

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

  • 在xml中顯示的配置
  • 在java中顯示配置
  • 隱式的自動裝配bean

1.Bean的自動裝配

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

1.1 autowire="byName" 實現(xiàn)自動裝配

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

需要保證所有bean的id唯一,并且這個bean需要和自動注入的屬性的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>

測試類

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

1.2 autowire="byType" 實現(xiàn)自動裝配

byType:會自動在容器上下文中查找,和自己對象屬性類型相同的bean。

需要保證所有bean的class唯一,并且這個bean需要和自動注入的屬性的類型一致。

<?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.注解實現(xiàn)自動裝配

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">
<!--開啟注解的支持-->
    <context:component-scan base-package="org.example"/>
</beans>

2.2 @Autowired注解

直接在屬性上使用即可,也可以在set方式上使用。使用@Autowired 可以不用編寫set方法了,前提是你這個自動裝配的屬性在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 {
//如果沒有(name="cat")那么就會找不到
    @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ū)別:

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

3.介紹一個idea中做筆記的小技巧

總結(jié)

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

相關(guān)文章

  • Java 自定義線程池和線程總數(shù)控制操作

    Java 自定義線程池和線程總數(shù)控制操作

    這篇文章主要介紹了Java 自定義線程池和線程總數(shù)控制操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java單線程ThreadLocal串值問題解決方案

    Java單線程ThreadLocal串值問題解決方案

    這篇文章主要介紹了Java單線程ThreadLocal串值問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • java:程序包org.apache.ibatis.annotations不存在報錯解決

    java:程序包org.apache.ibatis.annotations不存在報錯解決

    這篇文章主要給大家介紹了關(guān)于java:程序包org.apache.ibatis.annotations不存在報錯的解決方法,這個錯誤是我在直接導(dǎo)入springboot項目的時候報錯的,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • java分布式事務(wù)seata的使用方式

    java分布式事務(wù)seata的使用方式

    這篇文章主要介紹了java分布式事務(wù)seata的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • springboot一個自定義注解如何搞定多線程事務(wù)

    springboot一個自定義注解如何搞定多線程事務(wù)

    文章介紹了Spring?Boot中使用`@Async`注解進行聲明式多線程編程的方法,以及如何通過自定義注解和AOP實現(xiàn)多線程事務(wù)控制,同時,還解釋了`CountDownLatch`的使用場景及其工作原理
    2024-12-12
  • Java享元設(shè)計模式優(yōu)化對象創(chuàng)建提高性能和效率

    Java享元設(shè)計模式優(yōu)化對象創(chuàng)建提高性能和效率

    Java享元設(shè)計模式通過共享可重用的對象,減少了系統(tǒng)中對象的數(shù)量,優(yōu)化了對象的創(chuàng)建和管理,提高了性能和效率。它是一種經(jīng)典的設(shè)計模式,適用于需要處理大量相似對象的應(yīng)用程序
    2023-04-04
  • Springboot Caffeine本地緩存使用示例

    Springboot Caffeine本地緩存使用示例

    這篇文章主要介紹了Springboot Caffeine本地緩存使用示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Java8如何通過Lambda處理List集合

    Java8如何通過Lambda處理List集合

    這篇文章主要介紹了java8如何通過Lambda處理List集合,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • 關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄

    關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄

    這篇文章主要介紹了關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中構(gòu)造、生成XML簡明教程

    Java中構(gòu)造、生成XML簡明教程

    這篇文章主要介紹了Java中構(gòu)造、生成XML簡明教程,本文通過dom4j包來完成,需要的朋友可以參考下
    2014-08-08

最新評論

德安县| 永平县| 华池县| 怀集县| 措勤县| 徐汇区| 光山县| 嘉禾县| 上栗县| 文安县| 澳门| 保定市| 湘西| 武汉市| 航空| 闸北区| 遂溪县| 中宁县| 株洲市| 吉隆县| 阳城县| 奉化市| 五大连池市| 平罗县| 江都市| 永泰县| 商洛市| 新疆| 遂平县| 西丰县| 宁国市| 常熟市| 启东市| 汉中市| 南丰县| 静安区| 平原县| 邵东县| 岳普湖县| 湄潭县| 禄劝|