spring-boot


How to autowire dependency within a Spring Boot Converter?


I am struggling with a way to autowire a dependency within a converter class using spring boot. What is the most elegant solution to solve this problem?
Configuration
#Configuration
public class Config {
#Bean
public ConversionServiceFactoryBean conversionFacilitator() {
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
factory.setConverters(getConverters());
return factory;
}
private Set<Converter> getConverters() {
Set<Converter> converters = new HashSet<>();
converters.add(new MyConverter());
return converters;
}
}
Converter class
#Component
public class MyConverter implements Converter<Type1, Type2> {
#Autowired
private Dependency dependency; // Null here due to the component not being injected
#Override
public Type2 convert(Type1 type1) {
return dependency.something(type1);
}
}
The dependency is not being injected because you are creating MyConverter with new, instead of let Spring create it.
You do not need a method to return set of converters. Spring can do it for you, just auto wiring it. Spring is smart enough to give you a set with all the converter implementations it finds.
You should use something like:
#Configuration
public class Config {
#Bean
#Autowired
public ConversionServiceFactoryBean conversionFacilitator(Set<Converter> converters) {
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
factory.setConverters(converters);
return factory;
}
}

Related Links

want to create Spring boot project in Eclipse IDE
Not able to set spring.datasource.type in spring boot 1.4
LocalDateTime Serialization in Spring Boot 1.4 Release returns array
Stormpath: ERR_TOO_MANY_REDIRECTS
Spring boot multipart/related mime type support
Flyway 4.0.3., spring boot 1.4 and baseline
Creating a Client/Service in Netflix Eureka
Load 2 application.properties in two cild context (spring boot)
Not able to retrieve Build info using spring-boot-actuators through /info endpoint
Spring Cloud Refresh event does not recognize removed properties?
Does #WebMvcTest require #SpringBootApplication annotation?
Spring Boot oauth2: How to set the resource parameter in the authorization request to make adfs happy?
Overwrite Zuul's connection header Keep-Alive
Spring Boot 1.4 & Keycloak unsecured pages
Spring batch to Microservice conversion
Difference between #EntityScan and #ComponentScan

Categories

HOME
drupal
internet-explorer
stata
marie
sip
cors
ldap
abc
aix
jquery-plugins
pandoc
samsung-gear-s2
box-api
normals
point-of-sale
guidewire
opengrok
github-flavored-markdown
openam
spring-webflow
webgl-earth
concatenation
bootstrap-accordion
gsuite
aws-certificate-manager
gulp-jshint
launchd
angular-schema-form
hierarchy
directions
circle
ports
favorites
intersection
dom4j
form-fields
jquery-inputmask
blueimp
socketcluster
delphi-xe
peerjs
wufoo
result
qtwebkit
spring-ide
stream-socket-client
pyopenssl
evaluation
riemann
playframework-2.3
hellosign
catalyst
group
spring-social-twitter
disconnect
cosign-api
qtruby
queuing
supervised-learning
shtml
3d-rendering
clique
skbio
fadein
exchange-server-2007
first-order-logic
log4cxx
avrcp
vensim
esb-toolkit-2.1
avd
bungeecord
data-quality-services
caption
information-hiding
nsnotificationcenter
powerpoint-2010
pnunit
nest-initiative
windows-phone-store
mixradio
php-amqplib
ekevent
zend-db-table
azure-scheduler
latex-suite
cron4j
esi
filedialog
django-admin-tools
django-1.4
advanceddatagrid
yii-cactiverecord
mcpd
log-shipping
file-processing
cinder
large-data-volumes
carbon-emacs

Resources

Encrypt Message