Spring Framework: Best Programming Practices Part 5

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”/>
   4: </bean>

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.

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

   1: <beans>
   2:     <description>This file defines billing servicerelated beans and 
   3:         it depends onbaseServices.xml,which providesservice bean templates…
   4:     </description>
   5: </beans>

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.

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

   1: <beans>
   2:     <description>This file defines billing service related beans and it depends 
   3:         on baseServices.xml,which provides service bean templates…
   4:     </description>…
   5: </beans>

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>…
   3: </bean>

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”>
   3:         <value>datasource.properties</value>
   4:     </property>
   5: </bean>
   6:  
   7: <bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
   8:     <property name=”url”>
   9:         <value>${database.url}</value>
  10:     </property>
  11:     <property name=”driverClassName”>
  12:         <value>${database.driver}</value>
  13:     </property>
  14:     <property name=”username“>
  15:         <value>${database.user}</value>
  16:     </property>
  17:     <property name=”password”>
  18:         <value>${database.password}</value>
  19:     </property>
  20: </bean>

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” />
   4: </bean>

  • 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.

Technorati Tags: , ,

Advertisements

14 thoughts on “Spring Framework: Best Programming Practices Part 5

  1. karkeneab

    Using To Have The You’ve Always Dreamed Of!

    The Magic Power Coffee benefits…
    There isn’t a man out there that wouldn’t kill to better his sexual performance. Even the guys you seen in the skin flicks would give
    pretty much anything for an bigger boost, and that’s where comes in.

    a totally safe to use performance enhancement product that will change any normal fella
    into a stud between the sheets overnight.
    You don’t have to fret when using as they only include 100% natural ingredients, so there’s no need to worry about those
    dangerous effects when using them. The specially created
    blend has been carefullymade to give your body everything it needs to perform like a porn star between the sheets, and giving you those women orgasms every time.
    Clinical studies have verified the effectiveness of this supplement time and time again, so you can be sure that it’ll work just as well for you too.

    About
    Your body is basically begging for you to give it the nutrients it needs for those porn star . And, will give your
    body everything it wants with the special mix to achieve those porn star .
    So, the next time you take a lady into your bedroom, you’ll be much more confident. You’ll not only be able to blow her away, but you’ll have the best porn star
    of all time too. But, if it’s bigger erections that you are looking for, well A Male Enhancement drink
    got your back again. will give you erections that you’ve only ever dreamed about. Not only will they be rock hard, but they’ll last
    forever. You’re sexual desire will be be increased, and you’ll just keep on going all night,
    like the battery bunny. Your woman will think that she’s in a movie with the incredible performance you’re giving.

  2. Archimedes Trajano

    I don’t really find that putting IDs everywhere would increase readability. Though you should put IDs on the top level beans and whenever you are referencing them.

    I would try to avoid having too many top level beans as it makes things more complicated having to follow refs around.

  3. Mikeharvey

    Hey from Toronto, Canada

    Just a quick hello from as I’m new to the board. I’ve seen some interesting posts so far.

    To be honest I’m new to forums and computers in general 🙂

    Mike

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s