Skip to main content

Command Palette

Search for a command to run...

Java : Spring Boot @autowired

Published
1 min read
S

I love learning about technology and sharing that with others

Autowired

@Controller // Defines that this class is a spring bean
@RequestMapping("/users")
public class SomeController {

    // Tells the application context to inject an instance of UserService here
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public void login(@RequestParam("username") String username,
           @RequestParam("password") String password) {

        // The UserServiceImpl is already injected and you can use it
        userService.login(username, password);

    }
}

@ Controller , Service defines that it is spring bean and it will be managed by the spring application context.

Importatn concept

Application Context : is a container inside which all spring beans are managed.

autowiring takes place by placing an instance of one bean into the desired field in an instance of another bean both the classes should be beans that is they should be defined in the application context.

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)