Spring:注入内部bean信息

时间:2014-11-30 09:53:51

标签: spring spring-mvc

我怎么能意识到这一点:

 @Autowired
 private ArticoliOrdine articoliOrdine;

 @Autowired
 private Ordine ordine;

  //I want to do this in my applicationContext
 ArticlesOrderPK pk=new ArticolesOrderPK(order.getId(),article.getId());

两个getId()方法返回一个Integer

在我的applicationContext中,我尝试这样做:

<bean class="bean.ArticlesOrder" id="ArticlesOrder">
    <property name="ArticlesOrderPK">
        <bean class="bean.ArticolesOrderPK" id="ArticolesOrderPK">
            <property name="idOrder" ref=""/>
            <property name="idArticle" ref="" />
        </bean>
    </property>
</bean>

<bean class="bean.Ordine" id="order" scope="prototype"/>

如何将ArticleOrderPK.idOrder与Order.id和ArticolesOrderPK.idArticle绑定到Article.id? 谢谢,抱歉我的英文

1 个答案:

答案 0 :(得分:0)

你需要使用 SpEL - Spring Expression Language ,它带有 Spring 3.x +

在bean定义的value属性中(您需要与其他bean的属性绑定)使用下面的表达式

#{beanId.beanProperty} or #{beanId.someMethod()}

在你的情况下,它应该是下面的

<bean class="bean.ArticolesOrderPK" id="ArticolesOrderPK">
        <property name="idOrder" value="#{order.getId()}"/>
        <property name="idArticle" value="#{article.getId()}" />
</bean>
相关问题