[Bài đọc] Dependency Injection với @Autowired

2. Spring MVC

Một trong những lợi ích mà Framework Spring mang lại đó là “Dependency injection”. Spring bắt đầu như một “Dependency injection”. Cách dễ dàng để sử dụng “Dependency injection” trong Spring MVC Controller là chú thích một file hoặc một phương thức bằng @Autowired. Loại annotation Autowired nằm trong package “org.springframework.beans.factory.annotation”.

Hãy khảo sát ví dụ về “dependency injection” trong ứng dụng Spring MVC. Ta có controller như sau:

package com.codegym.controller;

import com.codegym.model.Product;
import com.codegym.model.ProductForm;
import com.codegym.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class ProductController {

@Autowired
private ProductService productService;

@RequestMapping("/products")
public ModelAndView listProducts() {
List<Product> products = productService.findAll();
ModelAndView modelAndView = new ModelAndView("/product/list", "products", products);
return modelAndView;
}
...
}

Trong ví dụ ở trên, ta thêm một trường private được chú thích bởi @Autowired:

@Autowired
private ProductService productService;

ProductService là một interface cung cấp các phương thức khác nhau để xử lý “product”.  Chú thích productService bằng annotation @Autowired sẽ tiêm một thể hiện của ProductService đưa vào thể hiện của ProductController.

Interface ProductService:

package com.codegym.service;

import com.codegym.model.Product;

import java.util.List;

public interface ProductService {
List<Product> findAll();

Product findById(Long id);

void save(Product product);

void remove(Long id);
}

Lớp ProductServiceImpl sau đây được implements từ interface ProductService:

package com.codegym.service.Impl;

import com.codegym.model.Product;
import com.codegym.repository.ProductRepository;
import com.codegym.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class ProductServiecImpl implements ProductService{

@Autowired
private ProductRepository productRepository;

@Override
public List<Product> findAll() {
return productRepository.findAll();
}

@Override
public Product findById(Long id) {
return productRepository.findById(id);
}

@Override
public void save(Product customer) {
productRepository.save(customer);
}

@Override
public void remove(Long id) {
productRepository.remove(id);
}
}

Ta sẽ tạo ra một thể hiện của lớp ProductServiecImpl như là một Bean trong lớp cấu hình của ứng dụng sử dụng annotation @Bean như sau:

@ComponentScan("com.codegym")
public class ApplicationConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{

@Bean
public ProductService productService(){
return new ProductServiecImpl();
}
...
}

Trả lời

Email của bạn sẽ không được hiển thị công khai.