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

SpringBoot自動裝配之@Enable深入講解

 更新時間:2023年01月16日 09:17:20   作者:不死鳥.亞歷山大.狼崽子  
這篇文章主要介紹了SpringBoot自動裝配之@Enable,SpringBoot中提供了很多Enable開頭的注解,這些注解都是用于動態(tài)啟用某些功能的。而其底層原理是使用@Import注?解導入一些配置類,實現(xiàn)Bean的動態(tài)加載

SpringBoot中提供了很多Enable開頭的注解,這些注解都是用于動態(tài)啟用某些功能的。而其底層原理是使用@Import注 解導入一些配置類,實現(xiàn)Bean的動態(tài)加載。

提問:SpringBoot 工程是否可以直接獲取jar包中定義的Bean?

答:不可以

案例:

兩個子模塊

①子模塊要得到

②子模塊的User類的bean(這里用編號表示)

方法一:使用@ComponentScan掃描com.itheima.springbooyembal包

package com.enable.entity;
public class User {
}
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    public User user(){
        return new User();
    }
}

引入依賴:

        <dependency>
            <groupId>com.enable</groupId>
            <artifactId>springboot-enable-other</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

使用ComponentScan:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@ComponentScan("com.enable.config")
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

測試如下:

方法二:可以使用@Import注解,加載類,這些類都會被Spring創(chuàng)建,并放入IOC容器。

package com.enable.entity;
public class User {
}
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    public User user(){
        return new User();
    }
}

引入依賴

        <dependency>
            <groupId>com.enable</groupId>
            <artifactId>springboot-enable-other</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

使用Import注解

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(UserConfig.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

測試如下:

方法三:對@Import注解進行封裝

自定義@EnableUser注解

package com.enable.config;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}

自定義配置類

package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    public User user(){
        return new User();
    }
}

新建實體類:

package com.enable.entity;
public class User {
}

引入依賴

        <dependency>
            <groupId>com.enable</groupId>
            <artifactId>springboot-enable-other</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

使用自定義的注解

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@EnableUser
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

測試如下:

到此這篇關(guān)于SpringBoot自動裝配之@Enable深入講解的文章就介紹到這了,更多相關(guān)SpringBoot @Enable內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼

    SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼

    這篇文章主要介紹了SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • Spring國際化和Validation詳解

    Spring國際化和Validation詳解

    本文介紹了SpringBoot中國際化和Validation的融合實現(xiàn),包括配置MessageSource和LocalValidatorFactoryBean,以及自定義約束注解和校驗器,通過解析請求頭中的Accept-Language,SpringBoot可以返回不同語言的文本信息
    2024-11-11
  • 詳細介紹使用Java調(diào)用Python的四種方法

    詳細介紹使用Java調(diào)用Python的四種方法

    這篇文章主要給大家介紹了關(guān)于使用Java調(diào)用Python的四種方法,每種方法根據(jù)實際項目需求有其適用場景,其中,推薦使用Runtime.getRuntime()方法,因為它更為簡潔且易于實現(xiàn),需要的朋友可以參考下
    2024-10-10
  • SpringBoot跨域問題的解決方法實例

    SpringBoot跨域問題的解決方法實例

    這篇文章主要給大家介紹了關(guān)于SpringBoot跨域問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • SpringBoot中定時任務@Scheduled的多線程使用詳解

    SpringBoot中定時任務@Scheduled的多線程使用詳解

    這篇文章主要為大家詳細介紹了pring Boot定時任務@Scheduled的多線程原理以及如何加入線程池來處理定時任務,感興趣的可以了解一下
    2023-04-04
  • Java中如何讀取和寫入zip文件問題

    Java中如何讀取和寫入zip文件問題

    這篇文章主要介紹了Java中如何讀取和寫入zip文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • SpringBoot 統(tǒng)一請求返回的實現(xiàn)

    SpringBoot 統(tǒng)一請求返回的實現(xiàn)

    這篇文章主要介紹了SpringBoot 統(tǒng)一請求返回的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Java開發(fā)基礎日期類代碼詳解

    Java開發(fā)基礎日期類代碼詳解

    這篇文章主要介紹了Java開發(fā)基礎日期類的相關(guān)內(nèi)容,代碼通過日期工具類獲取指定月份的星期與日期對應關(guān)系,以及獲取指定月份的所有日期與星期集合等,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • java中Calendar與Date類型互相轉(zhuǎn)換的方法

    java中Calendar與Date類型互相轉(zhuǎn)換的方法

    這篇文章主要介紹了java中Calendar與Date類型互相轉(zhuǎn)換的方法,Calendar與Date類型是我們?nèi)粘i_發(fā)中常用的兩種數(shù)據(jù)類型,它們用于不同的場景,兩者具有不同的方法,接下來通過實例給大家詳解,需要的朋友可以參考下
    2022-09-09
  • JVM性能調(diào)優(yōu)之運行時參數(shù)小結(jié)

    JVM性能調(diào)優(yōu)之運行時參數(shù)小結(jié)

    jvm是java的運行環(huán)境,在jvm中有很多的參數(shù)可以進行設置,本文主要介紹了JVM性能調(diào)優(yōu)之運行時參數(shù)小結(jié),具有一定的參考價值,感興趣的可以了解一下
    2024-04-04

最新評論

闵行区| 巴彦县| 天祝| 定陶县| 东乡族自治县| 辛集市| 新晃| 中阳县| 专栏| 望城县| 金湖县| 商水县| 集安市| 博野县| 江川县| 苍南县| 怀来县| 平泉县| 十堰市| 泗阳县| 什邡市| 永昌县| 黄石市| 庆安县| 阿勒泰市| 香港| 益阳市| 明溪县| 嘉兴市| 新巴尔虎左旗| 石狮市| 辉县市| 遂宁市| 定安县| 增城市| 宜丰县| 剑阁县| 蒙阴县| 达孜县| 徐闻县| 拜泉县|