We help IT Professionals succeed at work.

Spring, Struts and JPA best practice on defining Service classes

jamve
jamve asked
on
910 Views
Last Modified: 2013-11-24
I'm relatively new to Spring and JPA, and was wondering the best practice for using Service classes to interact with Struts Actions.

Currently for every POJO entity bean defined in a Model package, I have a corresponding Service interface, and a Service Implementation class in a Service Package with the @Transactional annotation. To be able to use these in any Struts Action, I have to pass these services as arguments to the Action Bean definition in ApplicationContext.xml.

For instance, take a clases Employee, a class Department, and a class Office. All three classes have their entity bean definition, and individual Service and Service implementation classes. And definitions for all three beans in Application Context, and all are passed to the constructor-arg of an EmployeeAction bean.

Is this the recommended approach or is there a simpler way to do this? It seems that going through all these to only use a Department for a pull down list in a JSP, and assign the Department object to the Employee, and persist it, is a bit overkill.

Any hints aprreciated.
 
Comment
Watch Question

Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
mrjoltcola: thanks for your helpful input,
and with respect to auxiliary clases, say Department, Office, and their corresponding services, you would inherit this from the UserService?
I'm using struts2.1.6, so far no need to extend ActionSupport, and SessionAware, i think this is no longer required, correct?
Also, spring framework 2.0.8, doesn't seem to require the spring-services.xml.
Top Expert 2009

Commented:
>>and with respect to auxiliary clases, say Department, Office, and their corresponding services, you would inherit this from the UserService?

Department and Office are domain classes.

DepartmentService and OfficeService might be service classes.

I do not see any inheritance between UserService and the others you mentioned. We typically design a service for each functionality area of the application.

>>I'm using struts2.1.6, so far no need to extend ActionSupport, and SessionAware, i think this is no longer required, correct?


It is not required but it is recommended and useful. It provides common constants and some other convenience methods.

You are also not required to implement the *Aware interfaces, you can write explicit code instead, it is up to you. I find those interfaces help designing clean Action hierarchies for reuse.

spring-services.xml in my example is simply an include file that lists my service beans. I could have put them directly in applicationContext.xml. Same for spring-dao.xml

Using the include files helps reduce the clutter.

Author

Commented:
Here is a sample of the code in the UserAction, i was wondering if there was a simpler way to do this instaed of passing all those services to the constructor, and also in the save() method, to be converting ids into objects before saving.




    public UserAction(UserService usvc, DepartmentService dsvc, RegionService rsvc, OfficeService osvc) {
        this.service = usvc;
        this.dservice = dsvc;
        this.rservice = rsvc;
        this.oservice = osvc;
    }
    
    public String execute() {
    	this.users = service.findAll();
    	this.departments = dservice.findAll();
    	    	
    	this.countries = rservice.findLevel(Region.Level.COUNTRY);
 
    	this.offices = oservice.findAll();
    	
        return Action.SUCCESS;
    }
    
	public String save() {
		this.service.save(user);
 
		country = rservice.find(Integer.parseInt(countryid.trim()));
		user.setCountry(country);
 
		country = rservice.find(Integer.parseInt(nationalityid.trim()));
		user.setNationality(country);
		
		state = rservice.find(Integer.parseInt(stateid.trim()));
		user.setState(state);
 
		department = dservice.find(Integer.parseInt(deptid.trim()));
		user.setDepartment(department);
 
		office = oservice.find(Integer.parseInt(officeid.trim()));
		user.setOffice(office);
		
		this.service.save(user);

Open in new window

Top Expert 2009

Commented:
I did not notice the reply here but I did originally provide the working sample of a Struts2 action for wire-by-name. The asker asked a followup, but the answer is what I already posted, except I just probably needed to attempt to explain again, so I will do so for the PAQ.

Hi jamve,

My 1st and 2nd posts and code sample explains and demonstrates the answer to your followup question. If you use wire-by-name, then all you need to do is make sure the Action class has a setter for that property bean, and Spring will set it for you. Please reread those posts.

Where your code has a constructor:

  UserAction(UserService usvc, DepartmentService dsvc, RegionService rsvc, OfficeService osvc)

My sample uses a default constructor, but shows that you must provide:

   void setUserService(UserService userService) ...

and so forth. Spring will set it for you, no need to add the parameter in the spring config file. Also reread the part where I suggest using inheritance so you can add these services to the base class of your actions, so each new action does not need the setter/getter explicitly. It works very well once you get the hang of it.

I am only objecting to the Cleanup Admin's recommendation to delete, since I think the question was answered, however I am sorry I did not followup to your last question to clarify.

mrjoltcola
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.