Spring Framework: Best Programming Practices Part 2

 

This is the second part of Spring Best Practices series as per Best practices introduction post. This second part is also related with the best practices when using Spring’s Core classes and other utilities. You can refer the other four posts through this links… Part 1, Part 3, Part 4 and Part 5. I am expecting the corrections (if any)as well as the new best ways from my readers.

  • Configuring Spring Application context for different locations:

If the configuration is the same for all the environments except for the developer’s machine, then make (a) separate configuration file(s) with the configuration that is different. Let this different configuration overwrite the definition(s) in the original file(s).Make sure this different configuration will never be placed in the other environments!!!Example of how to achieve this if you use the org.springframework.context.support.ClassPathXmlApplicationContext:Note: I give every example file below a unique name, so I can easily refer to it.Contents of beanRefContext.xml:<beans><bean id=”aBeanId”class=”org.springframework.context.support.ClassPathXmlApplicationContext”><constructor-arg><list><value>/spring/applicationContext-forAllTheEnvironments.xml</value><value>/spring/applicationContext-local.xml</value></list></constructor-arg><constructor-arg><ref bean=”frameworkApplicationContextId”/></constructor-arg></bean></beans>Beans defined in the config locations (first constructor-arg) overwrite the beans in the parent application context (second constructor-arg).Quote from the JavaDoc of this class “In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

  • Prefer static pointcut over dynamic point cut :

In Spring’s book of definitions static point cut refers to a pointcut that can be evaluated when a proxy is created. Criteria for static point cuts can not changed afterwards. Dynamic point cuts depend on runtime information such as arguement values or call stack.Dynamic pointcuts are slower to evaluate than static pointcuts and allow less potential for optimization. It’s always necessary to evaluate them on each invocation.

  • Use regular expression advisors to fine tune interceptor scope

Instead of using broad method point cuts and filtering target methods in interceptor, use more sophisticated and elegant regular expression at the application context level. Spring supports three types of regex method point cuts:· org.springframework.aop.support.JdkRegexpMethodPointcut : Java 1.4 regular expression for the fully-qualified method names to match.· org.springframework.aop.support.Perl5RegexpMethodPointcut : Perl5 regular expression for the fully-qualified method names to match· org.springframework.aop.support.RegexpMethodPointcutAdvisor : Convenient class for regexp method pointcuts that hold an Advice, making them an Advisor. By default, JdkRegexpMethodPointcut will be used on JDK 1.4+, falling back to Perl5RegexpMethodPointcut on JDK 1.3 (requiring Jakarta ORO on the classpath).Example:<bean id=”crudInterceptor” class=”com.mycompany.CrudInterceptor”/><bean id=”crud”class=”org.springframework.aop.support.RegexpMethodPointcutAdvisor”><property name=”advice”><ref local=” crudInterceptor “/></property><property name=”patterns”><value>.*create.*,.*destroy.*,.*get.*,.*update.*</value></property></bean>

  • Use autoproxying for large applications

ProxyFactoryBean works well for small application but it requires more verbose configuration. It allows control over every aspect of the proxy. Spring ease the use of ProxyFactoryBean by providing dedicated proxies such as TransactionProxyFactoryBean and LocalStatelessSessionProxyFactoryBean. Proxies also saves code duplication in configurations.Example:<bean id=”myProxy” class=”org.springframework.aop.framework.ProxyFactoryBean” abstract=”true”><property name=”interceptorNames”><list><value>interceptor1</value><value>interceptor2</value></list></property></bean><bean id=”mybean” parent=”myProxy”><property name=”proxyInterfaces”><value>com.mycompany.mybean</value></property><property name=”target”><bean class=”com.mycompany.MyBeanImpl”><property name=”name”><value>Dave</value></property><property name=”description”><value>Good Boy</value></property></bean></property></bean>Here number of child bean definitions can “extend” the myProxy definition, specifying a target, usually inner bean. They can optionally add further properties, such as proxy interfaces. Autoproxying means that depending on some configuration, proxying is applied consistently to a number of objects. Autoproxy can be created using BeanNameAutoProxyCreator or DefaultAdvisorAutoProxyCreator. BeanNameAutoProxyCreator is good for replacing several ProxyFactoryBean. DefaultAdvisorAutoProxyCreator is powerful. It examines all advisors defined in current context for mathcing pointcut methods on target object. Both advisor and advices and be used in BeanNameAutoProxyCreator interceptor list. DefaultAdvisorAutoProxyCreator strictly requires Advisors.Examples:BeanNameAutoProxyCreator :<bean id=”bnProxyCreator” class=”org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator”><property name=”beanNames”><value>bean1,mybean*</value></property><property name=”interceptorNames”><list><value>advisor1</value><value>interceptor1</value></list></property></bean>DefaultAdvisorAutoProxyCreator :<bean id=”daaProxyCreator” class=”org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator”/ >An autoproxy creator bean definition is intended to change the effect of other bean definitions, not for access by application code or other framework objects. Autoproxy can be used to define transactions as well. 15. Specify Transaction Rollback: By default transactions are rolled back only on runtime exceptions and not on checked exceptions. However it is a good practice to choose specific checked exceptions where you want the transaction to be rolled back based on business needs. Exceptions marked with negative (-) sign cause transaction to be rolled back.Example:<bean id=”businessBean” class=”org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource”><props><prop key=”businessMethod1″>PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ,-RollBackBusinessException</prop></props></property> </bean>You can even mark runtime exceptions as positive to prevent rollbacks but be careful while doing so as it could have adverse affects.

  • Aware of thread safe issues with AOP advice

Advice instances are most often shared among threads, so we need to consider thread safety issues. For example if the method interceptor is responsible for generating unique id or count, then consider using ThreadLocal variable with synchronized method for incrementing the count.Example:public class CountingMethodAdvice implements MethodInterceptor {// The next serial number to be assignedprivate static long nextSerialNum = 0;private static ThreadLocal serialNum = new ThreadLocal() {protected synchronized Object initialValue() {return new Long(nextSerialNum++);}};private static long getCount() {if(serialNum!=null){return ((Long) (serialNum.get())).longValue();}}public Object invoke(MethodInvocation methodInvocation) throws Throwable {System.out.println(”This method is called ” + getCount + ” times”);}}

Technorati Tags: , ,

+