Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

java code interview

The document outlines the fundamentals of the Spring Framework, including concepts such as loose coupling, dependency injection, and the roles of the IOC container. It covers Spring configurations, autowiring, and the differences between XML and Java configurations, as well as Spring Boot features and Spring Data JPA. Additionally, it addresses Spring MVC, testing, AOP, and provides interview questions related to these topics.

Uploaded by

majbah00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

java code interview

The document outlines the fundamentals of the Spring Framework, including concepts such as loose coupling, dependency injection, and the roles of the IOC container. It covers Spring configurations, autowiring, and the differences between XML and Java configurations, as well as Spring Boot features and Spring Data JPA. Additionally, it addresses Spring MVC, testing, AOP, and provides interview questions related to these topics.

Uploaded by

majbah00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Spring Basics

●​ Loose Coupling: Objects are independent and interact through interfaces, reducing
dependency.
●​ Dependency: One class relying on another to function.
●​ IOC (Inversion of Control): The framework controls object creation and lifecycle instead
of the programmer.
●​ Dependency Injection: Providing dependencies externally rather than creating them
inside a class.
●​ Examples of Dependency Injection: Constructor Injection, Setter Injection, Field
Injection.
●​ Auto Wiring: Spring automatically resolves dependencies using annotations like
@Autowired.
●​ IOC Container Roles: Manage bean creation, dependency injection, and lifecycle.
●​ Bean Factory vs Application Context: BeanFactory is lightweight;
ApplicationContext has more features like event handling and internationalization.
●​ Creating Application Context: Using ClassPathXmlApplicationContext or
AnnotationConfigApplicationContext.
●​ Component Scan: Spring scans for beans automatically in specified packages.
●​ Defining Component Scan: In XML (<context:component-scan>) or Java
(@ComponentScan).
●​ Spring Boot Component Scan: Automatic, using @SpringBootApplication.
●​ @Component: Marks a class as a Spring-managed bean.
●​ @Autowired: Automatically injects dependencies.
●​ Spring Stereotype Annotations:
○​ @Component - Generic bean
○​ @Service - Business logic
○​ @Repository - DAO layer
○​ @Controller - Web controller
●​ Default Scope of a Bean: Singleton.
●​ Are Beans Thread-Safe?: No, singleton beans are not thread-safe by default.
●​ Other Scopes: Prototype, Request, Session, Application, WebSocket.
●​ Spring Singleton vs GoF Singleton: Spring singleton is per container; GoF singleton is
per JVM.
●​ Types of Dependency Injection: Constructor, Setter, Field Injection.
●​ Choosing Constructor vs Setter Injection: Constructor for mandatory dependencies,
setter for optional.

Spring Configurations & Autowiring

●​ Application Contexts: XML, Java Config, Annotation-Based.


●​ XML vs Java Config: XML is verbose; Java Config is type-safe.
●​ Choosing XML vs Java Config: Java Config is preferred for maintainability.
●​ Spring Autowiring: Uses reflection to inject dependencies.
●​ Matching Types in Autowiring: By Type, By Name, By Constructor, By Qualifier.
●​ Debugging Spring Issues: Use logs, @Primary, @Qualifier, and check bean
definitions.
●​ NoUniqueBeanDefinitionException: Use @Primary or @Qualifier.
●​ NoSuchBeanDefinitionException: Ensure bean exists or is correctly scanned.
●​ @Primary: Marks a bean as the default choice.
●​ @Qualifier: Specifies a specific bean when multiple exist.
●​ CDI (Contexts and Dependency Injection): Java EE’s DI framework.
●​ Spring vs CDI: Spring is more powerful and widely used.

Spring Framework Versions & Modules

●​ Major Spring Features: AOP, MVC, Security, Transactions, Boot, Data, etc.
●​ Spring 4.0 Features: Java 8 support, WebSocket, Groovy, etc.
●​ Spring 5.0 Features: Reactive programming, Java 9/10 support.
●​ Important Spring Modules: Core, MVC, AOP, Security, Boot, Data.
●​ Spring Projects: Spring Boot, Spring Security, Spring Cloud, Spring Data.
●​ Dependency Management: Use spring-boot-starter-parent.
●​ Design Patterns in Spring: Factory, Singleton, Proxy, Template, MVC.

Spring MVC

●​ Model 1 vs Model 2: Model 1 (JSP-centric); Model 2 (MVC pattern).


