Mở Đầu

Trong quá trình sử dụng Spring Framework để lập trình, đặc biệt là Spring Boot. Chắc hẳn các bạn đã gặp các Annotation (Chú thích) là điều khó tránh khỏi. Nó thường hay sử dụng để cung cấp thông tin dữ liệu cho đoạn source code Java của bạn.

Bài viết sau đây, mình xin giải thích và hướng dẫn sử dụng một số annotation thường gặp nhiều nhất.

Các kiến thức cần nắm

Spring IOC Container

@Autowire

//Properties
@Service
public class UserService {

        @Autowired
        UserRepository userRepository;

        @Autowired
        FacebookUtil facebookUtil;
}
//Setter
@Service
public class User {
    
        Private String name;
        
        @Autowire
        void setName(String name) {
            this.name = name;
        }
}
//Contructor
@Service
public class User {

        private long id;

        private String name;

        @Autowire
        void user(long id, String name) {
            this.id = id;
            this.name = name;
        }
}

@Configuration

@Bean

@Configuration
public class WebDriverConfig {

    @Bean
    public WebDriver Chrome() {
        System.setProperty("webdriver.chrome.driver", "/Downloads/chromedriver_linux64/chromedriver");
        return new ChromeDriver();
    }
}

@ComponentScan