<p>I'm currently trying to get into the Java EE development with the Spring framework. As I'm new to Spring, it is hard to imaging how a good running project should start off.</p>

<p>Do you have any <em>best practices</em>, tipps or major <em>DO NOTs</em> for a starter? How did you start with Spring - big project or small tutorial-like applications? Which technology did you use right away: AOP, complex Hibernate...</p>

[{'answer_id': 8571, 'author': 'Michael Neale', 'author_id': 699, 'author_profile': 'https://Stackoverflow.com/users/699', 'pm_score': 2, 'selected': False, 'text': '<p>Whilst its been years since I have used spring, and I can\'t say I am a fan of it, I know that the App Fuse tool (<a href="https://java.net/projects/appfuse/" rel="nofollow noreferrer">https://java.net/projects/appfuse/</a>) has been helpful to help people bootstrap in terms of generating all the artifacts you need to get going.</p>\n'}, {'answer_id': 8576, 'author': 'GaryF', 'author_id': 1035, 'author_profile': 'https://Stackoverflow.com/users/1035', 'pm_score': 4, 'selected': False, 'text': "<p>Focus first on the heart of Spring: Dependency Injection. Once you see all the ways that DI can be used, then start thinking about the more interesting pieces like AOP, Remoting, JDBC Templates etc. So my best bit of advice is let your use of Spring grow out from the core.</p>\n\n<p>Best practice? If you're using the standard XML config, manage the size of individual files and comment them judiciously. You may think that you and others will perfectly understand your bean definitions, but in practice they're somewhat harder to come back to than plain old java code.</p>\n\n<p>Good luck!</p>\n"}, {'answer_id': 8586, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 2, 'selected': False, 'text': '<p>I actually quite liked Spring.. It was a fresh breeze of air in your average J2EE Java Beans..</p>\n\n<p>I recommend implementing the example Spring provides: </p>\n\n<p><a href="http://static.springframework.org/docs/Spring-MVC-step-by-step/" rel="nofollow noreferrer">http://static.springframework.org/docs/Spring-MVC-step-by-step/</a></p>\n\n<p>Also, I decided to go full monty and added Hibernate to my Spring application ;), because Spring provides excellent support for Hibernate... :)</p>\n\n<p>I do have a DON\'T however, which I learned the hard way (product in production)... If you only implement the Controller interface, and return a ModelAndView object with some data as provided with the interface, Spring does garbadge collect those resources, for tries to cache those data. So be careful to put large data in those ModelAndView objects, because they will hog up your server memory for as long as the server is in the air as soon as that page has been viewed...</p>\n'}, {'answer_id': 8635, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 2, 'selected': False, 'text': '<p>A good way to get started is to concentrate on the "Springframework". The Spring portfolio has grown to a big pile of projects around various aspects of Enterprise Software. Stick to the core at the beginning and try to grasp the concepts. <a href="http://www.springframework.org/download" rel="nofollow noreferrer">Download</a> the latest binaries and check out Spring\'s petclinic example once you are familiar with the core. It gives quite a good overview of the various projects SpringSource has to offer.</p>\n\n<p>Although the documentation is very good, <a href="http://dlinsin.blogspot.com/2008/05/book-review-building-spring-2.html" rel="nofollow noreferrer">I\'d recommend a book</a> after you grasp the concepts of the core. What I\'ve found problematic with the documentation, is that it\'s not in depth and can\'t give you all the details you need.  </p>\n'}, {'answer_id': 8642, 'author': 'martinsb', 'author_id': 837, 'author_profile': 'https://Stackoverflow.com/users/837', 'pm_score': 1, 'selected': False, 'text': '<p>Spring is also very much about unit testing and therefore testability of your classes. That basically means thinking about modularization, separation of concerns, referencing a class through interfaces etc.</p>\n'}, {'answer_id': 8967, 'author': 'Nicholas Trandem', 'author_id': 765, 'author_profile': 'https://Stackoverflow.com/users/765', 'pm_score': 1, 'selected': False, 'text': "<p>If you're just looking to dabble in it a bit and see if you like it, I recommend starting with the DAO layer, using Spring's JDBC and/or Hibernate support. This will expose you to a lot of the core concepts, but do so in a way that is easy to isolate from the rest of your app. This is the route I followed, and it was good warm-up before getting into building a full application with Spring.</p>\n"}, {'answer_id': 9245, 'author': 'bpapa', 'author_id': 543, 'author_profile': 'https://Stackoverflow.com/users/543', 'pm_score': 2, 'selected': False, 'text': '<p>Start here - I actually think it\'s among the best Software Dev books that I\'ve read.<br>\n<a href="http://books.google.com/books?id=L7d0LNpSrRwC&amp;dq=expert+spring+mvc+and+web+flow&amp;pg=PP1&amp;ots=GC7894yug6&amp;sig=xndzQdG2YWy27Ffx6kR1rXQwxf0&amp;hl=en&amp;sa=X&amp;oi=book_result&amp;resnum=1&amp;ct=result" rel="nofollow noreferrer">Expert Spring MVC And Web Flow</a></p>\n\n<p>Learn the new Annotation-based configuration for MVC classes. This is part of Spring 2.5. Using Annotation-based classes is going to make writing Unit tests a heck of a lot easier. Also being able to cut down on the amount of XML is a good thing.</p>\n\n<p>Oh yeah Unit Tests - if you\'re using Spring, you BETTER be Unit Testing. :) Write Unit tests for all of your Web and Service Layer classes. </p>\n\n<p>Read up on Domain Driven Design. The fact that you can use Domain Object classes at all levels of a Spring Application means you\'re going to have a VERY powerful Domain Model. Leverage it.</p>\n\n<p>However, when using your Domain Object classes for form population, you will want to take heed of the recent security concerns around the Spring Framework. <a href="http://www.theserverside.com/news/thread.tss?thread_id=50076" rel="nofollow noreferrer">A discussion on the Server Side</a> reveals the way to close the hole in the comments.</p>\n'}, {'answer_id': 13367, 'author': 'Brian Laframboise', 'author_id': 1557, 'author_profile': 'https://Stackoverflow.com/users/1557', 'pm_score': 6, 'selected': True, 'text': '<p>Small tip - I\'ve found it helpful to modularize and clearly label my Spring xml context files based on application concern. Here\'s an example for a web app I worked on:</p>\n\n<ul>\n<li><code>MyProject / src / main / resources / spring /</code>\n\n<ul>\n<li><em><strong>datasource.xml</strong></em>  -  My single data source bean.</li>\n<li><em><strong>persistence.xml</strong></em>  -  My DAOs/Repositories. Depends on <code>datasource.xml</code> beans.</li>\n<li><em><strong>services.xml</strong></em>  -  Service layer implementations. These are usually the beans to which I apply transactionality using AOP. Depends on <code>persistence.xml</code> beans.</li>\n<li><em><strong>controllers.xml</strong></em>  - My Spring MVC controllers. Depends on <code>services.xml</code> beans.</li>\n<li><em><strong>views.xml</strong></em> - My view implementations.</li>\n</ul></li>\n</ul>\n\n<p>This list is neither perfect nor exhaustive, but I hope it illustrates the point. Choose whatever naming strategy and granularity works best for you.</p>\n\n<p>In my (limited) experience, I\'ve seen this approach yeild the following benefits:</p>\n\n<p><strong>Clearer architecture</strong></p>\n\n<p>Clearly named context files gives those unfamiliar with your project structure a reasonable \nplace to start looking for bean definitions. Can make detecting circular/unwanted dependencies a little easier.</p>\n\n<p><strong>Helps domain design</strong></p>\n\n<p>If you want to add a bean definition, but it doesn\'t fit well in any of your context files, perhaps there\'s a new concept or concern emerging? Examples:</p>\n\n<ul>\n<li>Suppose you want to make your Service layer transactional with AOP. Do you add those bean definitions to <code>services.xml</code>, or put them in their own <code>transactionPolicy.xml</code>? Talk it over with your team. Should your transaction policy be pluggable?</li>\n<li>Add Acegi/Spring Security beans to your <code>controllers.xml</code> file, or create a <code>security.xml</code> context file? Do you have different security requirements for different deployments/environments?</li>\n</ul>\n\n<p><strong>Integration testing</strong></p>\n\n<p>You can wire up a subset of your application for integration testing (ex: given the above files, to test the database you need to create only <code>datasource.xml</code> and <code>persistence.xml</code> beans).</p>\n\n<p>Specifically, you can annotate an integration test class as such:</p>\n\n<pre><code>@ContextConfiguration(locations = { "/spring/datasource.xml" , "/spring/persistence.xml" })\n</code></pre>\n\n<p><strong>Works well with Spring IDE\'s Beans Graph</strong></p>\n\n<p>Having lots of focused and well-named context files makes it easy to create custom BeansConfigSets to visualize the layers of your app using Spring IDE\'s <a href="http://springide.org/project/wiki/BeansGraph" rel="noreferrer">Beans Graph</a>. I\'ve used this before to give new team members a high-level overview of our application\'s organization.</p>\n'}, {'answer_id': 1318317, 'author': 'duffymo', 'author_id': 37213, 'author_profile': 'https://Stackoverflow.com/users/37213', 'pm_score': 2, 'selected': False, 'text': '<p>"...Which technology did you use right away: AOP, complex Hibernate..." - I\'d say a better question would be to ask what people did not use right away.  I\'d add the examples you cite to that list.</p>\n\n<p>Spring MVC and JDBC template would be my starting recommendations.  You can go a very long way just with those.</p>\n\n<p>My recommendation would be to follow the Spring architectural recommendations faithfully.  Use their layering ideas.  Make sure that your web layer is completely detachable from the rest.  You do this by letting the web tier interact with the back end only through the service layer.</p>\n\n<p>If you want to reuse that service layer, a good recommendation is to expose it using Spring "contract first" web services.  If you start with the XML messages that you pass back and forth, your client and server can be completely decoupled.</p>\n\n<p>The IDE with the best Spring support is IntelliJ.  It\'s worth spending a few bucks.</p>\n'}, {'answer_id': 2792809, 'author': 'Chris J', 'author_id': 165119, 'author_profile': 'https://Stackoverflow.com/users/165119', 'pm_score': 1, 'selected': False, 'text': '<p>With the release of Spring 2.5 and 3.0, I think one of the most important best practices to take advantage of now are the Spring annotations. Annotations for Controllers, Services, and Repositories can save you a ton of time, allow you to focus on the business logic of your app, and can potentially all you to make all of your object plain old Java objects (POJOs).</p>\n'}, {'answer_id': 2792827, 'author': 'mP.', 'author_id': 56524, 'author_profile': 'https://Stackoverflow.com/users/56524', 'pm_score': 2, 'selected': False, 'text': '<p>First of all Spring is about modularity and works best if one focuses on writing small components that do one thing and do it well. </p>\n\n<p>If you follow best practices in general like:</p>\n\n<ul>\n<li>Defining an interface rather than abstract classes</li>\n<li>Making types immutable</li>\n<li>Keep dependencies as few as possible for a single class.</li>\n<li>Each class should do one thing and do it well. Big monolithic classes suck, they are hard to test and hard to use.</li>\n</ul>\n\n<p>If your components are small and follow the dogmas above they should be easy to wire up and play with other stuff. The above points are naturally also true of the Spring framework itself. </p>\n\n<p>PS</p>\n\n<p><strong>Dont listen to the points above, they are talking about how to do whatever. Its more important to learn how to think rather than how to do something. Humans can think, repeating something is not clever, thinking is.</strong></p>\n'}]
['https://Stackoverflow.com/questions/8569', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/834/']