1

Following is the code for my dispatcher-servlet.xml and configuration class :

Where do i put my db configuration and entity definitions?

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        super.addViewControllers(registry);
        // Default view
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/home").setViewName("home");
    }

    @Bean
    public Filter basicMetricFilter() {
        return new PieRequestMetricsFilter();
    }
}
Sachin Sharma
  • 1,446
  • 4
  • 18
  • 37

1 Answers1

3

You can put those configurations in MvcConfig but it's NOT a good idea. A better approach is to define one config for each architectural aspect of your application and then put related configs only in each one. Suppose you have a web application with traditional layered architecture. In this example you would have WebConfig just like your MvcConfig class, like this:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        super.addViewControllers(registry);
        // Default view
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/home").setViewName("home");
    }

    @Bean
    public Filter basicMetricFilter() {
        return new PieRequestMetricsFilter();
    }
}

Also, you could have a RepositoryConfig that contains your data access related configurations, like this:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackges = "com.example.repository")
public class RepositoryConfig() {
    // put your datasource, entity manager, jdbc templates, tx managers here
}

For wiring these configurations together, there is no need for any xml file such as dispatcher servlet or web.xml. You can define a WepApplicationInitializer that defines parent and child configs in this scenario, like this:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RepositoryConfig.class, SecurityConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

WebApplicationInitializer is an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container. An abstract base class implementation of WebApplicationInitializer named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration. For more details please consult the spring documentation.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • thanks Ali, that answered my question perfectly. :) few resources that i found helpful : http://docs.spring.io/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html http://www.mkyong.com/spring-security/spring-security-hibernate-annotation-example/ http://websystique.com/springmvc/spring-mvc-4-angularjs-example/ http://www.baeldung.com/spring-mvc-static-resources :) – Sachin Sharma Oct 08 '15 at 08:23
  • Your welcome. Usually spring projects have nice and very useful documentations. – Ali Dehghani Oct 08 '15 at 08:27
  • Yes. Docs for both spring and angular(just started) are awesome. one more issue that i am getting is that i can't find static resources : @Override public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.addResourceHandler("/static/**").addResourceLocations("/static/"); } – Sachin Sharma Oct 08 '15 at 08:30
  • this is what i defined in my config class. and i used the following in my jsp: can you help? – Sachin Sharma Oct 08 '15 at 08:31
  • where is `/static/` directory located compared to your classpath? maybe changing it to `...addResourceLocations("classpath:/static/")` fixes the problem. – Ali Dehghani Oct 08 '15 at 08:44
  • webapp/static/ i did that and it is giving me : java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.views.home_jsp now :( – Sachin Sharma Oct 08 '15 at 08:45
  • http://stackoverflow.com/questions/8205069/servletexception-in-left-do-java-lang-classnotfoundexception-org-apache-jsp – Ali Dehghani Oct 08 '15 at 08:47
  • seems there might be something wrong with the way i am using the c tag from jstl..... the link doesn't resolve my issue completely.....thanks for help though. – Sachin Sharma Oct 08 '15 at 08:56
  • stupid me. i had put space between – Sachin Sharma Oct 08 '15 at 09:00