大佬教程收集整理的这篇文章主要介绍了没有定义名为’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);
}
@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.scheR_918_11845@a.name").toLowerCase();
log.debug("scheR_918_11845@A IS " + schemaname);
String url = "jdbc:h2:mem:test;MODE=MysqL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=falSE;INIT=CREATE scheR_918_11845@A IF NOT EXISTS " +schemaname +"\\;" + "SET scheR_918_11845@A "+schemaname;
datasource.setUrl(url);
//datasource.setUrl("jdbc:h2:mem:test;MODE=MysqL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=falSE;INIT=CREATE scheR_918_11845@A 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.scheR_918_11845@a.name");
String username = ConfigurationUtil.config().getString("dB.USERNAME");
String password = ConfigurationUtil.config().getString("db.password");
if( log.isDebugEnabled() ) {
log.debug( "scheR_918_11845@A 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,Customcatonfiguration.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,结果是Springbeanfactory.setApplicationContext()中的逻辑被破坏,因为它可能会维护对封闭上下文的引用.因此,您需要允许为每个测试设置当前的ApplicationContext.换句话说,删除null-check,如下所示
public static void setApplicationContext(final ApplicationContext applicationContext) {
// always set the current context
Springbeanfactory.applicationContext = applicationContext;
}
希望这可以帮助!
以上是大佬教程为你收集整理的没有定义名为’org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry’的bean全部内容,希望文章能够帮你解决没有定义名为’org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry’的bean所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。