简体   繁体   English

将库添加为依赖项时,Spring初始化不起作用

[英]Spring initialization not working when the library is added as a dependency

I have a library which exercises Spring and its getting properly initialized when run with in the project. 我有一个练习Spring的库,在项目中运行时会正确初始化它。 When I start an other project and add this library as a dependency, I am getting an initialization failure. 当我启动另一个项目并将该库添加为依赖项时,出现初始化失败。 Here is the code: 这是代码:

    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;
    }
   }

Here is the exception which I am getting from the project when added as a dependency. 这是我从项目中获得的例外,将其添加为依赖项时。

[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

Can anyone help me with this. 谁能帮我这个。

The first 5 lines are showing you all the info you need and even a place to where to find an answer: 前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]

Line 1 states the actual problem. 第1行陈述了实际的问题。 Multiple SLF4J bindings. 多个SLF4J绑定。 Which means that you have more logger versions/implementations trying to do the same thing, which raises an exception. 这意味着您有更多的记录器版本/实现试图做同样的事情,这引发了异常。

Lines 2 and 3 show where this conflict occured 第2行和第3行显示了发生冲突的位置

Line 4 tells you where to look for more info about the error. 第4行告诉您在哪里可以找到有关该错误的更多信息。 And if you do, you will see what they suggest here . 如果这样做,您会在这里看到他们的建议。 If you have multiple implementations in your pom (I haven't seen it, though), you can exclude the one that is NOT on line 5 , like so: 如果您的pom中有多个实现(不过我没有看到),则可以排除不在第5行的实现 ,如下所示:

<exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

If that doesn't work: Try purging your local .m2 repository, maybe several loggers are cached there. 如果不起作用:尝试清除您的本地.m2存储库,可能有几个记录器缓存在那里。 You can do that with: 您可以执行以下操作:

mvn dependency:purge-local-repository

That one works fine with me, however, if it still runs into binding issues, than purge the repo with bellow command, it will remain empty and download all dependencies again (after purging): 那对我来说很好用,但是,如果仍然遇到绑定问题,而不是使用bellow命令清除回购协议,它将保留为空并再次下载所有依赖项(清除后):

mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false

I don't know why it happened, you haven't posted your pom but I avoid such issues by always using the slf4j that comes already with spring-boot so no extra dependencies are needed. 我不知道为什么会这样,您还没有发布pom,但是我总是通过使用spring-boot附带的slf4j来避免此类问题,因此不需要任何额外的依赖关系。 You can instantiate it like so: 您可以这样实例化它:

private final Logger logger = LoggerFactory.getLogger(this.getClass());

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM