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

  • Home
  • SEARCH
  • 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 3614706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:17:00+00:00 2026-05-18T22:17:00+00:00

I am using Spring’s MVC framework for an application I’m hosting on Google’s App

  • 0

I am using Spring’s MVC framework for an application I’m hosting on Google’s App Engine. So far, my controllers are registered via the @Controller annotation; however, prior to getting into Spring, I evaluated ASP.net MVC 2 which requires no configuration and is based on convention. Is convention over configuration (COC) the current and preferred method in the Java community to implement MVC with Spring? Also, this may be a result of my limited knowledge so far, but I noticed that I could only instantiate my Controllers the required constructor injection if I use the COC method via the ControllerClassNameHandlerMapping. For instance, the following controller bean config will fail if I use the DefaultAnnotationHandlerMapping.

<bean id="c" class="com.domain.TestController">
  <constructor-arg ref="service" />
</bean>
<bean id="service" class="com.domain.Service" />

My com.domain.TestController controller works fine if I use ControllerClassNameHandlerMapping/COC but it results in an error when I use DefaultAnnotationHandlerMapping/Annotations.

To clarify, I can get the constructor injection to work if I add the @Autwired annotation to the constructor, but if I do, I have to remove the @RequestMapping annotation or I will get an error stating that the controller is being mapped to the URL more than once. Some researching online indicates that the ControllerClassNameHandlerMapping is loading and mapping my controller to the URL even though I have
<context:annotation-config /> and <context:component-scan base-package ... /> in my config file.

Honestly, I can overcome these issues. I really just want to know which method, annotation or COC, is the better investment.

  • 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-18T22:17:01+00:00Added an answer on May 18, 2026 at 10:17 pm

    Tricky question; it’s a little subjective and really just up to your personal preference or your dev team’s consensus. I personally like annotations because I feel like it’s more flexible. If you have to follow conventions to get your framework to “automagically” do stuff, you are stuck with whatever convention the framework developers decided on (granted, it’s usually not a bad convention, but you are stuck with it). During my time of using Spring (2 years or so), it’s always seemed like our team has started out using COC, only to realize later that our needs aren’t really best served by the typical convention.

    I also feel like annotations improve the readability of your code. When looking at an annotated class, I can understand what it does and how it does it without needing to reference the application context.

    As for your second question (injecting beans into a controller), here’s some code to show how I usually wire a controller.

    web-servlet.xml

    <context:component-scan base-package="com.test.web.controllers" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
    

    TestController.java

    package com.test.web.controllers
    
    import com.test.dao.TestDAO;
    
    @Controller
    public class TestController {
        @Autowired
        @Qualifier("myTestDAO")
        private TestDAO testDAO;
    
        @RequestMapping(value = "/welcome.htm*", method = RequestMethod.GET)
        public ModelMap showWelcomePage() {
            ModelMap model = new ModelMap();
            model.addAttribute("products", testDAO.getOfferedProducts());
            return model;
        }
    }
    

    TestDAO would then have this class declaration:

    @Repository("myTestDAO")
    public class TestDAO {
    

    I usually don’t use constructor wiring because this is usually simpler, but if you need constructor wiring, I can post an example of that instead.

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

Sidebar

Related Questions

I am using spring mvc in my application,when i created user i have to
I am using spring mail to send an email via google's smptp server. I
I'm using spring MVC and spring security. I have annotation-driven controller and trying to
I have my application using Spring MVC + Apache 2 tiles + Spring security,
I am using Spring MVC with annotation configuration. I have a controller class for
Using Spring MVC, do interceptors only intercept defined controller mappings or can they also
Using spring 3.0 MVC: Is it possible to programatically execute a controller's action, and
Using Spring MVC, I have this Controller class: @Controller public class LoginController { @Autowired
Currently Using: Spring Tools Suite v2.7.2 Spring Framework v3.0.5 Sample Code in a controller:
I am using spring/hibernate stand alone application. if i dont configure Transactions i am

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.