aspose file tools*
The moose likes Other Application Frameworks and the fly likes Spring, in Spring-Struts-Hibernate application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring, in Spring-Struts-Hibernate application" Watch "Spring, in Spring-Struts-Hibernate application" New topic
Author

Spring, in Spring-Struts-Hibernate application

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
I am to start a job next week, but I have yet to learn Spring.

I know where Struts and Hibernate stand, in a Spring-Struts-Hibernate application. However, I dont know what Spring is for in this type of application - its purpose, what modules of Spring are involved, etc.

By saying that the application is a Spring-Struts-Hibernate application, what would you guess would Spring be for in this situation. In addition, what module/s of Spring is best involved in this situation?

Do you know any web materials for these Spring module/s involved?
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Most likely its being used for Dependency Injection, possible also for some Aspect Oriented Programming.

(as special cases of these, you'll often see it handle Session/EntityManager injection and transaction management)
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Originally posted by Eric Nielsen:
Most likely its being used for Dependency Injection, possible also for some Aspect Oriented Programming.

(as special cases of these, you'll often see it handle Session/EntityManager injection and transaction management)


I noticed that dependency injection was used, like a replacement to jndi. This is possible by creating annotations right, which is possible too low-level and error-prone? What makes it worth using Spring for just this feature?

Aop can introduce many possibilities; except in ejb where there is a corresponding interceptor technology, and filter for servlets/jsps.

Transaction management and entitymanager in jee 5 can already be injected. So, the spring's Transaction management and entitymanager is functionally exactly the same as the non-spring ejb 3 entitymanager and transaction management?
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Originally posted by Jesus Angeles:


I noticed that dependency injection was used, like a replacement to jndi. This is possible by creating annotations right, which is possible too low-level and error-prone? What makes it worth using Spring for just this feature?


Yes, Dependency Injection can be used as a replacement for some uses of JNDI. However I wouldn't use JNDI to handle the assembly of the loosely coupled components that DI based systems tend to create.

Yes, there are a few places that use annotations for DI (again mainly for Session/EntityManager injection), however most of the DI is simply done on "autowire by name|type" off the public setters of an object, or explicitly by a spring context xml file. I wouldn't consider it "too low-level" I consider it "right level". I don't consider it more error-prone that other approaches, but I do acknowledge that it adds an extra hurdle when debugging to remember to check that you don't have errors in your wiring before diving down other paths.

DI, to me, is just about a required tool for creating complex systems. Especially due to the fact that it helps produce easily testable systems.

Originally posted by Jesus Angeles:

Aop can introduce many possibilities; except in ejb where there is a corresponding interceptor technology, and filter for servlets/jsps.

Transaction management and entitymanager in jee 5 can already be injected. So, the spring's Transaction management and entitymanager is functionally exactly the same as the non-spring ejb 3 entitymanager and transaction management?


1) Even if EJB solved all your persistence AOP-esque issues, you still might have AOP issues to address in logging,auditing, or security arenas.

2) While EJB3 is designed to be injectable, you still need something (Spring, Guice, etc) to actually do the injecting...


Another place that people often use Spring in the Struts/Hibernate/Spring stack is "Spring Security" formerly known as Acegi Security, one of the more comprehensive security frameworks.
[ June 17, 2008: Message edited by: Eric Nielsen ]
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
In addition to what you mentioned, I found this material discussing an example where there are 4 layers: presentation, database, business, domain model.

http://www.onjava.com/pub/a/onjava/2004/04/07/wiringwebapps.html?page=1

Presentation - struts
database - hibernate
business - spring
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9524
    
    2

With Spring integration of Hibernate, you can make your data access layer even more clean and easy. I don't know how Struts integrate with Spring but with Hibernate yes it is advantageous to use Spring integration.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Originally posted by Jesus Angeles:
In addition to what you mentioned, I found this material discussing an example where there are 4 layers: presentation, database, business, domain model.

http://www.onjava.com/pub/a/onjava/2004/04/07/wiringwebapps.html?page=1

Presentation - struts
database - hibernate
business - spring


I understand what there are getting at, but I don't particularly like their nomenclature, thought this might just be semantics.

To me:
I'd want to say more like
Presentation: JSP/FreeMarker/Velocity with support from Struts 2
Controller: Struts 2
Application: Actions (aka Commands) with support from Spring 2
Domain: POJOs/Domain Objects/ Business
Persistence: Hibernate with support from Spring 2


Giving a little more detail:
The Presentation Tier is composed of a view technology -- something that renders HTML/XML/other responses. Often this tier makes use of custom tags, etc that are provided by a controller/view framework such as Struts 1/2, Spring MVC, etc. However I wouldn't few the Controller/View | Action-based framework as providing the view tier.

The Controller Tier handles dispatching requests to commands. Its probably the only tier in many (web) applications, that doesn't get customized, explicitly, by the developer.

The Application Tier provides the "Commands" of the application. Typically these will implement an interface, or extend a class from the Controller/View framework, but shouldn't really be viewed as part of the framework. In some cases where the framework only requires a loose contract (such as Struts 2), its often quite easy to see how the same Commands in the Application Tier could be used for both a web and non-web application -- thus keeping the application tier as loosely coupled to the C/V framework as possible should be a priority if the application is likely to have both web and standalone deployments. You might need DI/AOP assistance here.

The Domain Tier is the heart of your application and shouldn't really depend on any external framework as they typically impose design constraints that hinder your ability to truly capture your domain. The domain tier might need some DI/IOC, but that's been very rare in my experience.

The Persistence Tier is responsible for making sure data is around for future interactions -- whether this means database or flat-file, etc is a moot point. In many applications this is baliwick of Hibernate. Your DAOs or Repositories will often use Spring to wire in the Session/Entity Manager and handle transactions.


My biggest objection to their layers from the onjava article is that saying a framework provides your business tier really bothers me. It doesn't highlight the core/central nature of the business/model tier that can not really be provided by a framework.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Yes, it might be a loose statement to say that Spring was used for the business layer.

It is interesting that the IOC is not introduced in JEE or other frameworks. If it is worth plugging Spring into a system just for that feature, it must be worth including into other frameworks or technologies, especially JEE, or at least the jsp/servlet specification.

I want to ask, what kind of classes/objects/resources/etc. are usually injected, in a Struts-Spring-Hibernate application?
 
 
subject: Spring, in Spring-Struts-Hibernate application