Penjelasan @Autowired dapat disimak di link berikut.
Contoh code dengan @Autowired
@Controller// Defines that this class is a spring bean@RequestMapping("/users")publicclassSomeController {// Tells the application context to inject an instance of UserService here @AutowiredprivateUserService userService; @RequestMapping("/login")publicvoidlogin(@RequestParam("username") String username, @RequestParam("password") String password) {// The UserServiceImpl is already injected and you can use ituserService.login(username, password); }}
Contoh code tanpa @Autowired
@Controller// Defines that this class is a spring bean@RequestMapping("/users")publicclassSomeController {privateUserService userService;publicSomeController(UserService userService) {this.userService= userService; } @RequestMapping("/login")publicvoidlogin(@RequestParam("username") String username, @RequestParam("password") String password) {// The UserServiceImpl is already injected and you can use ituserService.login(username, password); }}
Override
Penjelasan @Override dapat disimak pada link berikut.
Contoh code penggunaan @Override pada subclass.
Animal.java
publicclassAnimal {publicvoidmakeSound() {System.out.println("The animal makes a sound"); }}
Dog.java
publicclassDogextendsAnimal { @OverridepublicvoidmakeSound() {System.out.println("The dog barks"); }}
Contoh code penggunaan @Override pada class yang mengimplementasikan method yang sama pada suatu interface.