Tag Archives: Spring

Spring Transaction Gotcha

Previously a follow developer asked me to have a look at a strange transactional problem he was having with Spring.  He had a class like below.


public class TransactionProblem {
    public void entryPointToClass()

    private void doTransactionalProcessing() {
        //do some db stuff

He was wondering why the doTransactionalProcessing was not being executed in a transaction.  The reason is depending on the spring configuration.  Spring can use CGLIB to subclass you service class and delegate all calls to your service class.  It works something like below

public class TransactionProblemProxy extends TransactionProblem {

    public void entryPointToClass() {
        //spring starts the transaction here if the method is annotated @Transactional (its not)
        //spring commits the transaction here if the method is annotated @Transactional (its not)</pre>


So as you can see from the above code when we call our service class it actually calls the TransactionProblemProxy.  The method is not annotated transactional so no transaction is created.  The call to the private method doTransactionalProcessing is not done through spring so the annotation is ignored.The solution to the above issue is either make the doTransactionalProcessing public and call it seperately or change the spring configuration to use AspectJ.

Spring-WS Contract First Web Service Example

This post will show you how to write contract-first Web services.  Contract-first web services start with defining and creating the xsd and wsdl (the contract) before writing any java code.  During this post we will create a very simple application that will expose one web service.  All code for this application can be found at https://github.com/jwatters1981/spring-web-services

As stated previously when creating contract-first web services the first step is to create the contract i.e. the xsd.  Below is the xsd file we will be using.


Step 1 Create the XSD

<xsd:schema xmlns="http://www.springwebservices.org/product/schema/beans"

    <xsd:element name="get-product-request">
                <xsd:element name="name" type="xsd:string" />
    <xsd:element name="product-response">
                <xsd:element name="product" type="product"
                             minOccurs="0" maxOccurs="unbounded"/>

    <xsd:complexType name="product">
            <xsd:element name="code" type="xsd:string" />
            <xsd:element name="price" type="xsd:double" />
            <xsd:element name="description" type="xsd:string" />



The xsd file is pretty straight forward we have a get-product-request element which contains a string element, a product element which holds code, price and description and a product-response element which holds a list of products which will be returned from the web service method.

Step 2 Generate Java Sources
The second step is to generate java source files from the xsd, this can be done with the xjc utility which comes with java.  To run this simply open up a command prompt for windows or a terminal for mac and linux and type xjc -d generated <path to xsd> for me this was   xjc -d generated /Users/johnwatters/spring-web-services/src/main/resources/product.xsd

You can build this source code generation into your maven build process but for this post I used the command approach and copied the generated source into my project.  You would obviously not do this in a real project.

After you run the xjc command you should see output like below

parsing a schema…
compiling a schema…


An example of some of the code generated can be seen below.

@XmlType(name = &quot;product&quot;, propOrder = {
public class Product {

    @XmlElement(required = true)
    protected String code;
    protected double price;
    @XmlElement(required = true)
    protected String description;

Copy the generated source into a maven project.


Step 3 Create a Spring web service Endpoint class

Below you will see the code that makes up the web service endpoint.  In order to tell spring that this is indeed and web service endpoint you need to annotate it with @Endpoint as seen below.

public class SampleEndpoint {

    public final static String NAMESPACE = &quot;http://www.springwebservices.org/product/schema/beans&quot;;
    public final static String GET_PERSONS_REQUEST = &quot;get-product-request&quot;;

     * @param code
     * @return
    @PayloadRoot(localPart = GET_PERSONS_REQUEST, namespace = NAMESPACE)
    public @ResponsePayload ProductResponse getProducts(@RequestPayload GetProductRequest  code) {
        ProductResponse productResponse = new ProductResponse();
        Product product = new Product();
        return productResponse;


The getProducts method is annotated with the @PayloadRoot annotation to enable spring to call the required method for the soap message.  This endpoint class could potentially have many method consuming different soap messages.



Step 4 The Spring context Files

Below is the spring context file for the application.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

    <context:component-scan base-package="org.springwebservices" />

    <bean id="productService"
        p:portTypeName="Product" p:locationUri="/productService/"
        p:requestSuffix="-request" p:responseSuffix="-response">
        <property name="schema">
            <bean class="org.springframework.xml.xsd.SimpleXsdSchema" p:xsd="classpath:/product.xsd" />



        <description>Enables the MessageDispatchServlet to invoke methods
            requiring OXM marshalling.
        <constructor-arg ref="marshaller" />

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="org.springwebservices.product.schema.beans" />
        <property name="schema" value="classpath:/product.xsd" />


The important lines are line number 10 which is the component scan.  This tells spring to look for any classes that are annotated, like our @Endpoint class.  Line number 12 to 17 expose the wsdl.  Once the application is up and running its this configuration that allows you to go to http://localhost:8080/spring-web-services/productServices.wsdl and see the wsdl.

This MarshallingMethodEndpointAdapter uses the generated source code to marshal the incoming requests.

Step 5 the web.xml

The important part of the web xml is the spring-ws.  This servlet takes in our spring context file and routes the requests onto our endpoint.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         id="WebApp_ID" version="2.5">



Step  6 The Maven pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <name>Simple Spring Web Services</name>
    <description>Spring web services using JaxB.</description>




                <!--This plugin's configuration is used to store Eclipse m2e settings
                    only. It has no influence on the Maven build itself. -->


To build the project run

mvn install tomcat:run


You should be able to browse to the wsdl at http://localhost:8080/spring-web-services/productService.wsdl

You can use Soap UI to generate a soap request from the above wsdl.  The url to send the soap request to is http://localhost:8080/spring-web-services/productService

You should get the below response back

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:product-response xmlns:ns3="http://www.springwebservices.org/product/schema/beans">

All source available at https://github.com/jwatters1981/spring-web-services


Please let me know if you found the post useful