grails项目中的java类中调用service方法

来源:互联网 发布:java年度工作总结 编辑:程序博客网 时间:2024/05/18 00:00
最近写java多线程时需要调用service,尝试后唯一下方法可行:
  以task.java里调用smsStackService内方法testSingleMt()方法为例
  1).在resources.groovy内
  beans = {
    myBean(com.crm.my.company.MyBeanImpl) {
        smsStackService = ref("smsStackService")
    }
}
  2).在com.crm.my.company包下建一个java类MyBeanImpl
public class MyBeanImpl {
    public SmsStackService smsStackService;
    public void setSmsStackService(SmsStackService smsStack) {
        this.smsStackService = smsStack;
    }
}
  3).task中调用方法
WebApplicationContext ctx;
   ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContextHolder.getServletContext());
                  MyBeanImpl myBeanImpl  =(MyBeanImpl)ctx.getBean("myBean");
            
                    Object sendResult = myBeanImpl.smsStackService.testSingleMt();
原创粉丝点击