0

My application is correctly running with:

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

When I replace this by the manual bean definition the Controllers are not detected anymore.

In any of the cases I'm using those annotations:

<context:annotation-config />
<mvc:annotation-driven />

The autowired methods of the controllers are called but the beans are not declared as entrypoints, thus, 404 error and not accessible.

What is the black magic behind component scan?

Controllers are declared like that:

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Controller configuration -->
    <bean class="com.xx.ControllerClass" />

</beans>
7
  • How exactly do you replace it with manual bean definition? Do you define controllers? Commented Jul 10, 2012 at 17:20
  • I just define beans. It should be ok, shouldn't? Commented Jul 10, 2012 at 17:27
  • Do you understand that controllers are beans too? Commented Jul 10, 2012 at 17:29
  • What I say is that I don't explicitly declare de controllers. I've just created an applicationContext-controller.xml and included de controllers as beans. Then I expect spring to see that those are controllers by detecting the annotation @RequestMapping. Commented Jul 10, 2012 at 17:35
  • 1
    Is this like transactions, where they need to be in the same file? Try adding annotation-driven to the file with the controllers defined, see if that helps. Commented Jul 10, 2012 at 19:40

1 Answer 1

3

Speculation based on your comments - you have declared the beans for your controllers in a applicationContext-controllers.xml file, now is this file imported in the web application Context file, the one that you declare with the DispatcherServlet web.xml file:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/applicationContext-controller.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet> 

If it is not, then that is probably the issue. There are typically two different application contexts for a Spring MVC based application, the one that you declare using ContextLoaderListener(the Root Web application Context) and the web related beans declared through DispatcherServlet, your Controllers, mvc:annotation-driven etc need to be in the web related beans declaration.

Sign up to request clarification or add additional context in comments.

1 Comment

That's the solution. I thought everything was in the same context. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.