Pragmatic Programmer Issues

Springframework utils classes

Comments: 2

Today I talked with my friend about mocking classes when there are no setter for dependency. Typically when we use springframework @Autowire support, spring will inject dependency for us. Of course we can overcame this issue by Reflection API or by using groovy. Finally I thought that spring must have some utility class for that, simple search confirmed my thoughts. There is ReflectionUtils and we can simply use it. So while searching I spot many *Utils classes, maybe there is more such utility classes. I wrote a python script to figure.

Here is the script

#!/usr/bin/env python
import os
sources = r"put_your_path"
utilsClassesDict = dict()

def processFile(fileName, className):
    import fileinput
    commentSpoted = False
    for line in fileinput.input(fileName):
        if commentSpoted:
            utilsClassesDict[className] = line  
        if "**" in line:
            commentSpoted = True

def processDirectory(fileName):
    fileList = os.listdir(fileName)    
    for file in fileList:
        newFile = os.path.join(fileName, file)        
        if os.path.isdir(newFile):
        elif os.path.isfile(newFile) and ("Utils" in file):            
            processFile(newFile, file[:-5])                

def printDict():
    keys = utilsClassesDict.keys()
    for k in keys:
        print("%s %s" % (k, utilsClassesDict[k]))

if os.path.exists(sources):
    print("wrong source path %s" % sources)

And for spring 2.5.6.SEC01 it returns this list.

  • AopConfigUtils * Utility class for handling registration of AOP auto-proxy creators.
  • AopNamespaceUtils * Utility class for handling registration of auto-proxy creators used internally
  • AopProxyUtils * Utility methods for AOP proxy factories.
  • AopUtils * Utility methods for AOP support code.
  • AspectJAopUtils * Utility methods for dealing with AspectJ advisors.
  • AspectJProxyUtils * Utility methods for working with AspectJ proxies.
  • AutoProxyUtils * Utilities for auto-proxy aware components.
  • AutowireUtils * Utility class that contains various methods useful for
  • BeanDefinitionReaderUtils * Utility methods that are useful for bean definition reader implementations.
  • BeanFactoryUtils * Convenience methods operating on bean factories, in particular
  • BeanUtils * Static convenience methods for JavaBeans: for instantiating beans,
  • BindingResultUtils * Convenience methods for looking up BindingResults in a model Map.
  • BshScriptUtils * Utility methods for handling BeanShell-scripted objects.
  • ClassLoaderUtils * Utility class for diagnostic purposes, to analyze the
  • ClassUtils * Miscellaneous class utility methods. Mainly for internal use within the
  • CollectionUtils * Miscellaneous collection utility methods.
  • ConnectionFactoryUtils * Helper class for managing a JMS {@link javax.jms.ConnectionFactory}, in particular
  • DataAccessUtils * Miscellaneous utility methods for DAO implementations.
  • DataSourceUtils * Helper class that provides static methods for obtaining JDBC Connections from
  • DelegatingActionUtils * Common methods for letting Struts Actions work with a
  • DomUtils * Convenience methods for working with the DOM API,
  • ExpressionEvaluationUtils * Convenience methods for transparent access to JSP 2.0’s built-in
  • FacesContextUtils * Convenience methods to retrieve the root WebApplicationContext for a given
  • FileCopyUtils * Simple utility methods for file and stream copying.
  • FileSystemUtils * Utility methods for working with the file system.
  • FreeMarkerTemplateUtils * Utility class for working with FreeMarker.
  • HtmlUtils * Utility class for HTML escaping. Escapes and unescapes
  • JBossWorkManagerUtils * Utility class for obtaining the JBoss JCA WorkManager,
  • JRubyScriptUtils * Utility methods for handling JRuby-scripted objects.
  • JasperReportsUtils * Utility methods for working with JasperReports. Provides a set of convenience
  • JavaScriptUtils * Utility class for JavaScript escaping.
  • JdbcUtils * Generic utility methods for working with JDBC. Mainly for internal use
  • JmsUtils * Generic utility methods for working with JMS. Mainly for internal use
  • JmxMetadataUtils * Utility methods for converting Spring JMX metadata into their plain JMX equivalents.
  • JmxUtils * Collection of generic utility methods to support Spring JMX.
  • JstlUtils * Helper class for preparing JSTL views,
  • LangNamespaceUtils * @author Rob Harrop
  • LobCreatorUtils * Helper class for registering a transaction synchronization for closing
  • NamedParameterUtils * Helper methods for named parameter parsing.
  • NestedExceptionUtils * Helper class for implementing exception classes which are capable of
  • NumberUtils * Miscellaneous utility methods for number conversion and parsing.
  • ObjectUtils * Miscellaneous object utility methods. Mainly for internal use within the
  • PatternMatchUtils * Utility methods for simple pattern matching, in particular for
  • PersistenceManagerFactoryUtils * Helper class featuring methods for JDO PersistenceManager handling,
  • PortletApplicationContextUtils * Convenience methods for retrieving the root WebApplicationContext for a given
  • PortletRequestUtils * Parameter extraction methods, for an approach distinct from data binding,
  • PortletUtils * Miscellaneous utilities for portlet applications.
  • PropertiesLoaderUtils * Convenient utility methods for loading of java.util.Properties,
  • PropertyAccessorUtils * Utility methods for classes that perform bean property access
  • ReflectionUtils * Simple utility class for working with the reflection API and handling
  • RemoteInvocationUtils * General utilities for handling remote invocations.
  • RequestContextUtils * Utility class for easy access to request-specific state which has been
  • RequestUtils * Parameter extraction methods, for an approach distinct from data binding,
  • ResourcePatternUtils * Utility class for determining whether a given URL is a resource
  • ResourceUtils * Utility methods for resolving resource locations to files in the
  • RmiClientInterceptorUtils * Factored-out methods for performing invocations within an RMI client.
  • ScopedProxyUtils * Utility class for creating a scoped proxy.
  • ServletRequestUtils * Parameter extraction methods, for an approach distinct from data binding,
  • SessionFactoryUtils * Helper class featuring methods for TopLink Session handling,
  • SqlParameterSourceUtils * Class that provides helper methods for the use of {@link SqlParameterSource}
  • StatementCreatorUtils * Utility methods for PreparedStatementSetter/Creator and CallableStatementCreator
  • StringUtils * Miscellaneous {@link String} utility methods.
  • StylerUtils * Simple utility class to allow for convenient access to value
  • SystemPropertyUtils * Helper class for resolving placeholders in texts. Usually applied to file paths.
  • TagUtils * Utility class for tag library related code, exposing functionality
  • TransactionSynchronizationUtils * Utility methods for triggering specific {@link TransactionSynchronization}
  • TransformerUtils * Contains common behavior relating to {@link javax.xml.transform.Transformer Transformers}.
  • TxNamespaceUtils * @author Rob Harrop
  • TypeUtils * Utility to work with Java 5 generic type parameters.
  • UiApplicationContextUtils * Utility class for UI application context implementations.
  • ValidationUtils * Utility class offering convenient methods for invoking a {@link Validator}
  • VelocityEngineUtils * Utility class for working with a VelocityEngine.
  • WebApplicationContextUtils * Convenience methods for retrieving the root
  • WebUtils * Miscellaneous utilities for web applications.

This is quite nice list (74 utility classes), isn’t it?

Most of them are really internal for context building issues, but there are some ninjas which should be useful for us in daily job, In next post I will try to pick up some of them.




Good work Pedro! I use reflection to set @Autowired fields but I knew about ReflectionUtils. The thing is that since we have to use reflection anyway I wanted to keep my code as free from Spring-based classes as possible.



Super artykul, dzieki!