I'm using custom crudrespository to persist data in redis. However, I'm unable to autowire custom repository.
All the configuration seems correct and redis is running on my local.
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomRepository extends CrudRepository<String,
Long> {
String get(String key);
void put(String key, String value);
}
//////////
public class StorageServiceImpl implements IStorageService {
@Autowired
private CustomRepository respository;
@Override
public void saveParameter() {
this.respository.put("key1","value1");
}
@Override
public String getParameter() {
return this.respository.get("key1");
}
/////
@Service
public interface IStorageService {
void saveParameter();
String getParameter();
}
///////
@SpringBootApplication(scanBasePackages = {"com.example.cache"})
@EnableRedisRepositories(basePackages = {"com.example.cache.repository"})
public class ApplicationConfiguration {
public static void main(String[] args){
SpringApplication.run(ApplicationConfiguration.class, args);
new StorageServiceImpl().saveParameter();
System.out.println(new StorageServiceImpl().getParameter());
}
}
When I try running this application using gradle bootRun, I get
Exception in thread "main" java.lang.NullPointerException at com.example.cache.impl.StorageServiceImpl.saveParameter(StorageServiceImpl.java:16) at com.example.cache.ApplicationConfiguration.main(ApplicationConfiguration.java:17)
Not sure what's wrong?