Contact Application Services

David Winterfeldt

2012


The service layer is a where business logic is located, and the persistent layer is converted to and from the JPA entities & JAXB beans using Dozer.

This example doesn't have this, but the service layer is where general handling of exceptions and validation can be put in place using AOP. This way standard Spring runtime exceptions can be thrown, but before they would go to the REST controller layer they could be translated into i18n friendly user messages. Also validation can intercept requests and immediately return results before any of the actual business logic starts processing in a service.

1. Spring Configuration

This is the Spring Dozer Mapper configuration. More than one mapping file can be passed in or wild cards can be used to load mapper configs.

A larger application may have a need to have different dozer mappers with different rules, and they could all be defined here. Then Spring injection could be controlled with custom qualifiers. An example could be if you wanted to completely copy an existing contact, but exclude primary keys from being copied. Then this new copy could be persisted as a new record.

META-INF/spring/services/mapper-context.xml
                
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="org.dozer.Mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
        <property name="mappingFiles">
            <list>
                <value>classpath:/dozer/dozer-joda-mappings.xml</value>
                <value>classpath:/dozer/dozer-mappings.xml</value>
            </list>
        </property>
    </bean>
    
</beans>
                
            

Spring configuration for loading all converters and services.

META-INF/spring/services/services-context.xml
                
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springbyexample.contact.converter,org.springbyexample.service,org.springbyexample.contact.service" />

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
          p:basenames="messages" />

</beans>
                
            

Spring configuration for security. This has the authentication manager use the default JDBC user service and activates detection of the @Secured annotation on Spring beans.

META-INF/spring/security/security-context.xml
                
<?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:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <security:global-method-security secured-annotations="enabled" />

    <security:authentication-manager>
        <security:authentication-provider>
            <security:jdbc-user-service data-source-ref="dataSource" />
        </security:authentication-provider>
    </security:authentication-manager>
    
</beans>