我是Spring的新手。

这是bean注册的代码:

<bean id="user" class="User_Imple"> </bean>
<bean id="userdeff" class="User"> </bean>

这是我的bean类:
public class User_Imple implements Master_interface {

    private int id;
    private User user; // here user is another class

    public User_Imple() {
        super();
    }

    public User_Imple(int id, User user) {
        super();
        this.id = id;
        this.user = user;
    }

    // some extra functions here....
}

这是我执行操作的主要方法:
public static void main(String arg[]) {

    ApplicationContext context = new ClassPathXmlApplicationContext("/bean.xml");
    Master_interface master = (Master_interface)context.getBean("user");

    // here is my some operations..
    int id = ...
    User user = ...

    // here is where i want to get a Spring bean
    User_Imple userImpl; //want Spring-managed bean created with above params
}

现在,我想用参数调用此构造函数,并且这些参数是在我的主要方法中动态生成的。这就是我想动态传递-而不是像bean.config文件中声明的那样静态传递的意思。

最佳答案

请看看Constructor injection

另外,请查看IntializingBeanBeanPostProcessor对springbean的其他生命周期拦截。

关于java - 如何将参数动态传递给Spring Bean,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16997034/

10-10 16:29