●​ Front Controller Pattern: Central controller handling all requests
(DispatcherServlet).
●​ Spring MVC Controller Example: @Controller with @RequestMapping.
●​ Spring MVC Flow: Request → DispatcherServlet → Controller → Service → View.
●​ ViewResolver: Resolves view names to actual views.
●​ Model & ModelAndView: Used to pass data to views.
●​ Dispatcher Servlet: Core of Spring MVC.
●​ Form Backing Object: Binds form data to an object.
●​ Validation in Spring MVC: @Valid, BindingResult.
●​ Spring Form Tags: <form:input>, <form:errors>.
●​ Path Variable vs Model Attribute: @PathVariable extracts values from URLs,
@ModelAttribute binds form data.
●​ Session Attribute: Stores data in HTTP session.
●​ Controller Advice: Centralized exception handling using @ExceptionHandler.

Spring Boot
●​ Spring Boot: Simplifies Spring applications with auto-configuration.
●​ Spring Boot vs Spring: Less configuration, embedded servers.
●​ Spring Boot vs Spring MVC: Spring Boot includes MVC but adds auto-config.
●​ @SpringBootApplication: Combines @Configuration, @ComponentScan,
@EnableAutoConfiguration.
●​ Auto Configuration: Automatically configures beans.
●​ Embedded Server: Built-in Tomcat, Jetty, Undertow.
●​ Starter Projects: Pre-configured dependencies (spring-boot-starter-web, etc.).
●​ Spring Initializr: Web tool to generate Spring Boot projects.
●​ application.properties: Configures Spring Boot apps.
●​ Spring Profiles: Manages environment-specific settings.
●​ Spring Boot Actuator: Monitors and manages applications.

Spring Data & JPA

●​ Spring JDBC vs JDBC: Less boilerplate, uses JdbcTemplate.


●​ RowMapper: Maps rows to objects.
●​ JPA vs Hibernate: JPA is the specification, Hibernate is the implementation.
●​ Entity, Entity Manager, Persistence Context: Manages database interactions.
●​ One-to-One, One-to-Many, Many-to-Many Mappings: Defines relationships between
entities.
●​ Spring Data JPA: Simplifies JPA with repositories (CrudRepository,
JpaRepository).

Spring Testing

●​ Unit Testing in Spring: Uses JUnit, Mockito, MockMvc.


●​ Mockito: Mocking framework for unit tests.
●​ MockMvc: Tests controllers without starting a server.
●​ @WebMvcTest: Focused testing for Spring MVC.
●​ @SpringBootTest: Full application testing.

Spring AOP

●​ Cross-Cutting Concerns: Logging, Security, Transactions.


●​ Aspect, Pointcut: Defines where and when advice is applied.
●​ Types of AOP Advice: Before, After, Around, etc.
●​ Weaving: Linking aspects with objects.

SOAP & REST

●​ SOAP: XML-based protocol for web services.


●​ WSDL: Defines SOAP web services.
●​ REST: Lightweight alternative to SOAP, uses HTTP methods.
●​ REST API Best Practices: Use proper HTTP status codes, HATEOAS, pagination.
●​ Swagger: API documentation tool.
●​ Content Negotiation: Returns different formats (XML, JSON).
●​ REST Exception Handling: @ExceptionHandler, @ControllerAdvice.
●​ Versioning Strategies: URL, Header, Query Param, Media Type.
Spring Basics Interview Questions
1.​ What is Loose Coupling?​

○​ Loose coupling means reducing direct dependencies between components,


making the system flexible and maintainable.
2.​ What is a Dependency?​

○​ A dependency is an object that another object requires to function properly.


3.​ What is IOC (Inversion of Control)?​

○​ IOC is a design principle where object creation and lifecycle management are
handled by a container rather than the class itself.
4.​ What is Dependency Injection?​

○​ Dependency Injection (DI) is a technique where dependencies are provided


externally rather than created within a class.
5.​ Can you give a few examples of Dependency Injection?​

○​ Constructor Injection, Setter Injection, Field Injection.


6.​ What is Auto Wiring?​

○​ Auto Wiring is Spring's way of automatically resolving dependencies using


annotations like @Autowired.
7.​ What are the important roles of an IOC Container?​

○​ Managing bean lifecycle, dependency injection, configuration, and event


handling.
8.​ What are Bean Factory and Application Context?​

○​ BeanFactory: Lightweight container that loads bean definitions.


○​ ApplicationContext: More advanced container that extends BeanFactory
with additional features like event propagation and internationalization.
9.​ Can you compare Bean Factory with Application Context?​

○​ BeanFactory is lazy-loading, whereas ApplicationContext eagerly loads


beans. The latter supports more enterprise-level features.
10.​How do you create an application context with Spring?​

○​ Using ClassPathXmlApplicationContext for XML config or


AnnotationConfigApplicationContext for Java-based config.
11.​How does Spring know where to search for Components or Beans?​
○​ Through @ComponentScan annotation or XML-based
<context:component-scan>.
12.​What is a Component Scan?​

