DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Dependency Injection in Spring
  • The Data Access Layer in Jmix: JPA on Steroids
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services

Trending

  • Designing for Sustainability: The Rise of Green Software
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  • Optimizing Software Performance for High-Impact Asset Management Systems
  • Designing AI Multi-Agent Systems in Java
  1. DZone
  2. Coding
  3. Frameworks
  4. Insights From Stackoverflow: Most Voted for Spring 4 Questions

Insights From Stackoverflow: Most Voted for Spring 4 Questions

Spring 4 is widely used, but as with any technology, there are loads of questions. Here are the most asked and answered questions on Stackoverflow, like differences in component and controller annotations, and what the Spring framework is actually used for.

By 
Alex Theedom user avatar
Alex Theedom
·
Feb. 20, 16 · Analysis
Likes (15)
Comment
Save
Tweet
Share
12.0K Views

Join the DZone community and get the full member experience.

Join For Free

What are the most voted for Spring 4 question asked and answered on Stackoverflow? Here are the top five questions and answers.

What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring, can they be used interchangeable or do they have specific functionality?

These stereotypes are intended to mark different layers in a multi-tier application. Components within the business, presentation and persistence layers are annotated respectively by @Service, @Component and @Repository as follows:

  • Data access components are annotated @Repository
  • service components @Service
  • controller components are annotated @Controller
  • any other component is annotated @Component.

All these annotations are themselves annotated with @Component. So the question is: Can we use @Component for all auto scanned classes? Yes and No!

All classes marked with this annotation will be auto scanned, however it would not be considered good practice as these annotations are designed to help the developer clearly communicated the intent of the component.

There is one exception to the technical interchangeability of these annotations. The @Repository marker confers special behaviour to all beans it annotates. The PersistenceExceptionTranslationPostProcessor automatically applies persistence exception translation to any bean marked with @Repository.

Consider the following paragraph from the Spring framework reference manual.

The postprocessor automatically looks for all exception translators (implementations of the PersistenceExceptionTranslator interface) and advises all beans marked with the @Repository annotation so that the discovered translators can intercept and apply the appropriate translation on the thrown exceptions.

A further consideration is that, in a future version of the Spring framework, the use of these annotation may convey special functionality relevant to their respective tier and by using these annotations appropriately you ensure that your application will benefit from such functionality with no or minimal refactoring.

The original question and answers can be viewed on the Stackoverflow website. Thank you to the following posters who answers form part of this article: akash746, nijogeorgep and Colin McCree who asked the original question.

Where does the @Transaction annotation belong? The DAO class, the service that calls the DAO or both?

Answers are divided into two camps: those who favour the annotation of classes at the service layer and those whose perfect to annotation the DAO.

The Service Layer

Traditional Spring architecture suggests that transactional semantics are located at the service level. The atomicity of the operation should inform our decision. The transaction annotation should be placed around all operations that are inseparable. Consider the classic example of a money transfer. It consists of the following calls:

  1. Credit the sends account
  2. Debit the receivers account

Both transactions must succeed or fail, so the transaction must surround both calls one and two.

If the service layer must call different methods on DAO in the data access layer in order to perform a complete operation and one of those calls fails while other success it may result in inconsistent database state. Therefore annotating around the calls made at the service layer will protect the atomicity of the operation.

For additional certainty, you may wish to annotate the debit and credit calls in the Data Access Layer by adding @Transactional(propagation = Propagation.MANDATORY). This will ensure that a transaction has been started in the caller and if no active transaction exists an exception will be thrown.

Annotating the service layer may results in longer lasting transactions than direct annotations on the DAO methods. The choice you make will depend on the level transaction isolation that the business logic requires.

DAO

There is an emerging trend towards domain-driven design. Spring Roo is a nice example of this trend.

The idea is to make the domain object a lot richer than they are in a traditional Spring architectures, usually they are anaemic, and in particular to put transaction and persistence semantics on the domain objects themselves.

