在Spring Framework中的@Repository之后运行@Component

时间:2018-03-30 09:42:41

标签: java spring redis spring-data spring-bean

我在spring项目中使用 Redis ,我定义了一个使用Redis存储库和@Autowire的@Component类,但在调试应用程序时,@ Component类构造函数在bean之前运行连接到Redis,因此应用程序退出时出现此错误:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataManager' defined in file [/mnt/D/ms/projects/EventDetection/out/production/classes/ir/rahati/data/datamanager/DataManager.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'ir.rahati.data.messagegraph.redis.RedisMessageGraphRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

DataManager类

@Component
public class DataManager {
        private final RedisMessageGraph messageGraph;
    @Autowired
    public DataManager( RedisMessageGraphRepository redisMessageGraphRepository) {
        this.messageGraph=new RedisMessageGraph(redisMessageGraphRepository);
    }
}

Redis存储库接口

@Repository
public interface RedisMessageGraphRepository extends CrudRepository<MessageGraphModel, String> {
}

配置类

@Configuration
@EnableRedisRepositories("ir.rahati.data.messagegraph")
public class RedisMessageGraphConfig {
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
        redisStandaloneConfiguration.setDatabase(3);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }


    @Bean(name = "redis15")
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());

        return template;
    }
}

0 个答案:

没有答案