Creating a Spring 4 MVC project with annotations and no xml files

后端 未结 4 1040
刺人心
刺人心 2021-02-06 19:22

I\'m new to Spring MVC and Hibernate. I\'m trying to start a project by following tutorials but I have been running into problems as my project structure is not consistent with

相关标签:
4条回答
  • 2021-02-06 19:40

    I Wish that this link will be helpful for you Spring security with annotation Mkyong

    0 讨论(0)
  • 2021-02-06 19:41

    The latest versions of STS integrate the Spring guides from https://spring.io/guides directly, try the "Import Spring Getting Started Content" wizard. There are good guides included for creating a Spring Boot based web service, for example, among many others.

    0 讨论(0)
  • 2021-02-06 19:52

    I know this doesn't answer your question fully, but hopefully the links will be useful.

    WebApplicationInitializer - A 100% code based approach to configuration

    as well as AnnotationConfigWebApplicationContext

    Also, if you have the time, reading the relevant sections of Spring's MVC chapter of their documentation is helpful.

    0 讨论(0)
  • 2021-02-06 19:55

    Here is a squeletal example of full java configuration. You will need :

    • a class extending AbstractAnnotationConfigDispatcherServletInitializer to replace the old web.xml file
    • one or more @Configuration annotaded class(es) to initialize the root context (replaces the old applicationContext.xml)
    • one or more @Configuration annotaded class(es) to initialize the DispatcherServlet context (replaces the old dispatcher-servlet.xml)

    This is the web.xml :

    public class WebAppConf extends AbstractAnnotationConfigDispatcherServletInitializer  {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            // declare root context configuration classes
            return new Class<?>[]{ RootConf.class };
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            // declare servlet context configuration classes
            return new Class<?>[]{ ServletConf.class };
        }
    
        @Override
        protected String[] getServletMappings() {
            // mapping of DispatcherServlet
            return new String[]{"/"};
        }
    
        @Override
        protected void customizeRegistration(Dynamic registration) {
            // additional configuration, here for MultipartConfig
            super.customizeRegistration(registration);
            MultipartConfigElement multipartConf = new MultipartConfigElement("", 200000L, -1L, 0);
            registration.setMultipartConfig(multipartConf);
        }
    }
    

    RootConf will declare business model, service and dao beans and is not shown here.

    ServletConf declares the controllers and servlet configuration :

    @Configuration
    @EnableWebMvc
    // declare where to find annotated controllers
    @ComponentScan({"org.example.web"})
    public class ServletConf extends WebMvcConfigurerAdapter {
        @Bean
        MultipartResolver multipartResolver() {
            return new StandardServletMultipartResolver();
        }
        @Bean
        ViewResolver internalViewResolver() {
            // the view resolver bean ...
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }
    

    As said above, it is squeletal, but it comes from a working minimal example so you should be able to start with that and extend it at will. In my example, the above three classes live in a org.example.config package that will never be scanned for autodetecting other configuration classes or annotated beans.

    Hope it helps ...

    0 讨论(0)
提交回复
热议问题