2010-11-23 72 views
10

我想知道如何使用注射在我的工作中使用guice。 因爲我不能使用默認構造函數@Inject我可以直接使用它的屬性如下(我總是得到一個NullPointerException與PersonDAO)?我知道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的在這裏,因爲我知道它在另一個類中有注射(但注射在構造水平上進行那裏)。

有人可以給我一個建議嗎?

這裏是關於石英配置的詳細信息:

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 

石英-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> 

回答