6
回答
springboot写可执行jar包依赖注入

springboot写可执行jar包,无法依赖注入redisTemplete和使用@Value ,请问有什么办法么?

<无标签>
举报
Yonle
发帖于4个月前 6回/85阅

先学会问问题,pom没,配置文件没,代码没,就说无法依赖注入,用不了@Value注解,怎么知道呢?

感谢您的关注,公司代码,不好全部贴出,简单说明下吧

pom:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
      </plugin>
      <plugin>
         <artifactId>maven-resources-plugin</artifactId>
         <configuration>
            <encoding>utf-8</encoding>
            <useDefaultDelimiters>true</useDefaultDelimiters>
         </configuration>
      </plugin>
   </plugins>
   <resources>
      <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
      </resource>
   </resources>
   <testResources>
      <testResource>
         <directory>src/test/resources</directory>
         <filtering>true</filtering>
      </testResource>
   </testResources>
</build>

配置文件用的application.properties,具体目录:src\main\resources\application.properties

Redis配置:

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        jsonRedisSerializer.setObjectMapper(jackson2ObjectMapperBuilder.build());
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

引用方法:

@Value("${spring.expire-seconds:691200}")//默认缓存8天
private static int expireSeconds;
@Autowired
protected RedisTemplate<String, Object> redisTemplate;

启动正常不报错,但是到了使用redis的时候,redisTemplate为空,没有注入,引起空指针异常。

--- 共有 1 条评论 ---
Kit_lee@Value里边的表达式写错了吧?改成@Value("${spring.expire-seconds}") 然后在application.properties里加: # 过期和秒数 spring.expire-seconds=70000 4个月前 回复

# 过期和秒数

spring.expire-seconds=70000

评论里不能回车换行,避免你写错,这里再回复一下。#号跟着的是注释

spring.expire-seconds=70000这个有的,在application.properties里,编译也能把数字编进去,

:691200

意思是如果取空了,默认为691200秒,还有另一个属性没有写默认,同样是空。其实,最重要的是redisTemplate的这个对象是空,直接连存都存不进去了。

@EnableRedisRepositories  开启redis

@Bean
    public RedisTemplate<Object,Object> redisTemplate() throws UnknownHostException{
        RedisTemplate<Object,Object> template =new RedisTemplate<Object,Object>();
        template.setConnectionFactory(redisConnectionFactory());
        return template;
    }

顶部