iteye_5599 2010-01-21 19:52
浏览 1398
已采纳

spring 中配置bean的注入是<property>的name与与ref的id名需要一样吗??

[code="java"]


<bean id="fnBean" class="com.lopu.framework.modules.maintain.action.FlightNatureAction" scope="prototype">
     <property name="fnService">//此name必须是iService吗???
        <ref bean="iService" />
    </property> 
</bean>

[/code]

[code="java"]public class FlightNatureAction extends ActionSupport implements
ModelDriven {

private IService fnService;
private List<FlightNature> fnLists = new ArrayList<FlightNature>();
private FlightNature fnBean = new FlightNature();

public IService getFnService() {
    return fnService;
}

public void setFnService(IService fnService) {
    this.fnService = fnService;
}

public List<FlightNature> getFnLists() {
    return fnLists;
}

public void setFnLists(List<FlightNature> fnLists) {
    this.fnLists = fnLists;
}

@SuppressWarnings("unchecked")
public String list() throws Exception {

//如果这样写会报NPE,即fnService没注入成功,但我把整段程序中的fnService改为iService,并改下配置文件就一切正常了
this.fnLists = this.fnService.loadAll(FlightNature.class);

    return SUCCESS;
}


public FlightNature getModel() {
    return this.fnBean;
}

}[/code]

本人spring菜鸟,多多指教。。。

  • 写回答

3条回答 默认 最新

  • CaiHuajiang 2010-01-21 20:00
    关注

    [quote] //此name必须是iService吗???



    [/quote]

    property name对应了
    public void setFnService(IService fnService) {

    this.fnService = fnService;

    }

    那么参数只要是IService类型的就OK了。。
    跟下面是一样的
    [code="xml"]

    <bean id="fnBean" class="com.lopu.framework.modules.maintain.action.FlightNatureAction" scope="prototype">
         <property name="fnService">//此name必须是iService吗???
            <ref bean="a" />
        </property> 
    </bean>
    

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败