I put my services in the spring-service.xml class, and Spring picks them up and wires them if Spring is the container for the Action classes.
Sample Action class below.
Main Topics
Browse All TopicsI'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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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.
>>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.
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
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
Business Accounts
Answer for Membership
by: mrjoltcolaPosted on 2009-07-03 at 10:29:25ID: 24773589
More often I see (and use) Spring wire-by-name so there is no need to specifically add each one to the applicationContext.xml for each action. Spring will do it for you, by examining the properties of each POJO. Just declare a setter method in the Action class for the service bean, and it will get called. I use wire-by-name exclusively.
Also, if there are services that you use in all or most Actions, use inheritance to declare the setters / properties in base Action classes. In my apps I commonly use base classes for a User domain object and UserService class, because 99% of the actions require an authenticated User to be in context. The mechanism for managing the User object in the session is hidden in the base class. Not sure if you mean Struts1 or 2, but in Struts2 I use the SessionAware interface to hide those things in the base classes, so there is no explicit session code in the leaf Action classes.