实例化的 bean 的 @PostConstruct class 没有被调用

@PostConstruct of bean instantiated class not getting called

我正在研究这个项目 https://github.com/davidmarquis/redis-scheduler。我在本地进行了设置,并且能够 运行 集成测试用例。现在,我正在尝试创建一个应用程序端点来设置一些延迟任务并启动轮询线程。 我已经能够将我的任务存储在 redis zset 中(通过 RedisTaskScheduler->scheduleAt() 方法)但是我无法启动轮询过程,该过程应该从 zset 中删除键。

以下是我做的-

以下是我创建的入口点 -

package com.github.davidmarquis.redisscheduler;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.JedisPool;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

public class Main {


    private RedisTaskScheduler scheduler;
    private JedisPool pool;

    public static void main(String[] args) {
        provideActors();

    }


    private static void provideActors() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");

        RedisTaskScheduler scheduler = ctx.getBean(RedisTaskScheduler.class);
        Long timestamp = 1593272685000l;
        scheduler.scheduleAt("something_via_main", Instant.ofEpochSecond(timestamp));
        Timestamp ts=new Timestamp(timestamp);
        Date date=new Date(ts.getTime());
        
        //ApplicationContext ctxAnno = new AnnotationConfigApplicationContext(AppConfig.class);
        //pool = (JedisPool) ctxAnno.getBean("jedis");
        //PollingThread thread = (PollingThread) ctxAnno.getBean(PollingThread.class);
        System.out.println("Main method "+date);
    }

    public void stopScheduler() {
        scheduler.stop();
    }

    public static void shutdown() {
        //pool.close();
    }
}

下面是我放在resources里的application-context.xml文件-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!--    <context:property-placeholder location="test-config.properties"/>-->

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6379"/>
        <property name="database" value="0"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>
    <bean id="springTemplateDriver" class="com.github.davidmarquis.redisscheduler.drivers.spring.RedisTemplateDriver">
        <constructor-arg name="redisTemplate" ref="redisTemplate"/>
    </bean>

    <bean id="scheduler" class="com.github.davidmarquis.redisscheduler.RedisTaskScheduler" >
        <constructor-arg name="driver" ref="springTemplateDriver"/>
        <!-- <constructor-arg name="listener" ref="triggerListener"/>-->
        <constructor-arg name="listener">
            <bean class="com.github.davidmarquis.redisscheduler.Implementation"/>
        </constructor-arg>
        <property name="pollingDelayMillis" value="10"/>  <!-- Speeds up polling for tests so that they run quicker. -->
    </bean>

</beans>

21:46:04.215 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@359f7cdf: defining beans [jedisConnectionFactory,redisTemplate,springTemplateDriver,scheduler]; root of factory hierarchy
21:46:04.216 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'jedisConnectionFactory'
21:46:04.216 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'jedisConnectionFactory'
21:46:04.334 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'jedisConnectionFactory' to allow for resolving potential circular references
21:46:04.427 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'jedisConnectionFactory'
21:46:04.588 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'jedisConnectionFactory'
21:46:04.588 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'redisTemplate'
21:46:04.588 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'redisTemplate'
21:46:04.597 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'redisTemplate' to allow for resolving potential circular references
21:46:04.598 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'jedisConnectionFactory'
21:46:04.630 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.data.redis.serializer.StringRedisSerializer#323b36e0'
21:46:04.630 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.data.redis.serializer.StringRedisSerializer#323b36e0'
21:46:04.631 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'redisTemplate'
21:46:04.636 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'redisTemplate'
21:46:04.637 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springTemplateDriver'
21:46:04.637 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'springTemplateDriver'
21:46:04.640 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'redisTemplate'
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'springTemplateDriver' to allow for resolving potential circular references
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'springTemplateDriver'
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'scheduler'
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'scheduler'
21:46:04.666 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springTemplateDriver'
21:46:04.666 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'com.github.davidmarquis.redisscheduler.Implementation#4f18837a'
21:46:04.675 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'com.github.davidmarquis.redisscheduler.Implementation#4f18837a'
21:46:09.144 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'scheduler' to allow for resolving potential circular references
21:46:09.150 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'scheduler'
21:46:09.152 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@2c78d320]
21:46:09.152 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:46:09.156 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
21:46:09.160 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'scheduler'
21:46:11.310 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection
21:46:14.382 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing bound connection.
Main method Sat Jun 27 21:14:45 IST 2020

经过调试发现RedisTaskScheduler的start()方法没有被调用,注解为PostConstruct。

请添加<context:annotation-config/>(或context:component-扫描/)以启用@PostConstruct处理。