Spring bean configuration best practices

In this section, we will see some of the best practices for configuring a bean in Spring:

  • Use ID as bean identifiers:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">

<!-- Bean Configuration definition describe here -->
<bean id="xxx" name="xxx" class=""/>

</beans>

In the preceding example, we identified the bean using id or name. We should use id to pick the bean instead of name. Usually, it does neither increase readability nor benefit any performance but it's just an industry standard practice which we need to follow.

  • Prefer type over index for constructor argument matching. The constructor argument with index attribute is shown as follows:
<constructor-arg index="0" value="abc"/>
<constructor-arg index="1" value="100"/>
  • The constructor argument with the type attribute is shown as follows: 
<constructor-arg type="java.lang.String"
value="abc"/>
<constructor-arg type="int" value="100"/>

As per the previous example, we can use index or type as a constructor argument. It is better to use the type attribute instead of index in a constructor argument because it is more readable and less error-prone. But sometimes, type-based arguments might create an ambiguity problem when a constructor has more than one argument of the same type. In that case, we need to use index or a name-based argument.

  • Use dependency check at the development phase: In bean definition, we should use the dependency-check attribute. It ensures that the container performs explicit dependency validation. It is useful when all or some of the properties of a bean must be set explicitly, or through auto wiring.
  • Do not specify version numbers in Spring schema references: in Spring configuration files, we specify the schema reference for different Spring modules. In schema references, we mention the XML namespace and its version number. Specifying the version number is not mandatory in the configuration file, so you can skip it. In fact, you should skip it all the time. Consider it as a best practice to follow. Spring automatically picks the highest version from the project dependencies (jars). A typical Spring configuration file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">

<!-- Bean Configuration definition describe here -->
<bean class=""/>

</beans>

As per best practices, this can be written like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">

<!-- Bean Configuration definition describe here -->
<bean class=""/>

</beans>

Add a header comment to each configuration file; it is preferred to add a configuration file header that describes the beans defined in the configuration files. The code of the description tag is as follows:

<beans>
<description>
This file defines customer service
related beans and it depends on
accountServices.xml, which provides
service bean templates...
</description>
...
</beans>

The advantages of the description tag is that some tools may catch up the description from this tag to help you in other places.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset