为什么 @autowiring 字段为空?

huangapple go评论70阅读模式
英文:

Why is @autowiring field null?

问题

以下是您提供的代码的翻译部分:

我有一个类似这样的 RedisConfig

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

    /**
     * Lettuce 连接工厂
     * 
     * @return Lettuce 连接工厂
     */
    @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 模板
     * 
     * @return redisTemplate
     */
    @Bean
    public RedisTemplate<Object,Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory());
        return redisTemplate;
    }

    /**
     * Redis 字符串模板
     * 
     * @return redisStringTemplate redis 字符串模板
     */
    @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;
    }

    /**
     * 配置 Redis 操作
     * 
     * @return Redis 配置操作
     */
    @Bean
    public ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }
}


然后我尝试在一个类中使用它类似这样

public class Someclass {
    
    private static Long TIMEOUT = 60L;
    
     /** 注入实际模板 */
    @Autowired
    private RedisTemplate<String, String> redisStringTemplate;

    /**
     * 在 Redis 中缓存用户。
     * 
     * @param user Exavault 用户
     */
    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);        
    }    
}


我在这里漏掉了什么
英文:

I have a RedisConfig like this:

@Configuration
@EnableRedisRepositories
@EnableRedisHttpSession
@ComponentScan(&quot;com.where.redis.config.is&quot;)
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(&quot;REDIS_HOST&quot;) != null ? System.getenv(&quot;REDIS_HOST&quot;)
: &quot;localhost&quot;;
RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(host,
6379);
return new LettuceConnectionFactory(serverConfig, clientConfig);
}
/**
* Redis Template
* 
* @return redisTemplate
*/
@Bean
public RedisTemplate&lt;Object,Object&gt; redisTemplate() {
RedisTemplate&lt;Object, Object&gt; redisTemplate = new RedisTemplate&lt;&gt;();
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
return redisTemplate;
}
/**
* Redis String Template
* 
* @return redisStringTemplate redis String Template
*/
@Bean(name = &quot;redisStringTemplate&quot;)
@Primary
public RedisTemplate&lt;String, String&gt; redisStringTemplate() {
RedisTemplate&lt;String, String&gt; redisStringTemplate = new RedisTemplate&lt;&gt;();
redisStringTemplate.setConnectionFactory(lettuceConnectionFactory());
JdkSerializationRedisSerializer jdkSerializationRedisSerializer 
= new JdkSerializationRedisSerializer();
RedisSerializer&lt;String&gt; 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;
}
}

And then I am trying to use it in a class like this:

public class Someclass {
private static Long TIMEOUT = 60L;
/** Inject the actual template */
@Autowired
private RedisTemplate&lt;String, String&gt; 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());
}
} 

I start the app as follows:

@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = &quot;com.where.all.java.code.is&quot;)
public class SchedulingTasksApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = 
SpringApplication.run(SchedulingTasksApplication.class, args);		
}	
}

What am I missing here?

答案1

得分: 4

以下是翻译后的内容:

要使用@Autowired的类必须具有Spring注解。请向类中添加以下其中一个注解:@Component、@Repository、@Service、@Controller、@Configuration。

另外,所在的包必须在ComponentScan搜索路径内。将包移动到被扫描的位置,或者配置ComponentScan来修复这个问题。

@Component
public class Someclass {

    private static Long TIMEOUT = 60L;

    /** 注入实际的模板 */
    @Autowired
    private RedisTemplate<String, String> redisStringTemplate;

    /**
     * 在redis中缓存用户。
     * 
     * @param user Exavault用户
     */
    private void cacheUser(ExavaultUser user) {

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

另外,@dassum为同样的问题添加了注释。

英文:

The class in which you want to use @Autowired does not have a Spring annotation. Add one of the following annotatons to the class: @Component, @Repository, @Service, @Controller, @Configuration.

Also, The package is must be inside the ComponentScan search path. Move the package to a scanned location or configure the ComponentScan to fix this.

@Component
public class Someclass {
private static Long TIMEOUT = 60L;
/** Inject the actual template */
@Autowired
private RedisTemplate&lt;String, String&gt; 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());
}
} 

Also, @dassum has added comment for the same.

huangapple
  • 本文由 发表于 2020年4月4日 14:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61024474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定