java code interview
java code interview
● 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.
● 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
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 Testing
Spring AOP
○ 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?
○ Spring Boot automatically scans components in the package of the main class
annotated with @SpringBootApplication.
15.What does @Component signify?
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?
○ Marks a bean as the default when multiple beans of the same type exist.
35.What is @Qualifier?
○ Spring Annotations are preferred due to broader adoption and more features.