Welcome!

Java Authors: Don MacVittie, Maureen O'Gara, Liz McMillan, Walter H. Pinson, III, Yakov Werde

Related Topics: Java

Java: Article

Spring and Java EE 5 (PART 2)

Simplicity and power combined

While building enterprise applications you probably want your business operations to be transactional. Next we'll see how you can use Spring declarative transactions and how Java EE containers provide the integration of Spring with their Transaction Manager.

Spring-Declarative TX and Integration with Application Server TM
Java EE provides a robust platform for building and deploying a transactional enterprise application. Java EE supports both programmatic and declarative transactions and unfortunately you can use declarative transactions only with EJB. Spring has robust support for declarative transactions and you can use the scheme with any POJO, adding declarative transaction capability to the Web container.

Let's assume that you want to define a declarative transaction for a method in a Spring bean. You have the following code:

@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
public Long addBid(Bid bid) {
..
}

When the addBid() method is invoked, Spring will automatically start a new transaction per our definition. The integration between Spring and the application server's transaction manager is what makes that possible.

Java EE containers such as Oracle Containers for Java EE (OC4J) provide integration between the transaction manager and Spring so that declarative transactions can be used. You can enable such integration with a simple setting:

!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.transaction.jta.OC4JJtaTransactionManager">

The Manageability of Spring Components
While Spring components are easier to build and deploy in an application server, managing them may pose maintainability issues since Spring is a "foreign" framework. Application servers such as Oracle Application Server address this with interesting integration for managing Spring components.

For example, a Spring bean can be exposed in the MBean browser of Oracle's Application Server Control by making some minimal configuration changes in the Spring application context configuration. For example, we can register the Spring beans with the following Spring configuration:

<bean id="howToMBeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="defaultDomain" value="SpringHowTo" />
</bean>

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=empService" value-ref="empService" />
<entry key="bean:name=employeeDAO" value-ref="employeeDAO" />
</map>
</property>
<property name="server" ref="howToMBeanServer" />
</bean>

The Spring beans appear as application-defined Managed Beans (MBean) in the MBean browser of application server's management console. e.g., OracleApplication Server Control's MBean browser as shown in Figure 1. Thus managing Spring components is a snap!

You can perform MBean operations on the Spring beans deployed as a part of your application

Conclusion
This concludes the second installment of the two-part article. In this article, we provided a series of examples to illustrate how Java EE 5 developers can exploit some common integration points with the Spring Framework. Specifically, we demonstrated how EJB 3.0 and JPA combine naturally with Spring, as well as demonstrated how Spring-based applications can utilize high-end Java EE container services such as database access and the JMS messaging infrastructure. Finally you learned how application servers provide management capability for Spring beans.

More Stories By Debu Panda

Debu Panda, lead author of the recently published EJB 3 in Action (Manning Publications), is a senior principal product manager on the Oracle Application Server development team, where he drives development of the Java EE container. He has more than 15 years of experience in the IT industry and has published numerous articles on enterprise Java technologies and has presented at many conferences. Debu maintains an active blog on enterprise Java at http://www.debupanda.com.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
jcl 11/24/09 01:16:00 PM EST

Hi,thank you for this tutorial

I'm interested on the first way to intregate Spring and EJB3.

I have tried it in a example project buy it doesn't run. I'm searching since many time a solution,but nothing.

I have posted on Spring forum,but no one seems can help me.

I appreciate if you can help me.Thank you

Antonio