简体   繁体   中英

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. 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:

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. Multiple SLF4J bindings. 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

Line 4 tells you where to look for more info about the error. 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:

<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. 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):

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. You can instantiate it like so:

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

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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