Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing SPeL on the applicationContext

I am trying to use SPeL to extract some data from the application context using

@Value

Precisely, I want this code as a value

@Autowired
private ApplicationContext context;

Map<String, CrudRepository> repos = 
context.getBeansOfType(CrudRepository.class);

@Value("#{...})
private Map<String, CrudRepository> repos;

is there a magic variable like "systemProperties" that I can invoke? There has to be!

like image 271
Christian Bongiorno Avatar asked Aug 31 '25 03:08

Christian Bongiorno


1 Answers

Something like this:

@Value("#{beanFactory.getBeansOfType(T(org.springframework.data.repository.CrudRepository))}")
private Map<String, MessageChannel> repos;

The SpEL here is based on the BeanExpressionContext, so its getBeanFactory() does the trick for us!

From other side you don't need @Value for the Map<String, CrudRepository>. The normal @Autowired works the same way.

like image 127
Artem Bilan Avatar answered Sep 02 '25 17:09

Artem Bilan