Reputation: 27654
I have a Configuration.class, which fetches properties from application.properties
.
@Component
public class Configuration {
@Value("${someValue}")
public String someValue;
}
I have another class called Fetcher
, which needs the property.
class Fetcher {
@Autowired
private Configuration configuration;
public void doSomethingWithSomeValue() {
System.out.println(configuration.someValue);
}
}
Of course the above code would fail because Fetcher
is not a Spring Bean(I didn't add @Component
/@Service
/@Repository
to it). My question is, is it possible to get someValue
in Fetcher
without making it a Spring Bean?
Upvotes: 0
Views: 775
Reputation: 89
No and yes.
No is because that's the whole idea of Spring and dependency injection. You have to use something which will have access to Spring ApplicationContext.
Yes, because you may use publish–subscribe pattern, or you may make some of your context aware beans to be a singleton, and let them expose all the necessary properties. But it is just delegation to object which has access to Spring context. To make an analogy: it is not the crime, it is just selling weapon and giving money to someone who will make an actual shot :)
Upvotes: 3