想在job中直接调用velocity,于是把velocity-1.4.jar放在projectName/lib下面,写好job的源代码,然后启动,却出现classloader问题,提示class找不到.很郁闷的,但是不要紧,grails的job支持注入,于是在spring的配置文件中加入bean定义:
[code:1]
<bean id="velocityHelper" class="com.rw.framework.util.velocity.VelocityHelper"
singleton="true" init-method="init" autowire="byName">
</bean>
[/code:1]
由于是byName,那么job的代码便是:
[code:1]
import com.rw.framework.util.velocity.VelocityHelper

class TestJob {
def VelocityHelper velocityHelper;

def startDelay = "1000"
def timeout = "10000"

def name = "TestJob"
def group = "rwGroup"


def execute() {
def context = velocityHelper.getContext()
context.put("userName", "rw")
System.out.println(velocityHelper.merge(context, modifyPath("test.vm")));
}
}

[/code:1]
启动,可以看到velocityHelper已经被注入job,并且可以流畅的执行了.在job中也可以注入service,只不过我们都不用在applicationContext.xml中自己定义了,grails很体贴的把这部分xml在启动的时候动态的加载到SpringConfig中了.同样,controller中也可以类似的调用.
评论
发表评论

您还没有登录,请登录后发表评论

geszJava
搜索本博客
最近加入圈子
存档
最新评论
评论排行榜