我有一个库可以运行Spring,并在项目中运行时正确初始化它。当我启动另一个项目并将此库添加为依赖项时,我收到初始化失败。 这是代码:
public class CBRepoFactory implements IRepoFactory {
private UserActivityRepositoryService userActivityRepositoryService;
private ItemInformationRepositoryService itemInformationRepositoryService;
public CBRepoFactory() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Config.class);
ctx.scan("com.example.db.app");
ctx.refresh();
userActivityRepositoryService = ctx.getBean(UserActivityRepositoryService.class);
itemInformationRepositoryService = ctx.getBean(ItemInformationRepositoryService.class);
// ctx.close();
}
@Override
public IRepoClient<UserActivity> getUserActivityRepositoryService() {
// TODO Auto-generated method stub
return this.userActivityRepositoryService;
}
@Override
public IRepoClient<ItemInformation> getItemInformationRepositoryService() {
// TODO Auto-generated method stub
return this.itemInformationRepositoryService;
}
}
以下是我作为依赖项添加时从项目中获取的异常。
[main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d71d552: startup date [Fri Aug 18 14:05:41 PDT 2017]; root of context hierarchy
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/support/PropertySourceFactory
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:301)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
at com.ebay.db.app.clientImpl.CBRepoFactory.<init>(CBRepoFactory.java:23)
at com.ebay.db.app.clientImpl.RepoFactoryBuilder.createFactory(RepoFactoryBuilder.java:11)
at testDBClient.testProgram.main(testProgram.java:15)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.support.PropertySourceFactory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more
任何人都可以帮助我。
答案 0 :(得分:0)
前5行显示了您需要的所有信息,甚至是找到答案的地方:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/sukrishna/.m2/test2/org/slf4j/slf4j-simple/1.7.5/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/sukrishna/.m2/test2/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
第1行说明实际问题。多个SLF4J绑定。这意味着您有更多的记录器版本/实现尝试执行相同的操作,这会引发异常。
第2行和第3行显示发生此冲突的位置
第4行告诉您在哪里可以查找有关错误的更多信息。如果你这样做,你会看到他们的建议here。如果您的pom中有多个实现(但我还没有看到它),您可以排除不在第5行的那个,如下所示:
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
如果这不起作用:尝试清除本地.m2存储库,可能会在那里缓存几个记录器。你可以用:
来做到这一点mvn dependency:purge-local-repository
然而,如果它仍然适用于绑定问题,而不是使用bellow命令清除repo,它将保持为空并再次下载所有依赖项(清除后):
mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
我不知道为什么会发生这种情况,你还没有发布你的pom但是我总是使用已经使用spring-boot的slf4j来避免这些问题,所以不需要额外的依赖。您可以像这样实例化它:
private final Logger logger = LoggerFactory.getLogger(this.getClass());
希望有所帮助