开发者

Can output of a method be used to autowire another bean?

I have a following class

public class Customer {
    private String firstName;
    private String lastName;
    public void setFirstName(String fName) {
       this.firstName = fName;
    }
    public void setLastName(String lName) {
       this.lastName = lName;
    }
};

I've another class that does t开发者_运维知识库he following.

public class NameGenerator {
    public String generateName() {
       return "Zee Zee";
    }
};

Is it possible to set the name of customer (inject name into customer) without having passing NameGenerator bean. Rather, I'm expecting to inject the output of generateName() method?

This question is for sake of understanding if it can or cannot be done and does not necessarily delve into best practices.


If you really want to use the result of a static method invocation and inject it in another bean, you could go for the MethodInvokingFactoryBean class.

The MethodInvokingFactoryBean returns a value which is the result of a static or instance method invocation.


I don't think if that would be possible. Why in the first place you want to do that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