Q.4. Write A Spring Program For Bean Definition Inheritance by Using Constructor
Q.4. Write A Spring Program For Bean Definition Inheritance by Using Constructor
Output :-
messageService1 hashCode: 12345678
messageService2 hashCode: 12345678
Hello from Singleton Bean!
Hello from Singleton Bean!
messageService1 and messageService2 are the same instance
(Singleton Scope)
Q.5. Write a spring DAO program for mapping database rows to java
class by using RowMapper<T> interface.
pom.xml: >> <dependencies> >> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
</dependency> >> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
</dependency> >> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
</dependency> >> <dependency> >>
<scope>runtime</scope> >> </dependency> >> </dependencies>
Step 2: Define the Java Bean Class (e.g., Employee)
public class Employee { >> private int id; >> private String
name; >> private String department; >> // Constructor
public Employee(int id, String name, String department) {
this.id = id; >> this.name = name;
this.department = department; >> } >> // Getters and setters
public int getId() { >> return id; >> }
public void setId(int id) { >> this.id = id; >> }
public String getName() { >> return name; >> }
public void setName(String name) { >> this.name = name;
} >> public String getDepartment() { >> return department;
} >> public void setDepartment(String department) {
this.department = department; >> } >> @Override
public String toString() {
return "Employee{id=" + id + ", name='" + name + "',
department='" + department + "'}"; >> } >> }
Step 3: Implement the RowMapper Interface
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet; >> import java.sql.SQLException;
public class EmployeeRowMapper implements
RowMapper<Employee> {
@Override >> public Employee mapRow(ResultSet
resultSet, int rowNum) throws SQLException {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String department = resultSet.getString("department");
return new Employee(id, name, department); >> } >> }
Step 4: Create the DAO Class
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List; > @Repository > public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public EmployeeDAO(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} >> // Method to fetch employee by id
public Employee getEmployeeById(int id) {
String sql = "SELECT * FROM employees WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new
EmployeeRowMapper()); >> } >> }
Step 6: Main Application Class
importorg.springframework.context.annotation.AnnotationConfigAp
plicationContext; >> import java.util.List; >> public class App {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class)) {
EmployeeDAO employeeDAO =
context.getBean(EmployeeDAO.class);
List<Employee> employees = employeeDAO.getAllEmployees();
for (Employee employee : employees) {
System.out.println(employee); >> }
Employee employeeById = employeeDAO.getEmployeeById(1);
System.out.println("Employee with ID 1: " + employeeById);
} >> } >> }
Q.6. Write a spring DAO program for binding variables in Spring JDBC.
pom.xml:
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
String sql = "INSERT INTO employees (id, name, department) VALUES (?, ?, ?)";
return jdbcTemplate.update(sql, employee.getId(),
employee.getName(), employee.getDepartment()); >> }
public int updateEmployeeDepartment(int id, String department) {
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet; >> import java.sql.SQLException;
importorg.springframework.context.annotation.AnnotationConfigApplication
Context; >> public class App {
public static void main(String[] args) { >> // Initialize Spring context
DatabaseInitializer.initializeDatabase(context.getBean(JdbcTemplate.class));
employeeDAO.addEmployee(newEmployee);
System.out.println ("All employees:");
employeeDAO.getAllEmployees().forEach(System.out::println);
Employee employee = employeeDAO.getEmployeeById(1);
System.out.println("Employee with ID 1: " + employee);
employeeDAO.updateEmployeeDepartment(1, "Product Management");
System.out.println("Updated employee: " + employeeDAO.getEmployeById(1
} >> } >> }