In use cases where all that is needed are simple CRUD operations, the web controllers operate directly on the domain object (functioning as entities in this context), and there is no service tier.

In cases where there’s some kind of coordination needed between domain objects, you can have a service bean handle that, with @Transaction as per tradition. You can set the transaction propagation on the domain objects to something like REQUIRED so that the domain objects use any existing transactions, such as transactions that were started at the service bean.

The original question and answers can be viewed on the Stackoverflow website. Thank you to the following posters who answers form part of this article: Willie Wheeler, Michael Wiles, mnp, tweekran, djt and Thomas Einwaller who asked the original question.

What is the difference between @Inject and @Autowired? Which one to use under what condition?

The short answer: There is no different and can be used interchangeably.

In more detail the @Inject annotation is part of Java EE 7’s Context and Dependency Injection framework (JSR 346 also see JSR 365 for Java 2.0) while @Autowired is the Spring Frameworks own implementation (see Java doc).

The original question and answers can be viewed on the Stackoverflow website. Thank you to the following posters who answers form part of this article: pap and Rachel who asked the original question.

Which annotation should I use @Resource or @Autowired? What is the difference?

Both the @Autowired (or @Inject) and @Resource annotations function equally. But there is a conceptual difference:

  • the @Resource should be used to get a known resource by name. The name is extracted from the annotated setter or field, or is taken from the name the annotation’s name parameter.
  • the @Inject or @Autowired annotations attempt to inject a suitable component by type.

Essentially these are two distinct concepts. Disappointingly Spring’s Implementation of the @Resource annotation has a built-in fallback, which triggers when resolution by-name fails. It falls back to the resolution by-type as used by the @Autowired annotation. Although this fallback is convenient, it could cause confusion, as people are unaware of the conceptual difference and would tend to use @Resource for type-based injections.

The manner is which dependencies are selected is specific to the annotation. Here is how each annotation resolves injection:

@Autowired and @Inject

  1. Matches by Type
  2. Restricts by Qualifiers
  3. Matches by Name

@Resource

  1. Matches by Name
  2. Matches by Type
  3. Restricts by Qualifiers (ignored if match is found by name)

This quote from the Spring Reference Manual suggests the use of @Resource in favour of @Autowired where inject by name is preferred.

If you intend to express annotation-driven injection by name, do not primarily use @Autowired, even if is technically capable of referring to a bean name through @Qualifier values. Instead, use the JSR-250 @Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name.

@Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level. By contrast, @Resource is supported only for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.

The original question and answers can be viewed on the Stackoverflow website. Thank you to the following posters who answers form part of this article: kartik, Stephan, Ichthyo and mlo55 who asked the original question.

What is the Spring Framework for?

The Spring Framework can be describe is three ways:

  1. Spring is a framework for dependency injection: a design pattern that allows the developer to build very decoupled systems by injecting dependencies into classes.
  2. It elegantly wraps Java libraries and makes then much easier to use in your application.
  3. Included in the framework are implementations of commonly used patterns such as REST and MVC web framework which are predominately use by in web applications.

The original question and answers can be viewed on the Stackoverflow website. Thank you to the following posters who answers form part of this article: karstensrage and maksim who asked the original question.

I hope you have found this insight into the most popular questions asked on stackoverflow interesting. If you have any feedback please leave a comment. The original posting of this article can be found here: Insights from Stackoverflow: Most voted Spring 4 questions.

If you are interested in design pattern you might be interested in my new book: Professional Java EE Design Patterns. It is the perfect companion for anyone who wants to work more effectively with Java EE, and the only resource that covers both the theory and application of design patterns in solving real-world problems.

Spring Framework Annotation Service layer Insight (email client) Web Service Dependency injection Framework Data access Data access layer application

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Dependency Injection in Spring
  • The Data Access Layer in Jmix: JPA on Steroids
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!