Spring 1.x modules - bhudevi0924/Spring-boot GitHub Wiki

Spring 1.x modules:

  • Core Container: This includes the foundational features like Inversion of Control (IoC) and Dependency Injection (DI). -> Example for DI public class OrderService { private OrderRepository orderRepository;

           // Constructor injection
          public OrderService(OrderRepository orderRepository) {
          this.orderRepository = orderRepository;
         }
    
         public void placeOrder(Order order) {
         orderRepository.save(order);
         }
       }
    
  • AOP (Aspect Oriented Programming): Support for Aspect-Oriented Programming to modularize cross-cutting concerns.

        public aspect LoggingAspect {
            before() : execution(* com.example.*.*(..)) {
            System.out.println("Logging method call: " + thisJoinPoint);
            }
         }
    
  • Data Access/ Integration: Simplifies integration with various data access technologies like JDBC, Hibernate, etc.

    -> using JDBC: public class JdbcMessageRepository implements MessageRepository { private final JdbcTemplate jdbcTemplate;

            public JdbcMessageRepository(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
            }
    
            public String getMessage() {
            return jdbcTemplate.queryForObject("SELECT message FROM messages", String.class);
            }
        }
    
  • Transaction Management: Provides support for managing transactions in Java applications. (@Transactional)

    @Transactional public void updateMessage(String newMessage) { jdbcTemplate.update("UPDATE messages SET message = ?", newMessage); }

    this method will execute within a transaction, and if any exceptions occur during its execution, the transaction will be rolled back, ensuring data consistency.

  • Web: Integration with Servlet API and other web-related technologies.

      @Controller
      public class MessageController {
    
          @Autowired
          private MessageService messageService;
    
          @RequestMapping("/message")
          public String showMessage(Model model) {
          String message = messageService.getMessage();
          model.addAttribute("message", message);
          return "message";
          }
      }
    
  • Testing: Support for unit and integration testing of Spring applications.

          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(classes = {TestConfig.class})
          public class MessageServiceTest {
    
              @Autowired
              private MessageService messageService;
    
              @Test
              public void testGetMessage() {
              String message = messageService.getMessage();
              assertEquals("Expected message", message);
              }
          }