When using Spring Boot, a lot of beans are created implicitly by Spring Boot itself. For example, when I link the spring-boot-starter-data-redis
dependency, the jedisConnectionFactory
bean is created automatically under the hood.
What I'm looking for is the way to define my custom bean with a dependency on such an implicit bean, e.g. new MyService( jedisConnectionFactory )
. The problem is that I don't have a variable or a method which would be resolved to that implicit bean.
For now I've come up with the following solution: create a separate BeanConfig
class, autowire/inject ApplicationContext
into it and then retrieve the required bean with ApplicationContext.getBean( Class<T> )
method call:
@Bean
public Transport eventTransport() {
final JedisConnectionFactory jedisConnectionFactory = context.getBean( JedisConnectionFactory.class );
return new RedisTransport( jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort() );
}
Is there any integrated way to get a reference to the beans defined internally? So that I could move this bean definition to MyApplication
class without injecting the ApplicationContext
instance.
@Bean
method.public Transport eventTransport(JedisConnectionFactory connectionFactory)
. Generally when you need to rely on theApplicationContext
(orBeanFactory
) to retrieve beans you are, generally speaking, doing it wrong (if you aren't a framework/extension developer that is).