首先,祝大家龙年吉祥,工资上涨问题是这样的
Spring的ApplicationContext.xml中配置了这么一个bean<bean id="productManangerView" class="com.star.client.form.view.ProductManageView">
<property name="productService" ref="productService"></property>
</bean>
ProductManageView中的部分代码 //...省略不必要的代码
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);
}
//...省略不必要的代码
调用时:ProductManageView productView = (ProductManageView) context.getBean("productManangerView");
productView.showView();会发现我写注释的地方productService为空,这是为什么呢?Spring没有创建productService--------------------------------------------------------
如果做以下修改/**
* 无参构造
*/
private Product、、ManageView() {
//addComponent();
//addListener();
//addObserver();
//init();
}
public void showView(){
addComponent();
addListener();
addObserver();
init();
this.setVisible(true);
}
运行时,正常Spring创建了productService,这又是为什么呢?
求大鸟解答,谢谢。
Spring的ApplicationContext.xml中配置了这么一个bean<bean id="productManangerView" class="com.star.client.form.view.ProductManageView">
<property name="productService" ref="productService"></property>
</bean>
ProductManageView中的部分代码 //...省略不必要的代码
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);
}
//...省略不必要的代码
调用时:ProductManageView productView = (ProductManageView) context.getBean("productManangerView");
productView.showView();会发现我写注释的地方productService为空,这是为什么呢?Spring没有创建productService--------------------------------------------------------
如果做以下修改/**
* 无参构造
*/
private Product、、ManageView() {
//addComponent();
//addListener();
//addObserver();
//init();
}
public void showView(){
addComponent();
addListener();
addObserver();
init();
this.setVisible(true);
}
运行时,正常Spring创建了productService,这又是为什么呢?
求大鸟解答,谢谢。
<?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> =======================================
SimpleBean.java package cn.mldn.lxh.demo02;
public class SimpleBean {
private String name ;
private String password ; public SimpleBean(String name,String password)
{
this.setName(name) ;
this.setPassword(password) ;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} ================================================= TestDemo03.java
package cn.mldn.lxh.demo02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo03 {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = null ;
context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
SimpleBean simple = (SimpleBean)context.getBean("simple") ;
System.out.println("姓名:"+simple.getName()) ;
System.out.println("密码:"+simple.getPassword()) ;
}
} ====================================================
虽然在程序中提倡使用POJO类,可是有些时候更希望可以在对象实例化时通过构造方法实例化
通过在配置中增加一个参数,同时Bean中增加一个构造方法
在Spring中如果需要使用构造,则加入constrator-arg元素进行配置 =====================以上内容来自转载=======================把productService放到构造函数中,然后给addComponents()也添加一个参数 addComponents(IProductService)你那样子如果有错,就说明spring注入参数是在调用构造函数后面进行的。