GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Karteikarten

Core Spring

Jerry Reeves Jr Information Technology Englisch 14 Karten

Karten in diesem Stapel (14)

  1. 1What are Spring Beans?Core business component defined insider Spring applications is termed as Bean.
    In simple terms Bean is an object that is instantiated, assembled, and managed by a Spring IoC container.
    Each technology has branded their core business components by certain nomenclature. For example, Business component in JavaEE applications are termed as Servlet, in Web Services they are named as Resource in Struts framework they are called ActionForm etc.
  2. 2How do you decide whether to create Singleton bean or Prototype bean in application?Though Spring IOC container has excellent support to manage the lifecycle of different types of beans, Spring does not manage the complete lifecycle of a prototype bean.
    The IOC container instantiates, configures, decorates, and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that Prototype instance.
    For most of the simple to average applications Singleton bean are sufficient and serve the purpose at each layer e.g. DAO, Service, Controller etc.
    Developer need to take smart decisions & identify which beans must be singleton, else multiple client requests using singleton bean can modify the state of common objects wrongly.
    Some dependent objects need a bean that has private state so that they can conduct their processing separately from other objects that depend on the bean. In this case, singletons are clearly not suitable, and we consider prototypes.
  3. 3When do you use Session and Request bean in Spring?Both beans are used mainly in Web Application.
    If the bean scope is Request, then on every request (from same user or different user) a new bean will be created.
    If the bean scope is Session then on every request same bean would be returned if requests are within the same user session also made from a client which is capable of maintaining the session (curl command can't maintain the user session unless pass cookie/session identifier header).
    Session beans are not destroyed until session timeout up or session destroyed.
  4. 4How an IOC container is configured in a spring console based application?Spring IOC container is primarily responsible for holding all business components termed as Bean.
    Few of these beans are added by spring framework and rest all are defined by developers.
    These beans can be configured using XML configuration file (usually named as applicationContext.xml) or using Java Configuration class (usually named as AppConfig.java).
    The Spring IOC container can be programmatically accessed using two interfaces namely: BeanFactory & ApplicationContext.
    It's always advisable to use ApplicationContext which is child interface of BeanFactory.
    There are multiple implementations available of ApplicationContext depending upon your bean configuration.
    For applicationContext.xml based bean configuration we use ClassPathXmlApplicationContext class.
    For AppConfig.java based bean configuration we use AnnotationConfigApplicationContext class.
  5. 5How to decide upon choosing BeanFactory or ApplicationContext in Spring application?The Spring IOC container can be programmatically accessed using two interfaces namely: BeanFactory & ApplicationContext
    The BeanFactory is the root interface and the ApplicationContext extends the features of BeanFactory.
    BeanFactory loads beans on-demand (Lazy Loading), while ApplicationContext loads all beans at startup(Eager Loading).
    BeanFactory is lightweight as compared to ApplicationContext.
    BeanFactory only supports two scopes — Singleton and Prototype, but ApplicationContext supports all types of bean scopes.
    ApplicationContext enhances BeanFactory to provide several features that are suitable for enterprise applications.It's always advisable to use ApplicationContext.
    We should use BeanFactory only when memory consumption is critical.
  6. 6What are the ways through which the Spring beans are configured?here are broadly two ways in which Spring beans are configured in application-
    Using XML configuration – We usually define xml file with standard name as applicationContext.xml inside src/main/resources folder of your maven project.
    Using Annotation configuration - @Bean, @Component, @Service, @Repository, @Controller, @RestController
  7. 7How Spring supports connection pooling? Elaborate what is connection pooling?Database connections are expensive operation.
    A connection pool is like a collection of open connections.
    If a connection is established or created it should be added to the connection pool.
    When a connection is released, it's returned back to the pool, so other clients can reuse it.
    While the connection is open it can be used again and again.
    Spring support connection pooling by supporting configuration of DataSource inside application.
    Spring provides DriverManagerDataSource for testing application during development phase.
    There are third party DB connection pooling providers like Apache DBCP, Hikari CP etc. which can also be configured in application.
  8. 8What is usual cause of org.springframework.beans.factory.NoUniqueBeanDefinitionException?Exception thrown when a BeanFactory is asked for a bean instance for which multiple matching candidates have been found when only one matching bean was expected.
    To understand problem, consider below situation where we have three input interfaced namely Keyboard, Mouse and Joystick implementing Usb interface.
    The class Counterstrike has one dependency called gameControl of type Usb.
    The Spring container while injecting dependency gameControl has confusion, as there are three qualifying beans for desired match, hence we will get NoUniqueBeanDefinitionException exception.
    The solution for the exception will be using @Qualifier annotation with exact matching bean name
  9. 9Have you configured Init & Destroy spring bean lifecycle hooks?Spring provides several ways through which you can tap into the bean lifecycle.
    For example, once a bean is instantiated, you might need to perform some initialization to get the bean into a usable state.
    Similarly, you might need to clean up resources before a bean is removed from the container.
    These actions can be achieved by configuring Init and Destroy lifecycle hooks into Spring application.
    @PostConstruct Annotation:
    Whenever we annotate a method in Spring Bean with @PostConstruct annotation, it gets executed after the spring bean is initialized.
    We can have only one method annotated with @PostConstruct annotation.
    @PreDestroy Annotation:
    When we annotate a Spring Bean method with PreDestroy annotation, it gets called when the bean instance is getting removed from the context.
    Note: if your spring bean scope is Prototype then it’s not completely managed by the spring container and the PreDestroy method won’t get called.
    Both of the above annotation are part of Common Annotations API and it’s part of JDK module javax.annotation-api.
  10. 10How do you decide as developer to choose among .properties or .yaml configuration file in Spring Boot application?In Spring Boot, we use an external configuration to define our properties.
    This allows us to use the same application code in different environments.
    We can use properties files, YAML files, environment variables and command-line arguments.
    application.properties file, which uses a key-value format:
    Here each line is a single configuration, so we need to express hierarchical data by using the same prefixes for our keys.
    application.yml file, which uses a key-value format:
    YAML is a convenient format for specifying hierarchical configuration data.
    The below code is more readable than .properties file alternative, due to lack of repeated prefixes.
  11. 11Can we achieve DI with Core Java without using Spring framework?Yes, Dependency Injection is a concept rather and then a framework.
    When the application under question is small, we can always meet the needs by injecting dependencies manually, without using any framework like Spring.
    We can use Factory Design Pattern and create dependencies which will then be passed to required classes.
    Though such code cannot replace the DI framework like Spring which provide much more extensive set of features.
  12. 12Brief us on Spring Framework.The Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so application developer can focus on your application.
  13. 13What is Dependency Injection?Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application. Dependency Injection makes our programming code loosely coupled.
  14. 14How many types of spring beans are there?There are 5 types of bean scope in Spring :-
    Singleton:- It return a single bean instance per spring IOC Container.
    Prototype:- It return a new bean instance each time when requested.
    Request:- It return a single instance for every HTTP request call.
    Session:- It returns a single instance for every HTTP request call.
    Global session:- Global session scope is equal as session scope on portlet-based web applications.

Informationen

Beschreibung
Core Spring Interview Questions
Erstellt von
Jerry Reeves Jr
Aufrufe
8
Bewertung

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck