(二)Spring组件注册-@ComponentScan和@ComponentScans

文章目录

  • 链接
    • 视频
    • github
    • 老的方式
    • 注解@ComponentScan
      • 测试用例
      • 配置类
      • 启动类
      • 结果
      • 结果解析
    • ComponentScan注解
    • ComponentScan的excludeFilters
      • 配置类
      • 启动类
      • 结果
      • 结果解析
    • ComponentScan的includeFilters
      • 配置类
      • 结果
      • 结果解析
      • 真正的配置
      • 正常xml配置
      • 配置类配置
      • 结果
    • FilterType
      • 结果
    • FilterType.CUSTOM
      • 自定义TypeFilter
      • 配置类
      • 结果
  • ComponentScans
      • 配置类
      • 运行结果
  • 问题

链接

视频

https://www.bilibili.com/medialist/play/ml1022680573/p3

github

https://github.com/ouyangxizhu/SpringSourceCode

老的方式

<!-- 包扫描、只要标注了@Controller@Service@Repository@Component -->
<context:component-scan base-package="com.ouyangxizhu" use-default-filters="false"></context:component-scan> 

注解@ComponentScan

测试用例

package com.ouyangxizhu.dao;

import org.springframework.stereotype.Repository;

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

}
package com.ouyangxizhu.service;

import org.springframework.stereotype.Service;

@Service
public class BookService {
   
}

package com.ouyangxizhu.controller;

import com.ouyangxizhu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class BookController {
   

	@Autowired
	private BookService bookService;

}

配置类

package com.ouyangxizhu.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(basePackages = "com.ouyangxizhu")
@Configuration
public class MainConfigComponentScan {
   

}

启动类

package com.ouyangxizhu;

import com.ouyangxizhu.config.MainConfigComponentScan;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.awt.*;

public class MainTestComponentScan {
   
    public static void main(String[] args) {
   
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigComponentScan.class);
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
   
            System.out.println(name);
        }
    }
}

结果

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
mainConfigComponentScan
mainConfigConfigurationBean
bookController
bookDao
bookService
person02

结果解析

包含spring的类和自己写的类
mainConfigComponentScan也是一个组件,可以查看@Configuration注解

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target({
   ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
   
    String value() default "";
}

这个注解也被@Component标注,因此mainConfigComponentScan也会被加入到IOC容器中

ComponentScan注解

这个注解定义如下:

//

你可能感兴趣的:(spring,ComponentScan,ComponentScans)