Lightweight Remoting Hessian and Burlap
An RMI environment can be cumbersome to set up, and uses arbitrary network ports that are not firewall-friendly. WSDL SOAP-based web services are hard to export in a portable fashion, and waste bandwidth because of their verbose protocol. Lightweight HTTP-based remoting protocols address both these issues They can be run on top of any standard Servlet container, and will work across any firewall. While there has been much talk about HTTP-based web services, it has focused on WSDL and SOAP with...
JDBC Access via a Template
The JdbcTemplate class and its associated helpers in the org.springframework.jdbc.core package apply a template style to JDBC handling. A JdbcTemplate instance is configured with a javax.sql.DataSource, and offers various query and update methods to perform data access. A simple update statement using a JdbcTemplate can look as follows JdbcTemplate jdbcTemplate new JdbcTemplate dataSource jdbcTemplate.update UPDATE products SET pr_price 1100 WHERE pr_price 1000 When a query returns a ResultSet...
Declarative Transaction Management
Probably the most valuable service of SLSBs is container-managed transactions CMT . Although the J2EE server, rather than its EJB container, is responsible for delivering transaction coordination via JTS JTA, SLSBs enable us to use declarative, rather than programmatic transaction management. That is, we can instruct the EJB container to delimit transactions outside Java code. Of course, if we want to roll back transactions, we'll need to use the EJB API, but in the happy path we won't need to...
Model and View Handling Nmu
To leverage DispatcherServlet's MVC support, a Controller can return an org.springframework .web.servlet.ModelAndView object, gathering The symbolic name of a view, to be resolved by a view resolver see below , or an instance of the org.springframework.web.servlet.View interface in which case view resolution is unnecessary . A java.util.Map of model attributes, with model object names as keys and model objects as values, to be exposed to the view in the case of a JSP, as request attributes . A...
RDBMS Operation Objects
The Spring Framework also offers a higher level of JDBC abstraction reusable RDBMS operation objects, representing queries, updates, or stored procedures. The RdbmsOperation class hierarchy can be found in the org.springframework.jdbc.object package subclasses include SqlUpdate, MappingSqlQuery, and StoredProcedure. Like JdbcTemplate instances, such objects can be created once and held as instance variables of DAO implementations, as they are also reusable and thread-safe. Using this approach,...
Setup within a Spring Context Qai
Client code can either explicitly work with a JAX-RPC service or with the service endpoint interface of a specific port. The former is appropriate when multiple ports are needed in the same client objects, or for access via the Dynamic Invocation Interface. The latter is preferable when clients want to access a specific endpoint via a Java service interface the commonest requirement , avoiding confronting them with JAX-RPC concepts. In a Spring bean factory or application context , a reference...
RMI Invoker for Transparent Remoting
A different flavor of RMI is built on sending method invocations through an RMI invoker via Java reflection. From the RMI point of view, each exported RMI service is of the invoker type, with a common remote interface and stub skeleton pair that just features a single invoke method. Each exported business service is wrapped with an RMI invoker service. With this model, remote-enabled services can implement any Java interface There is no need to derive the service interface from java.rmi.Remote,...
Stateless Session Beans
The classic J2EE solution is to model all business components as EJBs in particular, Stateless Session Beans and define all resources via JNDI, deferring configuration and instance management to the EJB container Letting the container initialize business objects on startup Configuring them via JNDI environment parameters Keeping them in the container's JNDI environment Accessing them via JNDI lookups For example, the ad hoc servlet from above could access a MyService SLSB as follows public...
Info Slo
Using a layer of transfer objects to hold disconnected data, even in co-located applications. This is usually best avoided, as it forces us to write fake objects and wastes a key advantage of a co-located architecture. Using a data access API such as Hibernate or JDO 2.0 that allows for disconnection and reconnection of persistent objects. As with transfer objects, this requires us to materialize all necessary relationships to support the current use case. Avoiding technologies such as entity...
Checked Exceptions and Advice
Checked exceptions are problematic with AOP, where advice, rather than application code, is concerned. There is no problem in having business objects that will be advised throw checked exceptions. So AOP needn't affect the signatures of your business objects. Typically you should choose whether or not to make an application checked based on whether it's likely to be recoverable potential use of AOP is irrelevant. However, an aspect can't introduce new checked exceptions of its own. So, in our...
Simplified Setup TransactionProxyFactoryBean
For the typical usage of transaction-enabling a certain object, Spring offers an alternative, simplified way of proxy configuration. TransactionProxyFactoryBean allows us to define transaction semantics for a particular target, without having to set up a standard AOP ProxyFactoryBean and a Transaction Interceptor. All relevant configuration is contained in a single transaction proxy definition lt bean id myTransactionManager lt bean id myBusinessObjectTarget lt bean id myBusinessObject lt...
Using the ProxyFactoryBean
The basic way to create an AOP proxy in Spring is to use org.springframework.aop.framework.Proxy FactoryBean. This gives complete control over the pointcuts and advice to apply, and their ordering. There are also simpler options that are preferable if you don't need such control, which I'll discuss later. We need to perform the following basic steps to advise an object using the Spring ProxyFactoryBean class Define the necessary pointcuts, interceptors, or other advice as objects in the...
JtaTransactionManager
If you want to delegate transaction management to your J2EE container, use Spring's JTA transaction strategy delegates to the JTA subsystem of the J2EE container, providing the ability to manage distributed transactions that span more than one resource. This strategy depends on running in a J2EE application server environment Spring provides only a means of tapping into the application server's transaction coordination, and does not perform transaction coordination itself. Transactional...
Factory Beans
A Spring bean factory supports a special concept for creating custom bean instances factory beans, implementing the interface. A factory bean introduces a level of indirection, so that a bean defined in the bean factory can act as factory for another object. The object that the factory bean creates will be available for bean references thus, the factory bean will behave just like the object it produces on getBean calls and lt ref gt tags. The FactoryBean interface defines a getObject method...
ThreadLocalPooling
There is normally only one appropriate size for the instance pool of a business object such as a stateless session bean the maximum number of threads that will need to access it simultaneously. Any more objects in the pool than this, and they can't all be in use simultaneously any fewer and there's possibly contention. This suggests a radically different approach to instance pooling. What if we take the view that thread management should drive management of instance objects This means that we...
DataSourceTransactionManager
If you need to perform data access only on a single database via JDBC, use org.springframework.jdbc . It performs transactions on a single javax.sql .DataSource, via thread-bound JDBC Connections that it manages internally. This strategy does not depend on a J2EE environment a potentially important benefit, as it can be used without any form of J2EE container. The DataSource to use can be locally defined for example, a Jakarta Commons DBCP BasicDataSource, or a non-XA container DataSource...
AspectJ
The most complete AOP implementation is AspectJ a language extension to Java that treats AOP concepts as first-class citizens of the language. AspectJ provides its own compiler. Unlike other AOP frameworks, which use standard Java and move pointcut definition into XML files, AspectJ wholly uses Java AspectJ code rather than metadata. There are some advantages to this approach in particular, it's type safe in some cases. However, it also has some disadvantages, as we'll see. AspectJ has many...
HibernateTemplate Convenience Methods
Beyond a plain execute method, the Spring HibernateTemplate class offers various convenience methods for single-step actions. This is particularly useful for returning disassociated objects to a web controller or remote caller, or reassociating incoming objects with Hibernate. With such a convenience method, the previous query example could look as follows HibernateTemplate hibernateTemplate new HibernateTemplate sessionFactory List products hibernateTemplate.find FROM example.Product WHERE...
The Middleware Company mPetStore February 2003
The Middleware Company's mPetStore www.middleware-company.com casestudy was built for a performance comparison case study. It is based on original Sun Pet Store, with the following changes to improve performance and reflect community criticism of the original Sun implementation Switching from BMP entity beans to CMP 2.0 entity beans. This produced a significant performance improvement. Switching from remote to local session bean interfaces. This produced no difference in performance, as target...