user13812165
user13812165

Reputation:

How can dependency injection by manual bean?

Hi i want to know dependency injection by manual registrations bean not auto bean.

this is auto register bean

@Service
public class Service {
 @Autowired
 private ModelMapper mapper;
}

it is very simple but i want know that injection that i made config

for example)..

@Configuration
public class ModelMapperConfig {

  @Bean
  public ModelMapper modelMapper() {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper
        .getConfiguration()
        .setMatchingStrategy(MatchingStrategies.STRICT);

    return modelMapper;
  }
}

@Service
public class Service {
 
 // i want dependency injection... !!
}

thank you.

Upvotes: 0

Views: 86

Answers (1)

Vladimir Stanciu
Vladimir Stanciu

Reputation: 1526

You can give a name to the ModelMapper bean you have created by using @Bean(name="customModelMapper") and then inject it with @Autowired and @Qualifier("customModelMapper"). So your service would look like this :

@Service
public class Service {
 @Autowired
 @Qualifier("customModelMapper")
 private ModelMapper mapper;
}

Upvotes: 1

Related Questions