没有定义名为’org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry’的bean

前端之家收集整理的这篇文章主要介绍了没有定义名为’org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry’的bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试运行完整的junit测试类包,我有一个我的域类的审计类,如下所示:

    @PrePersist
    public void prePersist(AuditableEntity e) {

      UserService userService = Springbeanfactory.getBean(UserService.class);  
      // some auditing here  
    }

– Springbeanfactory类:

public class Springbeanfactory {

    private static ApplicationContext applicationContext;

    public static requiredType) {
        T bean = null;
        if (applicationContext != null) {
            bean = applicationContext.getBean(name,requiredType);
        }
        return bean;
    }

    public static requiredType) {
        T bean = null;
        if (applicationContext != null) {
            bean = applicationContext.getBean(requiredType);
        }
        return bean;
    }

    public static void setApplicationContext(final ApplicationContext applicationContext) {
        if (Springbeanfactory.applicationContext == null) {
            Springbeanfactory.applicationContext = applicationContext;
        }
    }

}

-Test类配置:

@Autowired
private ApplicationContext applicationContext;

@Before
public void before() throws Exception {

    Springbeanfactory.setApplicationContext(applicationContext);

}

-SpringTestingConfig类:

@Configuration
@ComponentScan(basePackages = "com.myapp.data",excludeFilters = { @Filter(Configuration.class) })
@PropertySource("classpath:/test.properties")
@Profile("test")
public class SpringTestingConfig {

    private static Logger log = (Logger)LoggerFactory.getLogger(SpringTestingConfig.class);

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public DataSource XdataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        if(log.isDebugEnabled()) log.debug("profile.name","test");
        System.setProperty("profile.name","test");

        dataSource.setDriverClassName("org.h2.Driver");
        String schemaName = ConfigurationUtil.config().getString("db.schema.name").toLowerCase();
        log.debug("SCHEMA IS " + schemaName);
        String url = "jdbc:h2:mem:test;MODE=MysqL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS " +schemaName +"\\;" + "SET SCHEMA "+schemaName;
        dataSource.setUrl(url);
        //dataSource.setUrl("jdbc:h2:mem:test;MODE=MysqL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS "    + schemaName);

        dataSource.setUsername("sa");

        //use your own local MysqL in tests here...
//      dataSource.setDriverClassName("com.MysqL.jdbc.Driver");
//      dataSource.setUrl("jdbc:MysqL://localhost:3306/mv_tests?characterEncoding=UTF-8");
//      dataSource.setUsername("tomcat");
//      dataSource.setPassword("tomcat");
//        
        return dataSource;
    }

    @Bean
    public DataSource dataSource() {

        Springbeanfactory.setApplicationContext(applicationContext);
        LoggerUtils.setAllApplicationLogs("DEBUG");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        if(log.isDebugEnabled()) {
            log.debug("profile.name","test");
        }
        System.setProperty("profile.name","test");

        dataSource.setDriverClassName("com.MysqL.jdbc.Driver");
        String schemaName = ConfigurationUtil.config().getString("db.schema.name");
        String username = ConfigurationUtil.config().getString("db.username");
        String password = ConfigurationUtil.config().getString("db.password");
        if( log.isDebugEnabled() ) {
            log.debug( "SCHEMA IS " + schemaName );
            log.debug( "Username IS " + username );
            log.debug( "Password IS " + password );
        }

        dataSource.setUrl("jdbc:MysqL://localhost:3306/"+schemaName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }

}

-Test类注释:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ WebContextTestExecutionListener.class,DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class,TransactionalTestExecutionListener.class })
@ActiveProfiles("test")
@DirtiesContext
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,classes = { SpringConfig.class,SpringTestingConfig.class,SpringLocalContainerJPAConfig.class,CustomConfiguration.class })
@Transactional

当我的测试方法试图保存一个实体时,它调用PrePersist方法,然后调用获取spring服务:

UserService userService = Springbeanfactory.getBean(UserService.class);

这反过来会产生以下异常:

Error creating bean with name 'userService': 
Injection of autowired dependencies Failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private com.motivosity.data.repository.UserRepository com.motivosity.service.impl.UserServiceImpl.userRepository; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepositoryImpl': 
Injection of persistence dependencies Failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'springLocalContainerJPAConfig': Injection of autowired dependencies Failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
javax.sql.DataSource com.motivosity.data.config.SpringLocalContainerJPAConfig.dataSource; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'springTestingConfig': Initialization of bean Failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
Initialization of bean Failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' is defined

我必须提到在运行完整的测试类包时会发生此异常,但是当单独运行此测试类时,不会产生异常.

顺便说一下,我正在使用spring 3.2.3.RELEASE

更新:当我将spring版本升级到最新版本4.0.3时,我在同一个get UserService行上获得了一个新的异常:

org.springframework.context.support.GenericApplicationContext@3aa54263 has been closed already

请告知如何解决此异常.

最佳答案
当您使用@DirtiesContext注释测试类或测试方法时,您告诉Spring在该测试类或方法之后关闭ApplicationContext.因此,如果您稍后尝试从封闭的上下文中检索bean,您将获得一个类似于您所看到的异常.

我的猜测是你在测试套件中的其他测试类中使用@DirtiesContext,结果是Springbeanfactory.setApplicationContext()中的逻辑被破坏,因为它可能会维护对封闭上下文的引用.因此,您需要允许为每个测试设置当前的ApplicationContext.换句话说,删除null-check,如下所示

public static void setApplicationContext(final ApplicationContext applicationContext) {
    // always set the current context
    Springbeanfactory.applicationContext = applicationContext;
}

希望这可以帮助!

– Sam(Spring TestContext框架的作者)

原文链接:https://www.f2er.com/spring/431512.html

猜你在找的Spring相关文章