0

I am trying to initialize the spring beans from a servlet application. The spring project is in the form of a jar. To initialize the bean, I kept the application-context.xml of spring project in its classpath. Then I added the following codes in the web.xml of the servlet project so that it will initialize the beans

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:**/application-context.xml</param-value>
</context-param> 

It seems like when I run the server it is picking up the application-context.xml of the spring project. If I do not add

<context:component-scan base-package="com.company.package" />

in the application-context.xml, the build is successful. Also if I manually define a bean that has no annotations like @service, @component, @repository, @autowired anywhere inside the class, it creates the bean. I mean when I define bean as

<bean id=" exampleBeanName" class="com.company.package.ExampleBeanName"/>.

That time also there is no issue. However, if I add

<context:component-scan base-package="com.company.package" /> 

then the build fails. Here is the exception

2016-05-29 17:22:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'messageSource'
2016-05-29 17:22:39 DEBUG XmlWebApplicationContext:708 - Using MessageSource [org.springframework.context.support.ReloadableResourceBundleMessageSource: basenames=[classpath:i18n]]
2016-05-29 17:22:39 DEBUG XmlWebApplicationContext:742 - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@1dcbf884]
2016-05-29 17:22:39 DEBUG UiApplicationContextUtils:85 - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@63240690]
2016-05-29 17:22:39 DEBUG DefaultListableBeanFactory:741 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5896ffab: defining beans [signupController,profileDaoImpl,personalDetailsDTO,userCredentialsDTO,userSignUp,profileServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,jspViewResolver,propertyConfigurer,jdbcTemplate,dataSource,messageSource,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
2016-05-29 17:22:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'signupController'
2016-05-29 17:22:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'signupController'
2016-05-29 17:22:39 ERROR ContextLoader:336 - Context initialization failed
java.lang.NoClassDefFoundError: org/springframework/web/servlet/ModelAndView
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:612)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)

I have included spring jars in the pom.xml of the spring based project. Here are the jars

          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

If I run the spring based separately, it works. This issue arises only when I add its dependency in the servlet project. I am trying to resolve this for the past three days, but still I am unable to figure it out.I would appreciate any one's thoughts. Thank you.

Yogesh Ghimire
  • 432
  • 1
  • 8
  • 21
  • You have conflicting Spring dependencies in your POM... Make sure to stick to a **single** version. – Tunaki May 29 '16 at 23:08
  • I just ran the build using 4.2.6.RELEASE for all the spring jars... Still i get the same error – Yogesh Ghimire May 29 '16 at 23:14
  • Well, the thing is, this error is always caused by conflicting dependencies somewhere. Maybe they are provided by your server? Also, check the dependency tree `mvn dependency:tree` to see what Maven uses as resolved artifacts. – Tunaki May 29 '16 at 23:16
  • Why are you using xml based config. Use complete java config – LynAs May 30 '16 at 02:43

0 Answers0