Вы находитесь на странице: 1из 2

1.

Where to put context:component-scan


Does the context:component-scan line belong in the dispatcher-servlet.xml or the app-context.xml (contextConfigLocation) file? I have the following line in both files right now, which doesn't seem correct, but if I take it out of dispatcher-servlet.xml the app breaks. I've seen examples with it in different files so I'm not sure which is proper. <context:component-scan base-package="com.mycompany"/> Thanks for your help.
Reply With Quote

2.
jamestastic

Aug 2nd, 2010, 01:52 AM#2

Senior Member Join Date Nov 2008 Posts 340

There is no strict rule regarding the name of Spring configuration files nor which beans are defined in them. The guideline is to separate your bean definitions in a way that makes sense to you and works for your application. In your setup, you have two configuration files: dispatcher-servlet.xml and app-context.xml. My assumption is that your core application beans are defined in appcontext.xml and your web-specific beans are defined in dispatcher-servlet.xml. Further, I am assuming you have configured your web application to load both configuration files (generally via ContextLoaderListener defined in the web.xml file). Where you put the <context:component-scan /> element is entirely dependent on what you're trying to scan for. If you want to scan for core application beans, then it would make more sense in app-context.xml. On the other hand, if you're scanning for web-specific beans (such as @Controller beans), you should put it in dispatcherservlet.xml. Further, with the above assumptions, beans in the web configuration file would have access to beans in the core application configuration file as dependencies/references, but not the other way around, so anything you component-scan for in the core application configuration file would be available from the web-specific configuration file.
Reply With Quote

3.
Enrico Pizzi

Aug 2nd, 2010, 01:53 AM#3

Senior Member Join Date Jul 2010 Location Venice, Italy Posts 688

Hi, I think it depends on what you are scanning for. If you are scanning for controllers, this is Spring Web MVC (i.e. DispatcherServlet - related) and the scan should be done in dispatcher-servlet.xml configuration. If you are scanning for annotations non-webmvc related, the directive should be put in app-context.xml (i.e. the one/s you load with listener in web.xml). Assuming your controllers and your non-controller beans are in different packages (if not, consider refactoring your packages), if you put two directives one in each file and point (with the base-package attribute) to your controllers package for the scan in dispatcher-servlet.xml and to your other packages in the app-context.xml, it should work fine. Better than that, for the webmvc part, you could drop the component-scan and just use the <mvc:annotation-driven/> directive which will give you full annotated controllers config in just one line.
Reply With Quote

4.
neojonas
Member

Aug 2nd, 2010, 11:37 AM#4

Join Date Apr 2006 Posts 44

Thanks for the quick responses. dispatcher-servlet.xml is where I have all the MVC stuff configured. app-config.xml is where I have my services, dao, session factories, datasources, etc defined. I have 4 main packages - web, service, dao, domain. So it sounds like I should have dispatcher-servlet do context:component-scan on the web directory and appconfig.xml do a context:component-scan on the other 3 directories. This seems to make sense.

Originally Posted by Enrico Pizzi

Better than that, for the webmvc part, you could drop the component-scan and just use the <mvc:annotation-driven/> directive which will give you full annotated controllers config in just one line. I actually do have <mvc:annotation-driven/> in my dispatcher-servlet.xml file, but it fails if I also don't have <context:component-scan basepackage="com.mycompany.web" />. So it seems I need both??
Reply With Quote

5.
Enrico Pizzi

Aug 3rd, 2010, 01:39 AM#5

Senior Member Join Date

Jul 2010 Location Venice, Italy Posts 688

Interesting. Maybe that happens because you use annotations non-mvc related on your controllers...mvc:annotation-driven only scans webmvc-related stuff. Could you please post your xmls and the stacktrace you get from the server when trying to deploy the application without the context:component-scan in your dispatcherservlet.xml?
Reply With Quote

Model is an interface. ModelMap is an implementation of the Model interface. ModelAndView is a Model and also has a String to store the view string. ModelAttribute is an attribute in a Model to get it out. ModelAttribute is an Annotation. Basically when you have data in code and you want to make that data available to the rendering of your jsp page, you need to put that data somewhere so that it is available. That is what a Model is for. To hold that data you retrieved in your code. It is just a glorified map. So example, I have a form with a backing object with data. The form has three drop downs that I also need data to fill the drop downs. That is 4 different objects. 3 Lists for the drop downs and one object for the form backing object. If my code gets all this data, I put them into the Model's Map and now I can use them on my jsp page.

Вам также может понравиться