(二)Spring组件注册-@ComponentScan和@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注解

这个注解定义如下:

//
// 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.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值