This is the fifth part of Spring Best Practices series as per Best practices introduction post. This fifth part is also related with the best practices when using Spring’s XML Configurations and its the continuation of Part 4. You can refer the other four posts through this links… Part 1, Part 2, Part 3 and Part 4. I am expecting the corrections (if any)as well as the new best ways from my readers
- Use ids as bean identifiers
You can specify either an id or name as the bean identifier. Using ids will not increase readability, but it can leverage the XML parser to validate the bean references. If ids cannot be used due to XML IDREF constraints, you can use names as the bean identifiers. The issue with XML IDREF constraints is that the id must begin with a letter (or one of a few punctuation characters defined in the XML specification) followed by letters, digits, hyphens, underscores, colons, or full stops. In reality, it is very rare to run into the XML IDREF constraint problem.
- Use dependency-check at the development phase
You can set the dependency-check attribute on a bean definition to a value other than the default none, such as simple, objects, or all, so that the container can do the dependency validation for you. It is useful when all of the properties (or certain categories of properties) of a bean must be set explicitly, or via autowiring.
1: <bean id=”orderService” class=”com.spring.OrderService” dependency-check=”objects”>
2: <property name=”companyName” value=”valuemometum”/>
3: <constructor-arg ref=”orderDAO”/>
In this example, the container will ensure that properties that are not primitives or collections are set for the orderService bean. It is possible to enable the default dependency check for all of the beans, but this feature is rarely used because there can be beans with properties that don’t need to be set.
- Add a header comment to each configuration file
It is preferred to use descriptive ids and names instead of inline comments in the XML configuration files. In addition, it is helpful to add a configuration file header, which summarizes the beans defined in the file. Alternatively, you can add descriptions to the description element. For example
2: <description>This file defines billing servicerelated beans and
3: it depends onbaseServices.xml,which providesservice bean templates…
One advantage of using the description element is that it is easy to for tools to pick up the description from this element.
- Communicate with team members for changes
When you are refactoring Java source code, you need to make sure to update the configuration files accordingly and notify team members. The XML configurations are still code, and they are critical parts of the application, but they are hard to read and maintain. Most of the time, you need to read both the XML configurations and Java source code to figure out what is going on.
- Prefer setter injection over constructor injection
Spring provides three types of dependency injection: constructor injection, setter injection, and method injection. Typically we only use the first two types
In this example, the orderService bean uses constructor injection, while the BillingService bean uses setter injection. Constructor injection can ensure that a bean cannot be constructed in an invalid state, but setter injection is more flexible and manageable, especially when the class has multiple properties and some of them are optional.
- Do not abuse dependency injection
As the last point, Spring ApplicationContext can create Java objects for you, but not all Java objects should be created through dependency injection. As an example, domain objects should not be created through ApplicationContext. Spring is an excellent framework, but, as far as the readability and manageability are concerned, the XML-based configuration can become an issue when many beans are defined. Overuse of dependency injection will make the XML configuration more complicated and bloated. Remember, with powerful IDEs, such as Eclipse and IntelliJ, Java code is much easier to read, maintain, and manage than XML files!
- Comments to Configuration files as well as bean files
Add a description comment to each configuration file, which explains the reason for this file and summarizes the beans defined in it.For example
2: <description>This file defines billing service related beans and it depends
3: on baseServices.xml,which provides service bean templates…
Also we can place bean documentation in the description tag.For example
1: <bean id=”aBean” class=”x.y.z.AClass”>
2: <description>Bean explanation</description>…
One advantage of using the description element is that it is easy for tools to pick up the description from this element.
- Application Context Naming Conventions
Use the following naming convention for configuration filenames:applicationContext-<a describing name>.xml This makes the naming convention applications consistent.
- Seperate deployment details from application context
Try to seperate deployment details out of application context in other xml or properties file.Example : You can specify datasource details in a property file using Spring’s PropertyPlaceHolderConfigurer or through JNDI.Property file:
1: <bean id=”propertyConfigurer” class=”org.springframework.beans. factory.config.PropertyPlaceholderConfigurer”>
2: <property name=”location”>
7: <bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
8: <property name=”url”>
11: <property name=”driverClassName”>
14: <property name=”username“>
17: <property name=”password”>
datasource.properties should contain the following entries:
1: database.url=jdbc:hsqldb:mydbdatabase.driver=org.hsqldb.jdbcDriverdatabase.user=hsqldatabase.password=passwordJndi :
2: <bean id=”myDataSource” class=”org.springframework.jndi.JndiObjectFactoryBean”>
3: <property name=”jndiName” value=”jdbc/myDataSource” />
- Use of Standard Naming convention.
Its better to use the standard Java convention for instance field names when naming beans (Hungarian Notation).
- Use <null/> for null property
Use <null/> to initialize any property to null. Although simply leaving property without setting any value defaults to null but the property might be set to some other default value within bean or if you are using auto wiring.