关于使用spring版本4.1.6注解@Import报错

记录一下遇到的错误 org.springframework.beans.factory.parsing.BeanDefinitionParsingException:

使用环境:spring 4.1.6

代码:

类1 配置类

@Configuration
@Import(value= {Sky.class,MyImportSelector.class})
public class MainConfig3 {

}

类2

public class MyImportSelector implements ImportSelector {

    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//        String className = importingClassMetadata.getClassName();
//        System.out.println(className);
        return new String [] {"com.leon.pojo.Dog","com.leon.pojo.Sky","com.leon.pojo.Bird"};
    }
}

类3 测试类

public class MainTest {

AnnotationConfigApplicationContext config = new AnnotationConfigApplicationContext(MainConfig3.class);

@Test
        public void testImport() {
            String[] names = config.getBeanDefinitionNames();
            for(String st : names) {
                System.out.println(st);
            }
        }

 

 

很简单的一个导入bean注解,测试代码

在使用import的时候报错:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:

Configuration problem: com.leon.pojo.Sky was @Import'ed but is not annotated with @Configuration nor does it declare any @Bean methods; it does not implement ImportSelector or extend ImportBeanDefinitionRegistrar. Update the class to meet one of these requirements or do not attempt to @Import it.
Offending resource: class com.leon.pojo.Sky

 

解决方案:

1.在导入的bean类上加上@Repository  这样就能正常使用


2.把版本升级到4.2.6 (测试可用) 或者更高

转载于:https://www.cnblogs.com/sunshine99/p/10551907.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值