Spring中部分@Components注解的类无法被@ComponentScan扫描到的问题

Spring中部分@Components注解的类无法被@ComponentScan扫描到的问题

今天在做@Spring自动化装配的入门练习时,一个被@Components注解的test类一直无法@ComponentScan扫描到,但其他的被@Components注解的类可以被扫描到

package com.configBean;
@Component
class testBean1{
}
@Component("lonelyHeartsClub")//@Named("lonelyHeartsClub")两者之间有细微的差距,大多时候能够相互替换
class SgtPeppers implements CompactDisc{
    private String title="Sgt. Pepper`s Lonely Hearts Club Band";
    private String artist="The Beatles";
    public void play() {
        System.out.println("Playing "+title+" by "+artist);
    }
}
@Configuration
@ComponentScan(basePackages = {"com.configBean"})
class CDPlayerConfig{
}
public class  CDPlayerTest{ 
	@Test
    public void demo() {
      // ApplicationContext context=new AnnotationConfigApplicationContext(testBean.class);
       ApplicationContext context=new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        String [] beanDefinitionNames=context.getBeanDefinitionNames();
        for (String name:beanDefinitionNames) {
            System.out.println("BeanName:"+name);
        }
}

运行结果

BeanName:CDPlayerConfig
BeanName:lonelyHeartsClub

test这个类没有被加载到,在找原因时我尝试将CDPlayerConfig这个类注解,然后新创建了一个CDPlayerConfig1

//@Configuration
//@ComponentScan(basePackages = {"com.configBean"})
//class CDPlayerConfig{
//}
@Configuration
@ComponentScan(basePackages = {"com.configBean"})
class CDPlayerConfig1{
}

调用时出现了异常

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure during classpath scanning; nested exception is java.io.FileNotFoundException: class path resource [com/configBean/CDPlayerConfig.class] cannot be opened because it does not exist

将CDPlayerConfig给注解掉了为什么还是会出现异常?我发现在target的META-INF下有个spring.componts的文件
在这里插入图片描述
打开后发现里面是我之前被扫描到的bean,仔细看后发现里面过然有被我注解了的CDPlayerConfig类
在这里插入图片描述
抱着尝试的想法,我清空了spring.componts文件中的内容,再次尝试后发现之前不能被扫描到的test类,又可以被扫描到了,问题解决!
但存疑的是spring.componts文件是个什么文件,到底为什么会导致test类无法被扫描到我还是不太清楚。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值