53

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 for the sake of changing configurations..etc) then it moved to XML (for sake of being more standardized, error free..etc)

Now, while reading about @Configuration in Spring 3 , looks like we are again moving back to the initial approach.

Why would we want to hard-code configurations in the code rather than having it externalized ?

yathirigan
  • 5,619
  • 22
  • 66
  • 104

2 Answers2

43

There are some advantages

  1. Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.
  2. XML based on configuration can quickly grow big. [Yes we can split and import but still]
  3. Search is much simpler, refactoring will be bliss. Finding a bean definition will be far easier.

There are still people who like XML configuration and continue to do it.

References: Java configuration advantages Some more reasons

Narain
  • 872
  • 9
  • 11
  • 2
    It depends on what kind of configurations are loaded in Java classes. As far as we see, having all the configurations in one place is always safe. We should think everything in broader perspective or large scale. Without building the application we could make the application to behave in different ways in different environment. – prashanth-g Apr 03 '17 at 06:07
  • 2
    @prashanth-g Monolithic configuration files? Large scale? I don't think that computes. Imagine multiple configuration changes from multiple developers on different modules. I'd rather have each changes made in small modularized configuration files than in one place. – jonasespelita Jan 26 '18 at 06:59
  • @jonasespelita That was a Monolithic app in a multi-clustered environment. At this setup, we felt that having certain configurations like IP, URLs etc. in XML file would be a better solution. But it depends on the need and environment that you have. Further check https://stackoverflow.com/questions/182393/xml-configuration-versus-annotation-based-configuration – prashanth-g Jan 27 '18 at 10:51
  • @prashanth-g guess original question was about advantages on Java config. It's not about whether one is better over other. As both are supported u can make a choice. Are we still doing monolithics ? – Narain May 15 '18 at 19:06
1

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

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <context:component-scan base-package="controller" />
    <context:component-scan base-package="dao" />
    <context:component-scan base-package="service" />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>  

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.

majurageerthan
  • 2,169
  • 3
  • 17
  • 30
  • 1
    gr8 example. But what about the config for different environments. That is not possible post compile time any longer, which is a major drawback – samshers Aug 29 '19 at 08:08
  • 1
    java based configuration is useless, the main good thing about spring is the decoupling from java so you can change configuration without recompilation or rebuild – Enerccio May 15 '21 at 07:23