spring中@ComponentScan自动扫描并指定扫描规则

2023-05-18 05:05:04 规则 指定 扫描

1.使用注解配置包扫描

1.1.创建相关类

分别创建BookDao、BookService、BookServiceImpl以及BookController这三个类,并在这三个类中分别添加@Repository、@Service、@Controller注解

BookDao

package com.tianxia.springannotation.dao;
import org.springframework.stereotype.Repository;

// 名字默认是类名首字母小写
@Repository
public class BookDao {
}

BookService

package com.tianxia.springannotation.service;

public interface BookService {
}

BookServiceImpl

package com.tianxia.springannotation.service.impl;
import com.tianxia.springannotation.service.BookService;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {
}

BookController

package com.tianxia.springannotation.controller;
import org.springframework.stereotype.Controller;

@Controller
public class BookController {
}

1.2.SpringBoot启动类默认就有配置@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;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ComponentScanTest {
    
    @Test
    public void test() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringAnnotationApplication.class);
        // 我们现在就来看一下IOC容器中有哪些bean,即容器中所有bean定义的名字
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
    }
}

2.扫描时排除注解标注的类

在注解类上通过@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;

@SpringBootApplication
// value指定要扫描的包
@ComponentScan(value="com.tianxia.springannotation", excludeFilters={
        
        @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.扫描时只包含注解标注的类

在注解类中的includeFilters()方法来指定Spring在进行包扫描时,只包含哪些注解标注的

这里需要注意的是,当我们使用includeFilters()方法来指定只包含哪些注解标注的类时,需要禁用掉默认的过滤规则

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;

@SpringBootApplication
// value指定要扫描的包
@ComponentScan(value="com.tianxia.springannotation", includeFilters={
        
        @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.重复注解

@ComponentScans({
        @ComponentScan(value="com.tianxia.springannotation", includeFilters={
                
                @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class})
        }, useDefaultFilters=false),
        @ComponentScan(value="com.tianxia.springannotation", includeFilters={
                
                @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Service.class})
        }, useDefaultFilters=false)
})

到此这篇关于spring中@ComponentScan自动扫描并指定扫描规则的文章就介绍到这了,更多相关spring @ComponentScan自动扫描内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章