英文:
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("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;
}
}
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<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());
}
}
I start the app as follows:
@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);
}
}
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<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());
}
}
Also, @dassum has added comment for the same.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论