为什么@autowiring 字段为空?

Why is @autowiring field null?

我有这样的 RedisConfig:

@Configuration
@EnableRedisRepositories
@EnableRedisHttpSession
@ComponentScan("com.where.redis.config.is")
public class RedisConfig {

    /**
     * Lettuce Connection Factory
     * 
     * @return Lettuce Connection Factory
     */
    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory() {
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .readFrom(ReadFrom.REPLICA_PREFERRED).build();

        String host = System.getenv("REDIS_HOST") != null ? System.getenv("REDIS_HOST")
                : "localhost";
        RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(host,
                6379);

        return new LettuceConnectionFactory(serverConfig, clientConfig);
    }

    /**
     * Redis Template
     * 
     * @return redisTemplate
     */
    @Bean
    public RedisTemplate<Object,Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory());
        return redisTemplate;
    }

    /**
     * Redis String Template
     * 
     * @return redisStringTemplate redis String Template
     */
    @Bean(name = "redisStringTemplate")
    @Primary
    public RedisTemplate<String, String> redisStringTemplate() {
        RedisTemplate<String, String> redisStringTemplate = new RedisTemplate<>();
        redisStringTemplate.setConnectionFactory(lettuceConnectionFactory());
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer 
            = new JdkSerializationRedisSerializer();
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        redisStringTemplate.setKeySerializer(stringSerializer);
        redisStringTemplate.setHashKeySerializer(stringSerializer);
        redisStringTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        redisStringTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
        redisStringTemplate.setEnableTransactionSupport(true);
        redisStringTemplate.afterPropertiesSet();
        return redisStringTemplate;
    }

    /**
     * Configure Redis Action
     * 
     * @return Redis Configure Action
     */
    @Bean
    public ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }
}

然后我尝试像这样在 class 中使用它:

public class Someclass {

    private static Long TIMEOUT = 60L;

     /** Inject the actual template */
    @Autowired
    private RedisTemplate<String, String> redisStringTemplate;

    /**
     * Stores a User in redis. 
     * 
     * @param user Exavault User
     */
    private void cacheUser(ExavaultUser user) {

        redisStringTemplate.expire(REDIS_KEY, TIMEOUT, TimeUnit.MINUTES);
        redisStringTemplate.afterPropertiesSet();
        redisStringTemplate.opsForHash().put(REDIS_KEY, user.getUsername(), 
                user.getAccessToken());
    }
} 

我按如下方式启动应用程序:

@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = "com.where.all.java.code.is")
public class SchedulingTasksApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = 
                SpringApplication.run(SchedulingTasksApplication.class, args);      
    }   
}

我在这里错过了什么?

您想在其中使用@Autowired 的class 没有Spring 注释。将以下注释之一添加到 class:@Component、@Repository、@Service、@Controller、@Configuration。

此外,包必须位于 ComponentScan 搜索路径内。将包移动到扫描位置或配置 ComponentScan 来修复此问题。

@Component
public class Someclass {

    private static Long TIMEOUT = 60L;

     /** Inject the actual template */
    @Autowired
    private RedisTemplate<String, String> redisStringTemplate;

    /**
     * Stores a User in redis. 
     * 
     * @param user Exavault User
     */
    private void cacheUser(ExavaultUser user) {

        redisStringTemplate.expire(REDIS_KEY, TIMEOUT, TimeUnit.MINUTES);
        redisStringTemplate.afterPropertiesSet();
        redisStringTemplate.opsForHash().put(REDIS_KEY, user.getUsername(), 
                user.getAccessToken());
    }
} 

另外,@dassum 也添加了评论。