views:

139

answers:

2

I am starting out with web development under Java EE and spring and have a high level design question:

I have a basic shell set up: I have coded some domain objects (mainly getters and setters for various object attributes which I ultimately want displayed on the page). I am wondering what the proper way to tie everything together is once I have some DAOs coded.

Assuming a simple blog with various blog entries. I have coded a class for the blog entries containing the appropriate attributes corresponding to an entry. What is the appropriate flow for the controller to access these domain objects, call the appropriate DAO, pack the data into the model and invoke the view?

As you can tell I am pretty confused as to how this all fits together, and how tiered architectures should work. Please let me know if any clarification is needed. Thanks.

Update: Thanks for the answers below! I have another question: should my service layer classes be singletons? Could someone please explain why they should/shouldn't be singletons? Thanks!

+1  A: 

Controller (use Annotations in 2.5+ so that it's a POJO) performs a method on a Service Layer class, which is injected. The Service Layer is injected with a DAO to get objects from persistence. The business logic is inside of the Domain Objects.

bpapa
Thanks, this is exactly what I was looking for. Unfortunately I still need a few details. Could you elaborate (or point me to some resources) on how exactly all this looks? Specifically how the injections looks etc. Also is it standard to have business logic inside of the domain objects?
nick
This book is pretty amazing I think, but when I read it was pre-2.5, so no mention of annotations. It may have been updated since, I'm not quite sure - http://www.amazon.com/Expert-Spring-MVC-Web-Flow/dp/159059584X
bpapa
A: 

If you haven't done so already, check out the Spring MVC step-by-step tutorial.

Spring MVC tutorial

This will give you an excellent example of how to construct a simple Spring MVC web application.

The only problem is it doesn't cover using Annotations in Spring 2.5+. You'll have to either wait for the Spring 3.0 MVC tutorial to release, or dig through the official Spring documentation yourself to learn how to use it.

EDIT

Here's an example involving controlling a form:

Note: Spring 3.0.0

Your controller:

@Controller
@RequestMapping("/blog.htm")
public class BlogController{


  // Service layer class
  private final BlogManager blogManager;

  // Inject the blog manager into the constructor automatically
  @Autowired
  public BlogController(BlogManager blogManager){
    this.blogManager = blogManager
  }


  // Set up the form and return the logical view name
  @RequestMapping(method=RequestMethod.GET)
  public String setupForm(ModelMap model){
    model.addAttribute(new EntryForm());
    return "addBlog";
  }

  // Executed when posting the form
  @RequestMapping(method=RequestMethod.POST)
  public String addBlog(@ModelAttribute("entryForm")EntryForm entryForm){
    // Read the entry form command object from the form
    String text = entryForm.getText();

    // Call your service method
    blogManager.addEntry(text);

    // Usually redirect to a new logical view name
    return "redirect:/homepage";
  }  

}

Form command object:

public class EntryForm{

  private String text;

  // setters and getters for text
}

Here is your service layer class

public class BlogManager{

  private final BlogDAO blogDAO;

  @Autowired
  public BlogManager(BlogDAO blogDAO){
    this.blogDAO = blogDAO;
  }

  public void addEntry(String text){
    int blogID = 100;  // simple example id for a blog
    Blog blog = blogDAO.findById(blogID);
    blog.addEntry(text);
    blogDAO.update(blog);
  }

}

And now you update your spring.xml file to tie it all together

<?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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;


 <!-- Scan for spring annotations -->
 <context:component-scan base-package="test.package"/>

 <!-- defined in the xml file and autowired into controllers and services --> 
 <bean id="blogManager" class="test.package.BlogManager" />
 <bean id="blogDAO" class="test.package.dao.BlogDAO" />

 <!-- Resolves logical view names to jsps in /WEB-INF/jsp folder -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /property>
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>


</beans>
Christian
I breifly went through this tutorial. However, I didn't find any notion of a Service layer. I'd like to follow the suggestions of bpapa.
nick
@nick, If you didn't make it to Chapter 3, you wouldn't have found anything about a Service layer. http://static.springsource.org/docs/Spring-MVC-step-by-step/part3.html I edited my answer to include a simple example of what the service layer may look if implemented using Spring 3.0.0.
Christian
thank you very much for this...quick question: should service layer classes be singletons?
nick
@nick, if you haven't already, go ahead and ask a new question about the singletons.
Christian