考虑定义一个类型为' mezg.mezos.sessionmanager.repositories.SessionRepository'的bean。在您的配置中

时间:2017-09-02 08:52:40

标签: java spring spring-boot spring-data-redis spring-repositories

我目前正在尝试使用Redis的Spring启动应用程序。当我尝试执行程序时,我遇到了关于redis存储库的spring boot问题。

当我运行应用时出现以下错误:

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.

相关的源文件列在下面

服务界面

package mezg.mezos.sessionmanager.services;

import java.util.List;

public interface SessionService {

    List<String> listAll();

    String getById(String Key);
}

服务实施

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);
    }
}

存储库

package mezg.mezos.sessionmanager.repositories;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SessionRepository extends CrudRepository<String, String> {

}

控制器

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";
    }
}

主要课程

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 {
    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')
}

此论坛上描述了类似的情况,我确实尝试了建议的解决方案,但可以解决此问题。下面列出了一些问题。

堆栈溢出参考

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

Spring boot + redis

其他参考资料

http://www.cnblogs.com/exmyth/p/7119225.html

------------------------ 更新1 -------------- ---------

尝试使用 @ComponentScan @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);
    }
}

------------------------ 更新2 -------------- ---------

完整日志

https://gist.github.com/lrrp/3065dff578daa92dacf4bdc78f90c9b5

有关如何修复此Spring引导错误以及在此类情况下应遵循的任何最佳做法的任何建议?

0 个答案:

没有答案