我想知道如何在我的工作中使用Guice进行依赖注入。 由于我不能在默认构造函数中使用@Inject,那么我是否可以直接在属性上使用它,就像下面这样(我总是得到PersonDAO的NullPointerException)? 我知道DAO在这里没什么用,但这只是为了测试。
public class SimpleQuartzJob implements Job {
@Inject PersonDao Person;
private static Logger logger = Logger.getLogger(SimpleQuartzJob.class.getName());
public SimpleQuartzJob() {
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if (logger.isDebugEnabled()) logger.debug("In SimpleQuartzJob - executing its JOB at "
+ new Date() + " by " + context.getTrigger().getName());
// ... LOGIC ...
}
}
在我的模块中,我有以下声明:
bind(PersonDao.class).to(HibernatePersonDaoImpl.class);
实际上,我在这里使用PersonDao是因为我知道它可以在另一个类中通过注入使用(但那里的注入是在构造函数级别上完成的)。有人能给我建议吗?以下是有关quartz配置的更多信息:Web.xml: <servlet>
<servlet-name>QuartzInitializer</servlet-name>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>config-file</param-name>
<param-value>/quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
quartz.properties:
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames=quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5
quartz-config.xml :
<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>mail-job</name>
<group>MYJOB_GROUP</group>
<description>The job description</description>
<job-class>svc.data.server.quartz.SimpleQuartzJob</job-class>
</job>
<trigger>
<cron>
<name>my-trigger</name>
<group>MYTRIGGER_GROUP</group>
<job-name>mail-job</job-name>
<job-group>MYJOB_GROUP</job-group>
<cron-expression>10 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>