自动配置
Condition
修改启动类
package com.example.springboot_redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringBootRedisApplication {
//启动springboot的应用,返回spring的IOC容器
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringBootRedisApplication.class, args);
//获取Bean redisTemplate
Object bean = context.getBean("redisTemplate");
System.out.println(bean);
}
}
POM加入redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
创建文件domain.User和文件config.UserConfig
package com.example.springboot_redis.config;
import com.example.springboot_redis.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
package com.example.springboot_redis.domain;
public class User {
}
修改启动类
package com.example.springboot_redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringBootRedisApplication {
//启动springboot的应用,返回spring的IOC容器
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringBootRedisApplication.class, args);
// //获取Bean redisTemplate
// Object bean = context.getBean("redisTemplate");
// System.out.println(bean);
Object user = context.getBean("user");
System.out.println(user);
}
}
导入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
创建文件
package com.example.springboot_redis.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class ClassCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false;
}
}
这时候在运行就会报错,把false改为true则会正常运行
修改这个文件
package com.example.springboot_redis.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import redis.clients.jedis.Jedis;
public class ClassCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//context:上下文对象 用于获取环境、IOC容器、ClassLoader对象
//metadata:注解元对象 用于获取注解定义的属性值
//1.需求:导入Jedis坐标后创建Bean
//思路:判断redis.clients.jedis.Jedis.class文件是否存在
Boolean flag=true;
try {
Class<?> aClass = Class.forName("redis.clients.jedis.Jedis");
} catch (ClassNotFoundException e) {
flag=true;
}
return flag;
}
}