○​ It tells Spring to scan packages for components (@Component, @Service,


@Repository, @Controller).
13.​How do you define a component scan in XML and Java Configurations?​

○​ XML: <context:component-scan base-package="com.example"/>


○​ Java Config: @ComponentScan("com.example")
14.​How is it done with Spring Boot?​

○​ Spring Boot automatically scans components in the package of the main class
annotated with @SpringBootApplication.
15.​What does @Component signify?​

○​ It marks a class as a Spring-managed bean.


16.​What does @Autowired signify?​

○​ It enables automatic dependency injection by Spring.


17.​What’s the difference between @Controller, @Component, @Repository, and
@Service annotations in Spring?​

○​ @Component: Generic bean.


○​ @Service: Business logic layer.
○​ @Repository: DAO layer, exception translation.
○​ @Controller: Web layer (Spring MVC).
18.​What is the default scope of a bean?​

○​ Singleton (one instance per Spring container).


19.​Are Spring beans thread-safe?​

○​ No, singleton beans are not thread-safe by default.


20.​What are the other scopes available?​

○​ Prototype, Request, Session, Application, WebSocket.


21.​How is Spring’s singleton bean different from Gang of Four Singleton Pattern?​

○​ Spring singleton is per container, whereas GoF singleton is per JVM.


22.​What are the different types of dependency injections?​

○​ Constructor, Setter, Field Injection.


23.​What is setter injection?​

○​ Dependency is provided using setter methods.


24.​What is constructor injection?​

○​ Dependency is injected through the constructor.


25.​How do you choose between setter and constructor injections?​

○​ Constructor Injection for mandatory dependencies, Setter Injection for optional


ones.

Spring Configurations & Autowiring

26.​What are the different options available to create Application Contexts for Spring?​

○​ ClassPathXmlApplicationContext,
FileSystemXmlApplicationContext,
AnnotationConfigApplicationContext.
27.​What is the difference between XML and Java Configurations for Spring?​

○​ XML is declarative, whereas Java config is programmatic and type-safe.


28.​How do you choose between XML and Java Configurations for Spring?​

○​ Java Config is preferred due to better maintainability and type safety.


29.​How does Spring do Autowiring?​

○​ By Type, By Name, By Constructor, By Qualifier.


30.​What are the different kinds of matching used by Spring for Autowiring?​

○​ Type Matching, Name Matching, Constructor Matching, Primary Bean.


31.​How do you debug problems with Spring Framework?​

○​ Use logs, @Primary, @Qualifier, and check bean definitions.


32.​How do you solve NoUniqueBeanDefinitionException?​

○​ Use @Primary or @Qualifier to specify the correct bean.


33.​How do you solve NoSuchBeanDefinitionException?​

○​ Ensure the bean exists and is correctly defined.


34.​What is @Primary?​

○​ Marks a bean as the default when multiple beans of the same type exist.
35.​What is @Qualifier?​

○​ Helps specify which bean should be injected when multiple exist.


36.​What is CDI (Contexts and Dependency Injection)?​

○​ Java EE’s dependency injection framework.


37.​Does Spring Support CDI?​

○​ Yes, Spring can integrate with CDI.


38.​Would you recommend using CDI or Spring Annotations?​

○​ Spring Annotations are preferred due to broader adoption and more features.

Spring Framework Versions & Modules

39.​What are the major features in different versions of Spring?​

○​ Spring 4: Java 8 support, WebSockets, Groovy.


○​ Spring 5: Reactive programming, Java 9/10 support.
40.​What are new features in Spring Framework 4.0?​

○​ Java 8 support, WebSockets, REST improvements.


41.​What are new features in Spring Framework 5.0?​

○​ Reactive programming, Kotlin support, HTTP/2.


42.​What are important Spring Modules?​

○​ Core, AOP, MVC, Security, Transactions, JDBC.


43.​What are important Spring Projects?​

○​ Spring Boot, Spring Data, Spring Security, Spring Cloud.


44.​What is the simplest way of ensuring that we are using a single version of all
Spring-related dependencies?​

○​ Use spring-boot-starter-parent in Maven.


45.​Name some of the design patterns used in Spring Framework?​

○​ Factory, Singleton, Proxy, Template, MVC, Dependency Injection.


46.​What do you think about Spring Framework?​

○​ A powerful, feature-rich framework for Java application development.


47.​Why is Spring Popular?​

○​ Lightweight, flexible, supports dependency injection, AOP, and integrates well


with various technologies.
48.​Can you give a big picture of the Spring Framework?​

○​ Spring provides a comprehensive programming model for Java applications,


including DI, AOP, MVC, Security, and Data access.

You might also like