How to use SpEL to inject result of method call in Spring?

故事扮演 提交于 2019-12-24 00:09:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!