NutzCN Logo
问答 与Spring使用的问题
发布于 2445天前 作者 andymiaomiao 1457 次浏览 复制 上一个帖子 下一个帖子
标签:

大哥:下面的代码,service层对象不能注入,使用时为null,请指教,谢谢。

@Component
public class SEQUENCE implements RunMethod, Plugin{
@Autowired
BocDataBaseService bocDataBaseService;
}

10 回复

SEQUENCE的实例,你是new出来的,不是从spring ioc取出来的?

那表达式中El.eval("sequence('dgt','sequId')");这种方式调用,都是new出来的对象吗?

注册sequence的地方

package com.jettech.dgt.web.util.mathFuction;

import java.util.List;

import org.nutz.el.ElException;
import org.nutz.el.opt.RunMethod;
import org.nutz.plugin.Plugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jettech.dgt.model.BocDataBase;
import com.jettech.dgt.service.BocBaseFieldService;
import com.jettech.dgt.service.BocDataBaseService;

/**
* 函数-SEQUENCE-序列
* @author miaozuping
*
*/
@Component
public class SEQUENCE implements RunMethod, Plugin{

@Autowired
BocDataBaseService bocDataBaseService;

@Override
public boolean canWork() {
    return true;
}

@Override
public Object run(List<Object> fetchParam) {

    if(fetchParam.size()!=3&&fetchParam.size()!=2)

       throw new ElException("sequence函数语法错误:sequence(dbName,sequName)");

    Object dbName=fetchParam.get(0);

    Object sequName=fetchParam.get(1);

    Object doExecuted=fetchParam.size()==3?fetchParam.get(2):null;

    if (dbName==null||dbName.toString().equals("")||sequName==null||sequName.toString().equals(""))

       throw new ElException("sequence函数语法错误:sequence(dbName,sequName)");

    String result = getSequenceValue(
          String.valueOf(dbName),
          String.valueOf(sequName),
          doExecuted==null?false:Boolean.valueOf(String.valueOf(doExecuted))
         );
    return result;
}

@Override
public String fetchSelf() {

    return "sequence";
}


public String getSequenceValue(String dbName, String sequName, boolean doExecuted){

    BocDataBase db=bocDataBaseService.findByName(dbName);

    if (db==null)
    {
        throw new ElException("数据库:["+dbName+"],在系统中查询不到;");
    }
    else
    {
       if (doExecuted)
       {
         try
         {
          return BocBaseFieldService.getValueBySequenceName(sequName, db.getName(), db.getDriver(), db.getUrl(), db.getUserName(), db.getPassWord())+"";

         } 
         catch (Exception e)
         {
          throw new ElException(e.getMessage());
         }
       }
       else
       {
         try
         {
          List<String> errorList=BocBaseFieldService.validateSequenceName(sequName, db.getName(), db.getDriver(), db.getUrl(), db.getUserName(), db.getPassWord());

          if (!errorList.isEmpty()) 
          {
              throw new ElException(errorList.get(0));
          }
         } 
         catch (Exception e)
         {
          throw new ElException(e.getMessage());
         }

       }
    }

    return null;

}

}

如上,我们项目中只是用了NUTZ的表达式引擎部分,spring采用的是注解方式,大哥说的注册是哪块注册,可以提供示例代码吗?

那你是怎样把这个SEQUENCE 类注册到EL上下文的? js文件??

对,通过js文件

{
	"EL":{
		"custom":[
			"org.nutz.el.opt.custom.Max",
			"org.nutz.el.opt.custom.Min",
			"org.nutz.el.opt.custom.Trim",
			"org.nutz.el.opt.custom.MakeUUID",
			"org.nutz.el.opt.custom.DoBase64",
			"org.nutz.el.opt.custom.DoURLEncoder",
			"org.nutz.el.opt.custom.TimeNow",
			"org.nutz.el.opt.custom.ByMake",
			"com.jettech.dgt.web.util.mathFuction.IN",
			"com.jettech.dgt.web.util.mathFuction.NOTIN",
		"com.jettech.dgt.web.util.mathFuction.EXCEL",
			"com.jettech.dgt.web.util.mathFuction.UNIONEXCEL",
			"com.jettech.dgt.web.util.mathFuction.SEQUENCE",
		]}
}

用nutz什么版本? 如果是新版,用新的注册方式可以解决, 如果是老版本, 就只能用其他办法了

类似这种写法

public class SEQUENCE implements RunMethod, Plugin{
    BocDataBaseService bocDataBaseService;

    public BocDataBaseService getBocDataBaseService () {
        if (bocDataBaseService == null)
            bocDataBaseService = (BocDataBaseService)ContextLoader.getCurrentWebApplicationContext().getBean("bocDataBaseService");
    }
}

谢谢大哥,不知道为什么只能获取dao的bean,service获取不到,但基本上能解决问题了。

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