NutzCN Logo
问答 Mvcs.ctx.getDefaultIoc();调用问题
发布于 2060天前 作者 qq_851fca2b 1562 次浏览 复制 上一个帖子 下一个帖子
标签:
  1. Mvcs.ctx.getDefaultIoc();在此servlet实例建的任务中有效
    new Thread(new Runnable() {//启动定时器线程
    @Override
    public void run() {
    while(true) {
    Calendar date = Calendar.getInstance();
    int hour = date.get(Calendar.HOUR_OF_DAY);
    //while(hour==0) {
    synchronized(this) {
    Ioc ioc = Mvcs.ctx.getDefaultIoc();
    StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
    int count = staticpParamService.count();
    staticpParamService.staticDetail();
    System.out.println("测试定时器呀呀呀呀呀呀"+count);
    //}
    }
    try {
    Thread.sleep(3600);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }//睡眠250毫秒
    }
    }
    }).start();
  2. Mvcs.ctx.getDefaultIoc();在此TimerTask子类实例的run方法中无效@Override
    public void run() {
    // TODO Auto-generated method stub
    Ioc ioc = Mvcs.ctx.getDefaultIoc();
    StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
    int count = staticpParamService.count();
    staticpParamService.staticDetail();
    System.out.println("测试定时器呀呀呀呀呀呀"+count);
    System.out.println("66666666666666666666666666666");
    System.out.println(name);
    }
    如下是测试单元
    @Test
    public void test() {
    Calendar date = Calendar.getInstance();
    int hour = date.get(Calendar.HOUR_OF_DAY);
    System.out.println(hour);
    Timer timer = new Timer();
    MyTimerTask myTimerTask = new MyTimerTask("我是ximen");
    timer.schedule(myTimerTask, 2);
    System.out.println("fafafafaufasa");
    }
    //注释掉TimerTask子类实例的run方法中的 Mvcs.ctx.getDefaultIoc();获取的实例和方法,可以执行System.out.println("66666666666666666666666666666");
    System.out.println(name);否则不可以执行
8 回复

看来又不看提示的.... 有"插入代码"按钮呀

用try-catch包起来, 打印出错信息,才知道原因的

try {
    // TODO Auto-generated method stub
    Ioc ioc = Mvcs.ctx.getDefaultIoc();
    StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
    int count = staticpParamService.count();
    staticpParamService.staticDetail();
    System.out.println("测试定时器呀呀呀呀呀呀"+count);
    System.out.println("66666666666666666666666666666");
    System.out.println(name);
}
catch (Throwable e) {
      e.printStackTrace();
}
new Thread(new Runnable() {//启动定时器线程

			@Override
			public void run() {
				while(true) {
					Calendar date = Calendar.getInstance();
					int hour = date.get(Calendar.HOUR_OF_DAY);
					//while(hour==0) {
					synchronized(this) {
						Ioc ioc = Mvcs.ctx.getDefaultIoc();
						StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
						int count = staticpParamService.count();
						staticpParamService.staticDetail();
						System.out.println("测试定时器呀呀呀呀呀呀"+count);
						
					//}
					}
					
					try {
						Thread.sleep(3600);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}//睡眠250毫秒
				}
			}
			
		}).start();
@Override
	public void run() {
		// TODO Auto-generated method stub
		
		Ioc ioc = Mvcs.ctx.getDefaultIoc();
		StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
		int count = staticpParamService.count();
		staticpParamService.staticDetail();
		System.out.println("测试定时器呀呀呀呀呀呀"+count);
		System.out.println("66666666666666666666666666666");
		System.out.println(name);
	}
package com.cms.special.common;

import java.util.Calendar;
import java.util.Timer;

import org.junit.Test;

public class Tests {
	@Test
	public void test() {
		Calendar date = Calendar.getInstance();
		int hour = date.get(Calendar.HOUR_OF_DAY);
		System.out.println(hour);
		 Timer timer = new Timer();
		 MyTimerTask myTimerTask = new MyTimerTask("我是ximen");
		timer.schedule(myTimerTask, 2);
		 System.out.println("fafafafaufasa");
	}
}

主要是控制台不报错

用try-catch包起来, 打印出错信息,才知道原因的

run方法报错的时候, 默认是不会有提示的, jdk的原因

@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			Ioc ioc = Mvcs.ctx.getDefaultIoc();
			StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
			int count = staticpParamService.count();
			staticpParamService.staticDetail();
			System.out.println("测试定时器呀呀呀呀呀呀"+count);
			System.out.println("66666666666666666666666666666");
			System.out.println(name);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}

不是Exception , 是Throwable

exception没有就找error?

添加回复
请先登陆
回到顶部