关于使用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 (测试可用) 或者更高

猜你喜欢

转载自www.cnblogs.com/sunshine99/p/10551907.html
今日推荐