zhangrongyi830 2009-04-29 12:49
浏览 594
已采纳

InitializingBean的问题!!!!请大家帮助

   我有一个类继承了InitializingBean。   用来初始化,部署一些设置。   现在的问题是   被继承的类,总是被调用2次,在网上查了一下资料   ,说是要让此类为单例才会被只调用一次。 



所以我在被配置文件里面加入了singleton= "true " 这个设置 不过还是没有成功。请大家帮助!!!
现在的问题是
1.我这样来设计是正确的吗?·
2.如果正确,怎么让此类为单例?

[b]问题补充:[/b]
谢谢你的建议,不过我之前也试过init-method="xxx" 使用这个还是会调用两次
[b]问题补充:[/b]

public class AppCallService implements InitializingBean {

private AppCallDAO appCallDAO;
//private static final List<Integer> registedport = new ArrayList<Integer>();


public void showData(){
    ExecutorService threadService;// 线程池
    List<Integer> ports = AppProp.getPorts();
    if (ports.isEmpty()) {
        System.out.println("未配置服务端口");
        return;
    }
    List<AppletCallMonitor> services = new ArrayList<AppletCallMonitor>();
    threadService = Executors.newFixedThreadPool(ports.size());
    for (Integer port : ports) {

        AppletCallMonitor service = new AppletCallMonitor();
        //if(registedport.contains(port)) continue;
        //registedport.add(port);
        System.out.println(port.intValue());
        service.setPort(port.intValue());
        service.setAppCallDAO(appCallDAO);
        services.add(service);
        threadService.execute(service); 
    }

}


public void afterPropertiesSet() throws Exception {
    showData();
}

public void setAppCallDAO(AppCallDAO appCallDAO) {
    this.appCallDAO = appCallDAO;
}

}

class AppProp{

/**
 * 取服务端口
 * @return
 */
public static List<Integer> getPorts()
{
    List<Integer> ports=new ArrayList<Integer>();
    Properties props=getProperties();
    if(props == null)
        return ports;
    String[] portstrs=props.getProperty("PORT","").split(",");
    for(String port:portstrs){
        int iport=0;
        try{
            iport=Integer.valueOf(port);
        }catch(Exception e){                
        }
        if(iport > 0){
            ports.add(new Integer(iport));
        }
    }
    return ports;   
}

private static Properties getProperties(){
    FileInputStream fstream = null; 
    try {
        fstream = new FileInputStream("E:\\avetti\\workspace-a\\stmNEW\\Target\\WebRoot\\appCall.properties");
        Properties prps = new Properties();
        prps.load(fstream);
        return prps;
    }catch(Exception e){
        System.out.println(e);
        return null;
    }
    finally{
        try{
            fstream.close();
        }catch(Exception e){
        }
    }
}

}
[b]问题补充:[/b]
my-servlet.xml




[b]问题补充:[/b]
照lovewhzlq这样设置了依赖关系,还是会执行两次。 不知道什么原因
[b]问题补充:[/b]
是这样的,AppCallService implements InitializingBean 后,afterPropertiesSet里面的 showData() 方法执行了两次。后台信息不会影响吧,因为 我把showdata方法只在控制台输出1句话,它也会执行两次。 我在网上看到的那个提示,也是在讨论InitializingBean (文章提出InitializingBean 会执行两次,是因为BEAN没有单例)和init-method 分别该怎么用。 我都试用过了,都会执行两次。
[b]问题补充:[/b]
to: lovewhzlq
没有。 我想继承InitializingBean之后, 就没在其他BEAN去注入过了 。现在我用了一个private static final List registedport = new ArrayList();
来判断是否已经是配置过的端口号。所以占时能解决问题,但是调用两次。总是觉得不安全啊

  • 写回答

6条回答 默认 最新

  • wanghaolovezlq 2009-04-29 15:25
    关注

    这个太奇怪了,你是不是有别的bean要注入“appCallService”这个bean

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法