Benefits of JavaConfig over XML configurations in Spring?

前端 未结 2 405
南方客
南方客 2020-12-02 20:23

Earlier the configurations used to be in hard coded in the code, later it was externalized to .property files (for sake of avoiding hard coded values, avoiding changing code

2条回答
  •  一整个雨季
    2020-12-02 20:38

    Correct answer was given here, Other-than that answer, I give one more point

    According to the Spring 5 reference

    XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written. These days many developers choose Java-based configuration for their Spring applications.

    it means nowadays, people are moving towards java based config

    for example: Spring web-mvc config in xml

    
        
        
        
    
        
    
        
            
                /WEB-INF/pages/
            
            
                .jsp
            
        
      
    

    and same config in java based style

    @Configuration
    @EnableWebMvc
    
    @ComponentScans({
            @ComponentScan("controller"),
            @ComponentScan("dao"),
            @ComponentScan("service")      
    })
    public class WebMVCConfig implements WebMvcConfigurer {
    
        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setViewClass(JstlView.class);
            viewResolver.setPrefix("/WEB-INF/pages/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }
    }
    

    what is easy to understand? Obviously, the Java-based config is easy to understand.

    people who write codes and others can easily understand java code than XML. and you do not need to know about XML if you only know Java.

提交回复
热议问题