Spring Annotations

26. Are annotations better than XML for configuring Spring? 
Spring supports both XML and Annotation based configuration, and both complement each other. It all depends on the requirements and the developer’s personal opinion on which of the configurations better suits him. 
Annotations provide a lot of context in their declaration, leading to shorter and more concise configuration. All the information is in a single source file. When the class changes, you don’t have to worry about the XML file. 
However, XML excels at wiring up components without touching their source code or recompiling them. XML clearly separates the Plain Old Java Object (POJO) and its behavior. 
No matter the choice, Spring can accommodate both styles and even mix them together. 
27. What is annotation-based container configuration? 
Starting with Spring 2.5, annotation support has been added to the Spring Framework. It is now possible to configure Beans using annotations. 
XML configurations are injected after annotations. Therefore, when you use both annotations and XML based configuration, annotations configuration gets overridden by the XML ones. In the annotation-based configuration, the configuration is moved into the component class itself by using annotations on the relevant class, method, or field declaration. 

28. Explain @Component and the stereotype annotations in brief. 
@Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Contollerare specializations of @Component for more specific use cases. The @Component annotation is used on classes to indicate a Spring component. The @Component annotation marks the Java class as a bean or says component so that the component-scanning mechanism of Spring can add into the application context. The stereotype annotations are: 
@Service: This annotation is used on classes. The @Service marks a Java class that performs some service, such as execute business logic, perform calculations and call external APIs. This annotation is a specialized form of the @Component annotation intended to be used in the service layer. 
@Repository: This annotation is used on Java classes which directly access the database. The @Repositoryannotation works as a marker for any class that fulfills the role of repository or Data Access Object. This annotation has an automatic translation feature. For example, when an exception occurs in the @Repository, there is a handler for that exception and there is no need to add a try-catch block. 
@Controller: This annotation is used on Java classes that play the role of controller in your application. The@Controller annotation allows auto detection 
of component classes in the classpath and auto-registering bean definitions for them. To enable auto detection of such annotated controllers, you can add component scanning to your configuration. The Java class annotated with @Controller is capable of handling multiple request mappings.

Comments

Popular posts from this blog

Spring Data Access

Spring Model View Controller (MVC)