Advanced Java Programming Exam Paper
Advanced Java Programming Exam Paper
A servlet undergoes the following life-cycle methods: init(), service(), and destroy(). The init() method is called once when the servlet is first loaded, and it initializes resources needed for the servlet. The service() method is called for each request the servlet handles, processing client requests and generating responses. Finally, the destroy() method is invoked when the server decides to remove a servlet from service, allowing the servlet to release resources before being destroyed.
Spring Boot Auto Configuration Classes automatically configure beans based on the jars present in the classpath and the beans that have already been defined. This reduces the need for explicit configuration, speeding up development. For example, when including spring-boot-starter-web, Spring Boot will auto-configure Tomcat as the default embedded server if no other server is defined explicitly. These classes are guided by conditions specified by annotations like @ConditionalOnClass, enabling dynamic and appropriate configurations.
Built-in IoC container shutdown procedures contribute to application stability by properly releasing resources and gracefully terminating components. Using methods like registerShutdownHook(), the application ensures that beans release any resources, like database connections or external connections, and perform any cleanup tasks. This prevents resource leakage, locks, and other potential issues that could affect application performance or lead to errors upon re-deployment.
The @Autowired annotation in Spring is used for automatic dependency injection. It allows Spring to automatically resolve and inject the dependent beans. When placed on a constructor, method, or field, Spring attempts to resolve and inject the collaborating beans automatically. Its primary advantage is the reduction of explicit bean-wiring code, promoting simpler and cleaner application development. It operates with the help of Spring's reflection and knowledge of bean metadata.
The Entity Manager in JPA is responsible for managing the lifecycle of entity instances, providing APIs for CRUD operations, and handling transactional operations. It tracks the state changes of entities and synchronizes them with the database, allowing entities to be retrieved, removed, merged, or persisted. The Entity Manager works within a persistence context, where it ensures that database operations adhere to the object-relational mapping defined by JPA.
Constructor injection involves the passing of dependency through a constructor when the object is instantiated, which ensures that the object is in a fully initialized state right after its creation. It is best used when a dependency is mandatory for an object's correct functioning. In contrast, setter injection uses setter methods to inject dependencies after the object is created, allowing for optional dependencies and later initialization. Constructor injection also makes it clear that certain dependencies are required. Setter injection can lead to partially initialized objects if dependencies are not set.
Spring Boot simplifies CRUD operations through Spring Data JPA, which provides repository interfaces out-of-the-box. To create data entities, developers define an interface extending JpaRepository, specifying the entity type and primary key. Spring Boot automatically provides methods for CRUD operations like save(), findAll(), and deleteById(). This abstraction minimizes boilerplate code and allows developers to focus on business logic rather than infrastructure code, enhancing developer productivity and reducing errors.
DriverManager in JDBC serves as a factory for creating database connections. It manages the list of available database drivers and matches them with the JDBC URL to establish a connection. When a connection request is made, the DriverManager goes through the list of registered drivers to find the suitable driver that can handle the connection request for a specific URL, and then it facilitates opening a connection using that driver.
The JPA Repository is a full-featured repository interface in Spring Data JPA that extends the basic CRUD Repository, adding functionality related to pagination, sorting, and query creation. It allows more than just basic CRUD operations by providing rich APIs for handling queries and transactions. CRUD Repository focuses on basic CRUD operations without advanced features like pagination or sorting. JPA Repository is suitable for applications needing complex data retrieval functionalities beyond the basic CRUD operations.
Servlets provide several advantages in web development: they are robust and can handle complex requests, support concurrent processing, and integrate well with Java's extensive library ecosystem. They are portable across different platforms due to Java's platform independence. However, their disadvantages include complex setup and handling for simple applications, lack of native support for HTML rendering, and complexity in managing session data compared to newer frameworks like Spring MVC.