@Inject and @Autowired in Spring: what's the difference and which to choose?
Hi! I started learning Spring Framework and writing my first application. I'm confused about annotations for dependency injection. I see that some examples use @Autowired, while others use @Inject. Both annotations seem to do the same thing: inject beans into fields, constructors, or setters.
Here's an example of my code where I use @Autowired:
```java
@Component
public class UserService {
@Autowired
private UserRepository userRepository;
public User findUser(Long id) {
return userRepository.findById(id);
}
}
```
But my colleague said it's better to use @Inject from the javax.inject package. He claims it's a more "clean" approach, but couldn't clearly explain why. I googled it and found a lot of contradictory information. Some write that
No answers yet.
Log in to leave an answer
Blogs