加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何将参数动态传递给Spring bean

发布时间:2020-12-14 05:12:16 所属栏目:Java 来源:网络整理
导读:我是春天的新手 这是bean注册的代码: bean id="user" class="User_Imple" /beanbean id="userdeff" class="User" /bean 这是我的bean类: public class User_Imple implements Master_interface { private int id; private User user; // here user is anot
我是春天的新手

这是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
}

现在我想使用参数调用这个构造函数,这些参数是在我的main方法中动态生成的.这就是我想要传递的动态 – 不是静态的,就像在我的bean.config文件中声明的那样.

解决方法

请看看 Constructor injection.

另外,看看IntializingBean和BeanPostProcessor的其他生命周期拦截一个springbean.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读