Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3487678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:10:42+00:00 2026-05-18T11:10:42+00:00

I have a problem with my Spring application where my @Service classes are being

  • 0

I have a problem with my Spring application where my @Service classes are being created twice when the application starts. I know this is a problem with my configuration, as I’ve experienced it before, but what exactly am I doing wrong?

Is there anything fundamentally wrong with the way I’ve laid out my config, below? (I have omitted everything I deem to be irrelevant)

web.xml:

<servlet>
    <servlet-name>myapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/myapp-config.xml
        /WEB-INF/myapp-security.xml
        /WEB-INF/myapp-mvc.xml
    </param-value>
</context-param>

<listener>
    <listener-class>com.myapp.servlet.MyAppContextListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

myapp-servlet.xml

<context:component-scan base-package="com.myapp" annotation-config="true" />
<mvc:annotation-driven />

myapp-config.xml

<context:component-scan base-package="com.myapp" annotation-config="true" />
<context:annotation-config />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:10:42+00:00Added an answer on May 18, 2026 at 11:10 am

    In addition to @GaryF’s answer, there is a following beautiful solution for this problem (used in projects generated by Spring Roo):

    myapp-config.xml

    <!-- Load everything except @Controllers -->
    <context:component-scan base-package="com.myapp">
        <context:exclude-filter expression="org.springframework.stereotype.Controller"
            type="annotation"/>
    </context:component-scan>
    

    myapp-servlet.xml

    <!-- Load @Controllers only -->
    <context:component-scan base-package="com.myapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" 
            type="annotation"/>
    </context:component-scan>
    

    EDIT:

    Removing <context:component-scan> from myapp-config.xml means, that all your autodiscovered annotated beans are registered in DispatcherServlet‘s context (that is, the context loaded from myapp-servlet.xml).

    However the recommended approach is to use servlet’s context for presentation-specific things (such as controllers), and use the root context (myapp-config.xml) for the core services of your application. The solution above make this approach easy.

    Regarding the practical considerations, when your core services are placed in servlet’s application context, they can’t be accessed from outside the scope of that servlet, for example, from another servlets (you may need to use another servlets to implement another access technologies) or filters (such as Spring Security filters). That’s the reason for having core services in the root application context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which exposes a service via Spring's RMI proxy mechanism. There
I have a Spring MVC application and a problem with JUnit tests combined with
Problem, there's no method: bool ChangePassword(string newPassword); You have to know the current password
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a multi-threaded, multi-server web application with hibernate and spring managing transactions with
I'm getting a LazyInitializationException in my Spring Application. I have the OpenEntityManagerInViewFilter configured so
I have an Activity and Service that work together in my application. I've configured
I have this problem that I cannot fix. From my @Controller , i can
I am developing a spring mvc based application. I have a simple pojo form
I have an application which uses Spring 2.5 and Hibernate 3. There's a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.