Spring Framework is an open source java platform. It provides a comprehensive programming and configuration model for modern Java-based enterprise applications – on any kind of deployment platform. Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.
Spring is an open source development framework for enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based programming model. Spring Framework is built on top of two design concepts – Dependency Injection and Aspect Oriented Programming. Some of the features of spring framework are: Some of the advantages of using Spring Framework are: Following are the modules of the spring framework: We can use Spring XML based as well as Annotation based configuration to implement DI in spring applications. BeanFactory is the basic container whereas ApplicationContext is the advanced container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with spring AOP, message resource handling for i18n etc. Bean Factory is core of the spring framework and, it is a Lightweight container which loads bean definitions and manages your beans. Beans are configured using XML file and manage singleton defined bean. It is also responsible for life cycle methods and injects dependencies. It also removes adhoc singletons and factories. Bean wiring is the creation of associations between application components that are between the beans in a particular spring container. Spring framework is needed because it is – This is a very important module and supplies various necessary services like EJB integration, remoting, JNDI access and scheduling. It transforms spring into a framework. It also broadens the idea of BeanFactory by application of lifecycle events, providing support for internationalization messages and validation. Autowiring is used to build relationships between the collaborating beans. Spring container can automatically resolve collaborators for beans. Autowiring has five different modes: No: no autowire byName: Autowiring that can be done by property name byType: property type as autowired Constructor: It is similar to byType and it is property is in constructor autodetect: Spring is allowed to select autowiring from byType or constructor There are two important methods of Bean life cycle: Setup called when bean is loaded into container Teardown called when bean is unloaded into container For More: Following are the different types of events of listeners: The point where an aspect can be introduced in the application is known as a joinpoint. This point could be a field being modified, a method being called or even an exception being thrown. At these points, the new aspect’s code can be added to introduce a new behavior to the application. Aspect code can be inserted at this point into normal flow of application to change the current behavior. There are 5 types of advices in spring AOP. Some of the important Spring Framework modules are: Spring Context – for dependency injection. Spring AOP – for aspect oriented programming. Spring DAO – for database operations using DAO pattern Spring JDBC – for JDBC and DataSource support. Spring ORM – for ORM tools support such as Hibernate Spring Web Module – for creating web applications. Spring MVC – Model-View-Controller implementation for creating web applications, web services etc. Aspect: Aspect is a class that implements cross-cutting concerns, such as transaction management. Aspects can be a normal class configured and then configured in Spring Bean configuration file or we can use Spring AspectJ support to declare a class as Aspect using @Aspect annotation. Advice: Advice is the action taken for a particular join point. In terms of programming, they are methods that gets executed when a specific join point with matching pointcut is reached in the application. You can think of Advices as Spring interceptors or Servlet Filters. Pointcut: Pointcut are regular expressions that is matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points. Spring framework uses the AspectJ pointcut expression language for determining the join points where advice methods will be applied. Join Point: A join point is the specific point in the application such as method execution, exception handling, changing object variable values etc. In Spring AOP a join points is always the execution of a method. Advice Arguments: We can pass arguments in the advice methods. We can use args() expression in the pointcut to be applied to any method that matches the argument pattern. If we use this, then we need to use the same name in the advice method from where argument type is determined. There are five scopes defined for Spring Beans. Singleton: Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe. Prototype: A new instance will be created every time the bean is requested. Request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request. Session: A new bean will be created for each HTTP session by the container. Global-session: This is used to create global session beans for Portlet applications. Spring MVC Framework provides following ways to help us achieving robust exception handling. Controller Based – We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation. Global Exception Handler – Exception Handling is a cross-cutting concern and Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler. HandlerExceptionResolver implementation – For generic exceptions, most of the times we serve static pages. Spring Framework provides HandlerExceptionResolver interface that we can implement to create global exception handler. The reason behind this additional way to define global exception handler is that spring framework also provides default implementation classes that we can define in our spring bean configuration file to get spring framework exception handling benefits. IoC stands for Inversion of control pattern. It is also known as dependency injection. The basic concept of the IOC is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up. There are three types of dependency injection: Constructor Injection: Dependencies are provided as constructor parameters. e.g. Pico container, Spring etc. Setter Injection: Dependencies are assigned through JavaBeans properties (e.g.: setter methods). Interface Injection: It is done through an interface. e.g. Avalon. Spring OXM stands for Spring Object XML Mappers. It is a one of the Spring framework modules. This module is help to ease the mappings between java object and XML documents. The module can extend. Hence, it provides integration with various other popular frameworks such as JAXB, Castor, XStream etc. Spring AOP stands for Aspect -oriented programming. It is one of the main components of spring framework. It complements OOP by providing another way of thinking program structure. It helps in breaking down the logic of the program into several distinct parts called as concerns. Cross-cutting concerns is the functions which span multiple points of an application. In AOP, A proxy is an object that is created after applying advice to a target object. when you think of client objects the target object and the proxy object are the same. There are three types of AOP proxies. It is process of linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime To force the use of CGLIB proxies set the value of the proxy-target-class attribute of the element to true. When using the Spring JDBC framework the burden of resource management and error handling is reduced. So, developers only need to write the statements and queries to get the data to and from the database. JDBC can be used more efficiently with the help of a template class provided by Spring framework, called JdbcTemplate. Spring JDBC Template is a powerful mechanism to connect to the database and execute SQL queries. It internally uses JDBC api, but eliminates a lot of problems of JDBC API. The problems of JDBC API are as follows: The spring container finds the bean’s definition from the XML file and instantiates the bean. In Spring,When there are more than one beans of the same type and only one is needed to be wired with a property, the @Qualifierannotation is used along with @Autowired annotation to remove the confusion by specifying which exact bean will be wired. The WebFlux framework in Spring Framework 5 uses Reactor as its async foundation.It is provides two core types: Mono to represent a single async value, and Flux to represent a stream of async values. They both implement the Publisher interface defined in the Reactive Streams specification. Mono implements Publisher and returns 0 or 1 elements: Also, Flux implements Publisher and returns N elements: By definition, the two types represent streams, hence they’re both lazy, which means nothing is executed until we consume the stream using the subscribe () method. Both types are immutable, therefore calling any method will return a new instance of Flux or Mono. Spring has many Annotations to serve different purposes. For regular use we refer following popular Spring annotations: @Controller @Configuration @RequestMapping @ResponseBody @PathVariable @Autowired @Service @Scope @Aspect @Before @After @Around @Joinpoint @PointcutWhat is a spring?
Post a Question
What are some of the important features and advantages of Spring Framework?
What are the different modules in spring framework?
How do we implement DI in Spring Framework?
What is the difference between BeanFactory and ApplicationContext?
What is Bean Factory?
Define Bean Wiring?
Why Spring framework is needed?
Define Application context module?
What is Auto Wiring?
What are the different Modes of Autowiring?
What are the methods of bean life cycle?
What are the different types of events of Listeners?
What is a Joinpoint?
What are the types of advice in AOP?
Name some of the important Spring Modules?
Define Aspect, Advice, Pointcut, JointPoint and Advice Arguments in AOP?
What are different scopes of Spring Bean?
How to handle exceptions in Spring MVC Framework?
What is IOC?
What are the different types of Dependency Injection?
What is Spring OXM?
Read More : Spring Boot Interview Questions and Answers for 2021
What is Spring AOP?
What are some Spring AOP concepts?
What is a AOP Proxy?
What are the AOP Proxy types?
What is Waving in Spring?
What do the proxy-target-class attributes do in Spring AOP?
<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>
Can you explain, how can JDBC be used more efficiently in the Spring framework?
What is Spring JDBC Template?
What are the most used design patterns in Spring?
What are the Problems of JDBC API in Spring JDBC?
Can you explain Bean lifecycle in Spring framework?
What is use of @Qualifier annotation?
What are the mono and flux types?
public abstract class Mono<T> implements Publisher<T> {...}
public abstract class Flux<T> implements Publisher<T> {...}
What are some popular Spring annotations that you use in your project?
Related posts:
- Java Developer Interview Questions and Answers
- JDBC Interview Questions and Answers
- JNDI Interview Questions and Answers
- JSF Interview Questions and Answers
- JSP Interview Questions and Answers
- Spring Boot Interview Questions and Answers
- Swing Interview Questions and Answers
- Talend Data Integration Interview Questions and Answers