问题
I want to inject result of method call to my class. I have next code:
class Example {
private static final String NAME = "name";
// #1
@Autowired
@Qualifier("a.b")
private B b;
// #2
@Autowired
@Qualifier("#{a.b}")
private B b;
// #3
@Autowired
@Qualifier("#{a.b.getC('" + NAME + "')}")
private C c;
}
So I have 3 different variants. First one is working, second one is not working but not sure whether I wrote SpEL correctly and third one is not working too.
Actually what I need is third variant. I need to inject result of calling method "getC(name)" on bean called "a.b"
Maybe somebody had similar problem and know how to do it?
回答1:
you can't use SpringEL in @Qualifier,and can only use constant in SpringEL.
inject result of calling method "getC(name)" on bean called "a.b" like this:
@Bean(name="a.b")
private B b(){
return getC(name);
}
来源:https://stackoverflow.com/questions/38740809/how-to-use-spel-to-inject-result-of-method-call-in-spring