Spring Component Scanning

在Spring应用中,@ComponentScan注解用于指定需要扫描的包,默认扫描当前包及其子包。在SpringBoot应用中,@SpringBootApplication是一个组合注解,包含了@Configuration、@EnableAutoConfiguration和@ComponentScan。@ComponentScan可以带参数指定特定包进行扫描,也可以排除或包含特定过滤器。文章通过示例展示了不同情况下@ComponentScan的用法。
摘要由CSDN通过智能技术生成

@ComponentScan Without Arguments

In a Spring Application

Use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned.

@ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

package com.componentscan.springapp.animals;
@Component
public class Cat {}

package com.componentscan.springapp.animals;
@Component
public class Dog {}

package com.componentscan.springapp.flowers;
@Component
public class Rose {}

# ------------------------------------------------
package com.componentscan.springapp
@Configuration
@ComponentScan
public class SpringComponentScanApp {
    private static ApplicationContext applicationContext;
    @Bean
    public ExampleBean exampleBean() {
        return new ExampleBean();
    }
    public static void main(String[] args) {
        applicationContext = new AnnotationConfigApplicationContext(SpringComponentScanApp.class);
        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }
    }
}

springComponentScanApp
cat
dog
rose
exampleBean

In a Spring Boot Application

SpringBoot use @SpringBootApplication annotation, and it’s a combination of three annotations:

@Configuration
@EnableAutoConfiguration
@ComponentScan
package com.componentscan.springbootapp;

@SpringBootApplication
public class SpringBootComponentScanApp {
    private static ApplicationContext applicationContext;
    @Bean
    public ExampleBean exampleBean() {
        return new ExampleBean();
    }
    public static void main(String[] args) {
        applicationContext = SpringApplication.run(SpringBootComponentScanApp.class, args);
        checkBeansPresence( "cat", "dog", "rose", "exampleBean", "springBootComponentScanApp");
    }
    private static void checkBeansPresence(String... beans) {
        for (String beanName : beans) {
            System.out.println("Is " + beanName + " in ApplicationContext: " + applicationContext.containsBean(beanName));
        }
    }
}

Is cat in ApplicationContext: true
Is dog in ApplicationContext: true
Is rose in ApplicationContext: true
Is exampleBean in ApplicationContext: true
Is springBootComponentScanApp in ApplicationContext: true

这里检验存在而不是全部输出, is that the output would be too large.
This is because of the implicit @EnableAutoConfiguration annotation, which makes Spring Boot create many beans automatically, relying on the dependencies in pom.xml file.


@ComponentScan With Arguments

In a Spring Application

package com.componentscan.springapp.animals;
@Component
public class Cat {}

package com.componentscan.springapp.animals;
@Component
public class Dog {}

package com.componentscan.springapp.flowers;
@Component
public class Rose {}

@ComponentScan(basePackages = "com.componentscan.springapp.animals")
@Configuration
public class SpringComponentScanApp {
    private static ApplicationContext applicationContext;
    @Bean
    public ExampleBean exampleBean() {
        return new ExampleBean();
    }
    public static void main(String[] args) {
        applicationContext = new AnnotationConfigApplicationContext(SpringComponentScanApp.class);
        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }
    }
}

springComponentScanApp
exampleBean
cat
dog

In a Spring Boot Application

@SpringBootApplication
@ComponentScan(basePackages = "com.componentscan.springbootapp.animals")

@ComponentScan with Multiple Packages

@ComponentScan(basePackages = {"com.componentscan.springapp.animals", "com.componentscan.springapp.flowers"})

# since spring 4.1.1
@ComponentScan(basePackages = "com.componentscan.springapp.animals;com.baeldung.componentscan.springapp.flowers")
@ComponentScan(basePackages = "com.componentscan.springapp.animals,com.baeldung.componentscan.springapp.flowers")
@ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals com.componentscan.springapp.flowers")

@ComponentScan with Exclusions/IncludeFilters

参考:Spring @ComponentScan – Filter Types


参考:Spring Component Scanning

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值