Spring MVC, two instances of configuration instead of one

前端 未结 2 2080
别那么骄傲
别那么骄傲 2020-12-30 18:21

I\'m starting to learn about Spring MVC. I\'m trying to get rid of all Spring XML configuration. Here is my web.xml:



        
2条回答
  •  温柔的废话
    2020-12-30 18:46

    The problem here is that you are using the same config for the servlet and the root context. Thats why you have two instance of the configuration. In Spring MVC, you have 2 contexts, the servlet context and the root context. The servlet context is for your controllers and the root context for your business objects and your services.

    If you don't want to use XML, create two config classes. Something like this :

    Root context :

    @Configuration
    @EnableTransactionManagement
    @ComponentScan("pl.mbrnwsk.sklep")
    public class AppConfiguration {
    
        public String hbm2ddl_auto = "update";
    
        public AppConfiguration(){
            System.out.println("AppConfiguration");
        }
    
        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource ds = new DriverManagerDataSource();
            ds.setDriverClassName("org.hsqldb.jdbcDriver");
            ds.setUrl("jdbc:hsqldb:file:/SklepDB/");
            ds.setUsername("SA");
            ds.setPassword("");
            return ds;
        }
    
        @Bean
        public SessionFactory sessionFactory() {
            LocalSessionFactoryBuilder ss = new LocalSessionFactoryBuilder(dataSource());
            ss.scanPackages("pl.mbrnwsk.sklep.model");
            ss.setProperty("hibernate.show_sql", "true");
            ss.setProperty("hibernate.hbm2ddl.auto", hbm2ddl_auto);
            ss.setProperty("hibernate.dialect",
                    "org.hibernate.dialect.HSQLDialect");
            return ss.buildSessionFactory();
        }
    
        @Bean
        public PlatformTransactionManager txManager(){
            return new HibernateTransactionManager(sessionFactory());   
        }
    }
    

    Servlet context :

    @Configuration
    @ComponentScan("pl.mbrnwsk.sklep.controller")
    public class ServletConfiguration {
    
        public AppConfiguration(){
            System.out.println("ServletConfiguration");
        }
    
        @Bean
        public ViewResolver viewResolver(){
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setPrefix("/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }
    }
    

    Web.xml :

    
    
          
          
              contextClass
              
                  org.springframework.web.context.support.AnnotationConfigWebApplicationContext
              
          
    
          
          
              contextConfigLocation
              pl.mbrnwsk.sklep.config.AppConfiguration
          
    
          
          
              org.springframework.web.context.ContextLoaderListener
          
    
          
          
              dispatcher
              org.springframework.web.servlet.DispatcherServlet
              
              
                  contextClass
                  
                      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
                  
              
              
              
                  contextConfigLocation
                  pl.mbrnwsk.sklep.config.ServletConfiguration
              
          
    
          
          
              dispatcher
              /
          
    
    

提交回复
热议问题