I am currently trying out a Spring boot application with Redis.I have a problem related to spring boot concerning redis repositories when I try to execute the program.
When I run the app I get the following error:
2017-09-02 09:21:37.065 INFO 5130 --- [st-startStop-18] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-02 09:21:37.106 WARN 5130 --- [st-startStop-18] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller': Unsatisfied dependency expressed through method 'setSessionService' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionServiceImpl' defined in file [/rezsystem/apache-tomcat-8.0.36/webapps/sessionmanager/WEB-INF/classes/rezg/rezos/sessionmanager/services/SessionServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'mezg.mezos.sessionmanager.repositories.SessionRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-09-02 09:21:37.118 INFO 5130 --- [st-startStop-18] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-09-02 09:21:37.306 ERROR 5130 --- [st-startStop-18] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
**Description:**
Parameter 0 of constructor in mezg.mezos.sessionmanager.services.SessionServiceImpl required a bean of type 'mezg.mezos.sessionmanager.repositories.SessionRepository' that could not be found.
**Action:**
Consider defining a bean of type 'mezg.mezos.sessionmanager.repositories.SessionRepository' in your configuration.
The related source files are listed below
Service Interface
package mezg.mezos.sessionmanager.services;
import java.util.List;
public interface SessionService {
List<String> listAll();
String getById(String Key);
}
Service Implemention
package mezg.mezos.sessionmanager.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import mezg.mezos.sessionmanager.repositories.SessionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class SessionServiceImpl implements SessionService{
private SessionRepository sessionRepository; //error refere to this
@Autowired
public SessionServiceImpl(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}
@Override
public List<String> listAll() {
List<String> allSessions = new ArrayList<>();
sessionRepository.findAll().forEach(allSessions::add);
return allSessions;
}
@Override
public String getById(String Key) {
return sessionRepository.findOne(Key);
}
}
Repository
package mezg.mezos.sessionmanager.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SessionRepository extends CrudRepository<String, String> {
}
Controller
package mezg.mezos.sessionmanager.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import mezg.mezos.sessionmanager.services.SessionService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.ui.Model;
@RestController
@RequestMapping(value="/sessionmanager")
public class SessionController {
final static Logger logger = LoggerFactory.getLogger(SessionController.class);
private SessionService sessionService;
@Autowired
public void setSessionService(SessionService sessionService) {
this.sessionService = sessionService;
}
@RequestMapping(value = "/servicecheck", method = RequestMethod.GET)
public String servicecheck() {
return "This is the First Message From Remote session manager service!";
}
@RequestMapping(value ="/all", method = RequestMethod.GET)
public String listAllSessions(Model model){
model.addAttribute("sessions", sessionService.listAll());
return "display all Sessions";
}
@RequestMapping("/session/show/{key}")
public String getProduct(@PathVariable String key, Model model){
model.addAttribute("product", sessionService.getById(key));
return "display selected Sessions";
}
}
Main class
package mezg.mezos.sessionmanager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("mezg.mezos.sessionmanager")
public class SessionStoreApplication {
public static void main(String[] args) {
SpringApplication.run(SessionStoreApplication.class, args);
}
Gradle Dependencies
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
There were similar scenarios described on this forum and I did try out the suggested solutions but could resolve this issue.Some of these issue are listed below.
Stack overflow Reference
Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration
Consider defining a bean of type 'package' in your configuration [Spring-Boot]
Consider defining a bean of type 'service' in your configuration [Spring boot]
Spring boot CrudRepo Define a bean
Other References
http://www.cnblogs.com/exmyth/p/7119225.html
------------------------UPDATE 1-----------------------
Tried Updating the Main Class with @ComponentScan and @EnableRedisRepositories
package mezg.mezos.sessionmanager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"mezg.mezos.sessionmanager","mezg.mezos.sessionmanager.controller","mezg.mezos.sessionmanager.services","mezg.mezos.sessionmanager.repositories"})
@EnableRedisRepositories("mezg.mezos.sessionmanager.repositories")
public class SessionStoreApplication {
public static void main(String[] args) {
SpringApplication.run(SessionStoreApplication.class, args);
}
}
------------------------UPDATE 2-----------------------
Full Logs
https://gist.github.com/lrrp/3065dff578daa92dacf4bdc78f90c9b5
Any suggestion on how how I can fix this Spring boot error and any best practices to follow in situations such as this?