使用@ComponentScan扫描包,简单示例

2022-07-07 11:38:14

代码结构,按照要求新建简单的代码结构

1、在配置类DemoConfig中添加@ComponentScan扫描,可以扫描到加上了Controller、Repository、Service等注解的代码

@Configuration
@ComponentScan(value = "cn.enjoy.*")
public class DemoConfig {

}

2、输出扫描到的类

public class DemoApp {
    public static void main(String[] args){
        AnnotationConfigApplicationContext applicationContext = 
new AnnotationConfigApplicationContext(DemoConfig.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for(String vo:beanDefinitionNames){
            System.out.println(vo);
        }
    }
}

输出结果

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
demoConfig
personController
personDao
personImpl
person
  • 作者:一点光辉
  • 原文链接:https://blog.csdn.net/qq_22701869/article/details/102546916
    更新时间:2022-07-07 11:38:14