0

From what I've seen, many people prefer to put the "business logic" in a separate module project. How is this done with a Spring 3 application?

From what I have seen, the Controllers and Services are pretty spring-specific. Is that the point? If I have opted to go with spring, is there no point in separating the "logic" and just creating the services directly (that interact with spring web flow, dao, etc)?

I am going to create an API using RESTful web services later, also through Spring. Can this all be done in ONE application, or as I mentioned, is there some way I should split up the logic?

For example - login - all handled through spring security and spring web flow and .... you guessed it .... spring. Seems difficult to modularize that.

However, say I have a service which generates a PDF given customer information .... would THAT be something I should separate?

Thanks!

Sam Levin
  • 3,326
  • 7
  • 30
  • 44

1 Answers1

1

Generally, Spring's DispatcherServlet is responsible for the REST service. It's web application context contains controllers, view resolvers, handler mapping, etc.

The application logic that is not part of the web, i.e. services, repositories, datasources, etc, is usually placed in one (or more) root application context. It is imported to the application by registering a ContextLoaderListener in the web.xml file and then configure the contextConfigLocation accordingly. If you use Spring security, the security application context is added here.

Example:

<web-app ...>
    <!-- Enables Spring root application context-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- One (or more) root application contexts -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/rootApplicationContext.xml</param-value>
    </context-param>

    <!-- Servlet declaration. Each servlet has is its own web application context -->
    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>/example/*</url-pattern>
    </servlet-mapping>
</web-app>

This setup allows the web application context to inherit any beans that are declared in the root application context(s).

Note, by convention, the web application context that is used for the specific servlet is located in /WEB-INF/[servlet-name]-servlet.xml, i.e. /WEB-INF/example-servlet.xml for the example above.

matsev
  • 32,104
  • 16
  • 121
  • 156
  • Thanks for your reply - however, I am having trouble understanding what you are saying. Can you elaborate a little more? I'm new to this, and may be biting off more than I can chew. However, I want to make sure my application is modular and structured using a best-practice approach with Maven. What is a "bean"? An EJB or a Spring bean? – Sam Levin Jun 30 '12 at 13:36
  • I refer to Spring beans. Basically, a Spring bean is a Java object whose lifecycle is handled by Spring. The beans are declared in the application context xml file(s) (or in a Java based configuration files). Spring handles the entire lifecycle of the beans, such as instantiation, dependency injection, lifecycle callbacks, etc. – matsev Jun 30 '12 at 18:07