Reputation: 208
I have a problem with a project. I am creating a very primitive chat program, with spring and redis.
I have the redis connection as a Spring Bean:
private HashOperations hashOps;
@Autowired
public UserRepositoryImpl(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@PostConstruct
private void init() {
hashOps = redisTemplate.opsForHash();
}
Until here everything seems to work, and the hashops is declared as template. But now I call
public boolean createUser(User user){
if(user.getName().isEmpty() || user.getPassword().isEmpty()){
return false;
}
user.setStatus("");
if(hashOps.putIfAbsent(KEY, user.getName(), user)){
List<String> following = null;
hashOps.put("isFollowing", user.getName(),following);
FollowListImpl fLI = new FollowListImpl(this.redisTemplate);
fLI.follow("global", user);
return true;
}
return false;
}
to create the first data (a user), in redis. THis method is in the same class as the declaration of hashOps
but hasOps
is now NULL. But when I use a project from someone else, who has a similar, solution hashOps
keeps the template even through method calls.
Redis is Connected in the main class (starter class):
@SpringBootApplication
public class CommunicatorApplication {
@Bean
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.setHostName("localhost");
jedisConnectionFactory.setPassword("");
return jedisConnectionFactory;
}
@Bean
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(getConnectionFactory());
return template;
}
public static void main(String[] args) {
SpringApplication.run(CommunicatorApplication.class, args);
}
}
Now the question is, how does spring actually keep hashOps
when it is working properly, and what did I do wrong, that it doesn't keep it in my code.
I call createUser
here:
@Controller
public class LoginController {
@RequestMapping(value = "/login", params = "btnRegister")
public String loginRegister(@ModelAttribute Login login, Model model){
model.addAttribute("login", login != null ? login : new Login());
UserRepositoryImpl user = new UserRepositoryImpl(null);
user.createUser(login.getName(),login.getPassword());
System.out.println(user.findUser(login.getName()));
return "login";
}
Upvotes: 0
Views: 1492
Reputation: 200527
This line is instantiating a new instance of UserRepositoryImpl
:
UserRepositoryImpl user = new UserRepositoryImpl(null);
This instance you are creating is not attached to Spring in any way. You are even explicitly passing null
as the redisTemplate
argument here, so of course this is not going to work.
You need to use the instance of UserRepositoryImpl
that is created and managed by Spring. In order to do this you need to instruct Spring to wire the instance into the LoginController
. One method of accomplishing this is to use the @Autowired
annotation like so:
@Controller
public class LoginController {
@Autowired
private UserRepositoryImpl userRepository;
@RequestMapping(value = "/login", params = "btnRegister")
public String loginRegister(@ModelAttribute Login login, Model model){
model.addAttribute("login", login != null ? login : new Login());
userRepository.createUser(login.getName(),login.getPassword());
System.out.println(userRepository.findUser(login.getName()));
return "login";
}
Upvotes: 1