日期:2014-05-20  浏览次数:20754 次

[Spring]一个和无参构造相关的问题,求大鸟解答
首先,祝大家龙年吉祥,工资上涨

问题是这样的
Spring的ApplicationContext.xml中配置了这么一个bean
XML code

<bean id="productManangerView" class="com.star.client.form.view.ProductManageView">
        <property name="productService" ref="productService"></property>
    </bean>



ProductManageView中的部分代码
Java code

         //...省略不必要的代码
         private ProductSearchForm productSearchForm = new ProductSearchForm();//搜索面板
         private IProductService productService;

    public void setProductService(IProductService productService) {
        this.productService = productService;
    }

         
    /**
     * 无参构造
     */
    private ProductManageView() {
        addComponent();
        addListener();
        addObserver();
        init();
    }

    /**
     * 内部控件初始化
     */
    private void addComponent() {
        JTabbedPane pane = new JTabbedPane(JTabbedPane.TOP);// 服务产品管理面板
        JTabbedPane productPane = new JTabbedPane(JTabbedPane.TOP);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
        JPanel leftPanel = new JPanel(new BorderLayout());
        JPanel rightPanel = new JPanel(new BorderLayout());
        splitPane.setDividerSize(5);
        splitPane.setDividerLocation(380);
        splitPane.setEnabled(false);
        productSearchForm.setProductService(productService); //注意这里!为搜索面板set了服务接口,我的问题就发生在这里
        leftPanel.add(productSearchForm.getControl(), BorderLayout.NORTH);
        leftPanel.add(productDisplayer.getControl(), BorderLayout.CENTER);
        productForm.setFormEnable(false);
        productPane.addTab("产品信息", productForm.getControl());
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(deleteButton);
        rightPanel.add(productPane);
        rightPanel.add(buttonPanel, BorderLayout.SOUTH);
        splitPane.setLeftComponent(leftPanel);
        splitPane.setRightComponent(rightPanel);

        pane.addTab("服务产品管理 ", splitPane);
        
        this.add(pane);
        addDialog.setProductService(productService);
        updateDialog.setProductService(productService);
    }
         public void showView(){
        this.setVisible(true);
    }
//...省略不必要的代码



调用时:
Java code

ProductManageView productView = (ProductManageView) context.getBean("productManangerView");
        productView.showView();


会发现我写注释的地方productService为空,这是为什么呢?Spring没有创建productService

--------------------
如果做以下修改
Java code

/**
     * 无参构造
     */
    private Product、、ManageView() {
        //addComponent();
        //addListener();
        //addObserver();
        //init();
    }
         public void showView(){
                  addComponent();
        addListener();
        addObserver();
        init();
        this.setVisible(true);
    }



运行时,正常Spring创建了productService,这又是为什么呢?
求大鸟解答,谢谢。

------解决方案--------------------
转自:http://blog.csdn.net/nothingisgod/article/details/6293350
XML code

applicationContext.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
<bean id="fru" class="cn.mldn.lxh.demo01.Orange"></bean> 
<bean id="simple" class="cn.mldn.lxh.demo02.SimpleBean"> 
  <constructor-arg index="0"> 
   <value>LiXingHua</value> 
  </constructor-arg> 
  <constructor-arg index="1" value="www.MLDN.cn"></constructor-arg> 
</bean> 
</beans>