티스토리 뷰

Spring Boot

Constructor Injection

seoca 2019. 3. 8. 04:42

 

 

 

When a bean has a single constructor, @Autowired annotation is no longer needed. 

Use 'final' to make sure that object is not changed after it's initialized 

However,  for Setter injection, you need @Autowired.  

 

 

private final UsersRepository usersRepository;
private final ModelMapper modelMapper;
 
//@Autowired
public UsersController(UsersRepository usersRepository, ModelMapper modelMapper){
    this.usersRepository = usersRepository;
    this.modelMapper = modelMapper;
}

cs

 

 

reference

https://www.baeldung.com/constructor-injection-in-spring

http://woowabros.github.io/experience/2019/02/28/pilot-project-settle.html