Q.7.Write A Spring Program For Autowiring by Using Bytype ? Pom - XML
Q.7.Write A Spring Program For Autowiring by Using Bytype ? Pom - XML
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version> <!-- Use the latest version -->
</dependency>
</dependencies>
Step 2: Define Java Beans (Services)
GreetingService.java
public class GreetingService {
public String getGreeting() {
return "Hello, World!";
}
}
UserService.java
import org.springframework.beans.factory.annotation.Autowired;
public class UserService {
private GreetingService greetingService;
// Autowiring by type
@Autowired
public void setGreetingService(GreetingService greetingService) {
this.greetingService = greetingService;
}
public void greetUser() {
System.out.println(greetingService.getGreeting() + " From
UserService!");
}
}
Step 3: Java Configuration Class
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
// Define GreetingService bean
@Bean
public GreetingService greetingService() {
return new GreetingService();
}
// Define UserService bean
@Bean
public UserService userService() {
return new UserService();
}
}
Step 4: Main Application Class
Importorg.springframework.context.annotation.AnnotationConfigAp
plicationContext;
public class App {
public static void main(String[] args) {
// Initialize Spring context using Java configuration class
(AppConfig)
try (AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class)) {
// Retrieve UserService bean from Spring context
UserService userService = context.getBean(UserService.class);
// Call greetUser method which uses GreetingService
userService.greetUser();
}
}
}
Output :-
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
String sql = "INSERT INTO Employee (id, name, salary) VALUES (?,?, ?)";
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import.org.springframework.jdbc.datasource.DriverManagerDataSource;
>> import javax.sql.DataSource; >> @Configuration
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa"); >> dataSource.setPassword("");
return dataSource; >> }
// Define JdbcTemplate bean >> @Bean public
JdbcTemplate jdbcTemplate(DataSource dataSource) { return new
JdbcTemplate(dataSource); >> } // Define
EmployeeDAO bean >> @Bean
importorg.springframework.context.annotation.AnnotationConfigApplication
Context; public class App { >> public static void main(String[] args) {
employeeDAO.addEmployee(employee1);
employeeDAO.addEmployee(employee2);
employeeDAO.getAllEmployees().forEach(System.out::println);