我的石英工作中的注射

发布于 2024-10-03 21:40:17 字数 3406 浏览 5 评论 0原文

我想知道如何使用 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>

I would like to know how can I use injection in my job using guice.
As I can't use @Inject on the default constructor may I use it directly on the attribute as follow (I always got a NullPointerException with PersonDAO) ? I know that a DAO has nothing to do here but it's just for testing.

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 ...
  }

} 

In my module I have the following declaration:

bind(PersonDao.class).to(HibernatePersonDaoImpl.class);

Actually I use the PersonDao here 'cause I know it works within another class with injection (but the injection is done on the constructor level there).

Can someone give me an advice?

Here is more info about quartz config:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-10-10 21:40:17

显然此时注射器不可用。
这是解决方案,我测试过并且有效:
http://codesmell.wordpress.com/2009/01/11/quartz -适合/

Apparently the injector was not available at this moment.
Here is the solution, I tested and it works:
http://codesmell.wordpress.com/2009/01/11/quartz-fits/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文