Last Updated on July 9, 2023 by KnownSense
21. What is Annotation based Container configuration? How can we turn on annotation wiring in Spring framework?
Annotation-based container configuration is an alternative to XML setups. Rather than using XML for describing a bean wiring, the developer moves the configuration to the component class by using annotations on the appropriate class, field, or method declaration.
Because annotation wiring is turned off by default, it needs to be turned on before it can be used. It is done by configuring the <context:annotation-config/> element in the Spring configuration file.
22. What are most important annotations supported by Spring?
@Autowired – Used for autowiring bean on the setter methods, a property, constructor or methods with arbitrary names or several arguments. It provides precise control over how and where the autowiring needs to be done.
@Component – A generic stereotype for a Spring-managed component, it marks a Java class as a bean that can be picked up by a component-scanning mechanism and pull it into the application context.
@Controller – Marks a class as a Spring Web MVC controller. Beans marked with this annotation are automatically imported into the Dependency Injection container.
@Qualifier – Used along with @Autowired annotation for specifying that only one of the several yet alike beans, needs to be wired.
@Repository – A specialization of the component annotation with almost identical use and functionality. Specifically, it provides additional benefits for DAOs (Data Access Objects).
@RequestMapping – Maps a particular HTTP request method to a specific class or method in controller responsible for handling the respective request.
@Required – Applied to bean property setter methods, it indicates that the bean property needs to be populated at the configuration time with the use of an explicit property value present in a bean definition or via autowiring. In case the bean property is not populated, the container throws the BeanInitializationException message.
@Service – Another specialization of the component annotation. Although it doesn’t offer any additional behavior over the component annotation, it can be used over the @component annotation in service-layer classes for specifying the intent in a better way.
23. Difference between Constructor and setter injection?
Constructor Injection | Setter Injection |
There is no partial injection. | There can be partial injection. |
It doesn’t override the setter property. | It overrides the constructor property. |
It will create a new instance if any modification is done. | It will not create new instance if any modification is done. |
It works better for many properties. | It works better for few properties. |
24. Differentiate between BeanFactory and ApplicationContext?
BeanFactory | ApplicationContext |
It is an interface defined in org.springframework.beans.factory.BeanFactory | It is an interface defined in org.springframework.context.ApplicationContext |
It uses Lazy initialization | It uses Eager/ Aggressive initialization |
It explicitly provides a resource object using the syntax | It creates and manages resource objects on its own |
It doesn’t supports internationalization | It supports internationalization |
It doesn’t supports annotation based dependency | It supports annotation based dependency |
25. How many bean scopes are supported by Spring?
The Spring Framework supports five scopes. They are:
- Singleton: This provides scope for the bean definition to single instance per Spring IoC container.
- Prototype: This provides scope for a single bean definition to have any number of object instances.
- Request: This provides scope for a bean definition to an HTTP-request.
- Session: This provides scope for a bean definition to an HTTP-session.
- Global-session: This provides scope for a bean definition to an Global HTTP-session.
26. What do you understand by Aspect Oriented Programming (AOP)?
Enterprise applications have some common cross-cutting concerns that are applicable to different types of Objects and application modules, such as logging, transaction management, data validation, authentication, etc. The modularity of application is achieved by classes in Object-oriented programming. In AOP, application modularity is achieved by Aspects and they are configured to cut across different class methods.
AOP takes out the direct dependency of cross-cutting tasks from classes that are not possible in normal object-oriented programming. For example, we can have a separate class for logging but the other classes will have to call these methods. But, in AOP we configure the aspects and method execution happens automatically
27. What do you mean by Spring DAO ?
The Spring DAO support eases working with data access technologies, such as JDBC, JDO, and Hibernate, in a reliable way. Also, it allows coding without worrying about catching specific-technology exceptions and easily makes a switch amongst persistence technologies.
28. How to access Hibernate using Spring
Hibernate can be accessed using Spring Framework in the following two ways:
Extending HibernateDAOSupport and then applying an AOP Interceptor node
Inversion of Control with a Hibernate Template and Callback.
29. Difference between Spring AOP and AspectJ AOP?
AspectJ is the industry-standard implementation for Aspect-Oriented Programming whereas Spring implements AOP for some cases.
The main differences between Spring AOP and AspectJ are:
- Spring AOP is simpler to use than AspectJ because we don’t need to worry about the weaving process.
- Spring AOP supports AspectJ annotations, so if you are familiar with AspectJ then working with Spring AOP is easier.
- Spring AOP supports only proxy-based AOP, so it can be applied only to method execution join points. AspectJ support all kinds of pointcuts.
- One of the shortcomings of Spring AOP is that it can be applied only to the beans created through Spring Context.
30. What are the types of transaction management supported by Spring?
Spring Framework provides support for two types of transaction management:
- Declarative transaction management – While the transaction management is separated from the business code, only annotations or XML-based configurations are used for managing transactions.
- Programmatic transaction management – The transaction is managed with programming. Although extremely flexible, it is very difficult to maintain.
Authored by codingknownsense.com