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

spring中@ComponentScan自動掃描并指定掃描規(guī)則

 更新時間:2023年04月28日 08:32:55   作者:懷夢  
本文主要介紹了spring中@ComponentScan自動掃描并指定掃描規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.使用注解配置包掃描

1.1.創(chuàng)建相關(guān)類

分別創(chuàng)建BookDao、BookService、BookServiceImpl以及BookController這三個類,并在這三個類中分別添加@Repository、@Service、@Controller注解

BookDao

package com.tianxia.springannotation.dao;
import org.springframework.stereotype.Repository;
/**
 * BookDao
 * @author liqb
 * @date 2023-04-21 16:37
 **/
// 名字默認(rèn)是類名首字母小寫
@Repository
public class BookDao {
}

BookService

package com.tianxia.springannotation.service;
/**
 * BookService
 * @author liqb
 * @date 2023-04-21 16:38
 **/
public interface BookService {
}

BookServiceImpl

package com.tianxia.springannotation.service.impl;
import com.tianxia.springannotation.service.BookService;
import org.springframework.stereotype.Service;
/**
 * BookServiceImpl
 * @author liqb
 * @date 2023-04-21 16:38
 **/
@Service
public class BookServiceImpl implements BookService {
}

BookController

package com.tianxia.springannotation.controller;
import org.springframework.stereotype.Controller;
/**
 * BookController
 * @author liqb
 * @date 2023-04-21 16:39
 **/
@Controller
public class BookController {
}

1.2.SpringBoot啟動類默認(rèn)就有配置@ComponentScan

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}

1.3.查看IOC中的bean的名稱

package com.tianxia.springannotation;
import com.tianxia.springannotation.config.MainConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * ComponentScanTest
 * @author liqb
 * @date 2023-04-21 16:45
 **/
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ComponentScanTest {
    /**
     * 查看IOC容器中有哪些bean
     * @author liqb
     * @date 2023-04-21 16:45
     */
    @Test
    public void test() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringAnnotationApplication.class);
        // 我們現(xiàn)在就來看一下IOC容器中有哪些bean,即容器中所有bean定義的名字
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
    }
}

2.掃描時排除注解標(biāo)注的類

在注解類上通過@ComponentScan注解的excludeFilters()方法

package com.tianxia.springannotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
/**
 * 啟動類
 * @author liqb
 * @date 2023-04-21 16:12
 **/
@SpringBootApplication
// value指定要掃描的包
@ComponentScan(value="com.tianxia.springannotation", excludeFilters={
        /*
         * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等
         * classes:除了@Controller和@Service標(biāo)注的組件之外,IOC容器中剩下的組件我都要,即相當(dāng)于是我要排除@Controller和@Service這倆注解標(biāo)注的組件。
         */
        @ComponentScan.Filter(type= FilterType.ANNOTATION, classes={Controller.class, Service.class})
})
public class SpringAnnotationApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringAnnotationApplication.class, args);
    }
}

3.掃描時只包含注解標(biāo)注的類

在注解類中的includeFilters()方法來指定Spring在進(jìn)行包掃描時,只包含哪些注解標(biāo)注的

這里需要注意的是,當(dāng)我們使用includeFilters()方法來指定只包含哪些注解標(biāo)注的類時,需要禁用掉默認(rèn)的過濾規(guī)則

package com.tianxia.springannotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
 * 啟動類
 * @author liqb
 * @date 2023-04-21 16:12
 **/
@SpringBootApplication
// value指定要掃描的包
@ComponentScan(value="com.tianxia.springannotation", includeFilters={
        /*
         * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等
         * classes:我們需要Spring在掃描時,只包含@Controller注解標(biāo)注的類
         */
        @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class})
}, useDefaultFilters=false)
public class SpringAnnotationApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringAnnotationApplication.class, args);
    }
}

4.重復(fù)注解

@ComponentScans({
        @ComponentScan(value="com.tianxia.springannotation", includeFilters={
                /*
                 * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等
                 * classes:我們需要Spring在掃描時,只包含@Controller注解標(biāo)注的類
                 */
                @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class})
        }, useDefaultFilters=false),
        @ComponentScan(value="com.tianxia.springannotation", includeFilters={
                /*
                 * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等
                 * classes:我們需要Spring在掃描時,只包含@Controller注解標(biāo)注的類
                 */
                @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Service.class})
        }, useDefaultFilters=false)
})

到此這篇關(guān)于spring中@ComponentScan自動掃描并指定掃描規(guī)則的文章就介紹到這了,更多相關(guān)spring @ComponentScan自動掃描內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis延遲加載與立即加載案例教程

    MyBatis延遲加載與立即加載案例教程

    這篇文章主要介紹了MyBatis延遲加載與立即加載案例教程,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Java注解使用及原理解析

    Java注解使用及原理解析

    這篇文章主要介紹了Java注解使用及原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Java編寫實(shí)現(xiàn)九宮格應(yīng)用

    Java編寫實(shí)現(xiàn)九宮格應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了Java編寫實(shí)現(xiàn)九宮格應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • java內(nèi)存管理關(guān)系及內(nèi)存泄露的原理分析

    java內(nèi)存管理關(guān)系及內(nèi)存泄露的原理分析

    這篇文章主要介紹了java內(nèi)存管理關(guān)系及內(nèi)存泄露的原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringCloud Zuul自定義filter代碼實(shí)例

    SpringCloud Zuul自定義filter代碼實(shí)例

    這篇文章主要介紹了SpringCloud Zuul自定義filter代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • 使用json字符串插入節(jié)點(diǎn)或者覆蓋節(jié)點(diǎn)

    使用json字符串插入節(jié)點(diǎn)或者覆蓋節(jié)點(diǎn)

    這篇文章主要介紹了使用json字符串插入節(jié)點(diǎn)或者覆蓋節(jié)點(diǎn)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 創(chuàng)建多模塊項(xiàng)目的超詳細(xì)教程

    IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 創(chuàng)建多模塊項(xiàng)目的超詳細(xì)教程

    這篇文章主要介紹了IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 創(chuàng)建多模塊項(xiàng)目的教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • @Autowired注解以及失效的幾個原因圖文詳解

    @Autowired注解以及失效的幾個原因圖文詳解

    在微服務(wù)項(xiàng)目中,會遇到@Autowired注解失效的情況,下面這篇文章主要給大家介紹了關(guān)于@Autowired注解以及失效的幾個原因的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • 簡述mybatis大于小于的轉(zhuǎn)義

    簡述mybatis大于小于的轉(zhuǎn)義

    這篇文章主要介紹了mybatis大于小于的轉(zhuǎn)義以及xml中常用轉(zhuǎn)義字符,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Java字節(jié)碼ByteBuddy使用及原理解析下

    Java字節(jié)碼ByteBuddy使用及原理解析下

    這篇文章主要為大家介紹了Java字節(jié)碼ByteBuddy使用及原理解析下篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05

最新評論

奉新县| 朔州市| 阿坝| 巧家县| 萍乡市| 乌审旗| 县级市| 三门县| 海口市| 漠河县| 昌宁县| 柏乡县| 大悟县| 元阳县| 滨海县| 高陵县| 安新县| 沽源县| 白银市| 鄂伦春自治旗| 华坪县| 韶关市| 通江县| 山阳县| 香港| 南通市| 专栏| 谷城县| 德昌县| 嘉定区| 固镇县| 西安市| 东港市| 花垣县| 黄石市| 恩施市| 宝鸡市| 鄂尔多斯市| 于都县| 大邑县| 长乐市|