通过redis自动配置过程看这两个注解的联合使用,达到自动配置
springboot项目,maven
application.properties
配置redis
...
spring.redis.port=7379
...
需要获取配置的类,比如@Configuration注解的配置类,需要获取redis配置的其他类,redisProperties.getHost() 就可以取到配置
import org.springframework.boot.autoconfigure.data.redis.RedisProperties
@EnableConfigurationProperties(RedisProperties.class)
class JedisMy{
@Autowired
private RedisProperties redisProperties
}
获取使用内置的Environment获取配置,需要指定配置的key,上面的自动配置,key就是类的属性,redis的配置都是RedisProperties的属性
import org.springframework.core.env.Environment
class ddd{
@Autowired
private Environment env;
public void dd(){
env.getProperty("spring.redis.port");
}
}