1 package com.haifei.springboot8condition; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 @SpringBootApplication 8 public class Springboot8ConditionApplication { 9 10 public static void main(String[] args) { 11 //启动springboot的应用,并返回spring的IOC容器 12 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 13 14 //获取bean redisTemlpate 15 Object redisTemplate = context.getBean("redisTemplate"); 16 System.out.println(redisTemplate); 17 /* 18 pom.xml中引入redis的起步依赖坐标前 19 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available 20 21 引入之后 22 org.springframework.data.redis.core.RedisTemplate@315df4bb 23 */ 24 } 25 26 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.5.3</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.haifei</groupId> 12 <artifactId>springboot8-condition</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot8-condition</name> 15 <description>Demo project for Spring Boot</description> 16 <properties> 17 <java.version>1.8</java.version> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter</artifactId> 23 </dependency> 24 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-test</artifactId> 28 <scope>test</scope> 29 </dependency> 30 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-data-redis</artifactId> 34 </dependency> 35 </dependencies> 36 37 <build> 38 <plugins> 39 <plugin> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-maven-plugin</artifactId> 42 </plugin> 43 </plugins> 44 </build> 45 46 </project>
2
1
1 package com.haifei.springboot8condition.domain; 2 3 public class User { 4 5 }
1 package com.haifei.springboot8condition.config; 2 3 import com.haifei.springboot8condition.condition.ClassCondition; 4 import com.haifei.springboot8condition.domain.User; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Conditional; 7 import org.springframework.context.annotation.Configuration; 8 9 @Configuration 10 public class UserConfig { 11 12 @Bean 13 public User user(){ 14 return new User(); 15 } 16 17 }
1 package com.haifei.springboot8condition; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 @SpringBootApplication 8 public class Springboot8ConditionApplication { 9 10 /*public static void main(String[] args) { 11 //启动springboot的应用,并返回spring的IOC容器 12 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 13 14 //获取bean redisTemlpate 15 Object redisTemplate = context.getBean("redisTemplate"); 16 System.out.println(redisTemplate); 17 // pom.xml中引入redis的起步依赖坐标前 18 // org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available 19 // 引入之后 20 // org.springframework.data.redis.core.RedisTemplate@315df4bb 21 }*/ 22 23 public static void main(String[] args) { 24 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 25 Object user = context.getBean("user"); 26 System.out.println(user); //com.haifei.springboot8condition.domain.User@704b2127 27 } 28 29 }
2
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.5.3</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.haifei</groupId> 12 <artifactId>springboot8-condition</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot8-condition</name> 15 <description>Demo project for Spring Boot</description> 16 <properties> 17 <java.version>1.8</java.version> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter</artifactId> 23 </dependency> 24 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-test</artifactId> 28 <scope>test</scope> 29 </dependency> 30 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-data-redis</artifactId> 34 </dependency> 35 36 <!--<dependency> 37 <groupId>redis.clients</groupId> 38 <artifactId>jedis</artifactId> 39 </dependency>--> 40 </dependencies> 41 42 <build> 43 <plugins> 44 <plugin> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-maven-plugin</artifactId> 47 </plugin> 48 </plugins> 49 </build> 50 51 </project>
1 package com.haifei.springboot8condition.config; 2 3 import com.haifei.springboot8condition.condition.ClassCondition; 4 import com.haifei.springboot8condition.domain.User; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Conditional; 7 import org.springframework.context.annotation.Configuration; 8 9 @Configuration 10 public class UserConfig { 11 12 @Bean 13 @Conditional(ClassCondition.class) 14 public User user(){ 15 return new User(); 16 } 17 18 }
1 package com.haifei.springboot8condition.condition; 2 3 import org.springframework.context.annotation.Condition; 4 import org.springframework.context.annotation.ConditionContext; 5 import org.springframework.core.type.AnnotatedTypeMetadata; 6 7 public class ClassCondition implements Condition { 8 9 @Override 10 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 11 //1.需求: 导入Jedis坐标后创建Bean 12 //思路:判断redis.clients.jedis.Jedis.class文件是否存在 13 boolean flag = true; 14 try { 15 Class<?> cls = Class.forName("redis.clients.jedis.Jedis"); 16 } catch (ClassNotFoundException e) { 17 flag = false; 18 } 19 return flag; 20 } 21 22 }
1 package com.haifei.springboot8condition; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 @SpringBootApplication 8 public class Springboot8ConditionApplication { 9 10 public static void main(String[] args) { 11 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 12 Object user = context.getBean("user"); 13 System.out.println(user); 14 /* 15 pom.xml中有jedis的依赖坐标:com.haifei.springboot8condition.domain.User@c1a4620 16 pom.xml中无jedis的依赖坐标:NoSuchBeanDefinitionException: No bean named 'user' available 17 */ 18 } 19 20 }
3
1 package com.haifei.springboot8condition.config; 2 3 import com.haifei.springboot8condition.condition.ClassCondition; 4 import com.haifei.springboot8condition.condition.ConditionOnClass; 5 import com.haifei.springboot8condition.domain.User; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Conditional; 8 import org.springframework.context.annotation.Configuration; 9 10 @Configuration 11 public class UserConfig { 12 13 @Bean 14 // @Conditional(ClassCondition.class) 15 @ConditionOnClass("redis.clients.jedis.Jedis") //使用自定义注解 16 public User user(){ 17 return new User(); 18 } 19 20 }
1 package com.haifei.springboot8condition.condition; 2 3 import org.springframework.context.annotation.Conditional; 4 5 import java.lang.annotation.*; 6 7 /** 8 * 自定义注解 9 */ 10 @Target({ElementType.TYPE, ElementType.METHOD}) 11 @Retention(RetentionPolicy.RUNTIME) 12 @Documented 13 //以上三个来自@Conditional的元注解 14 @Conditional(ClassCondition.class) 15 public @interface ConditionOnClass { 16 17 String[] value(); 18 19 }
1 package com.haifei.springboot8condition.condition; 2 3 import org.springframework.context.annotation.Condition; 4 import org.springframework.context.annotation.ConditionContext; 5 import org.springframework.core.type.AnnotatedTypeMetadata; 6 7 public class ClassCondition implements Condition { 8 9 @Override 10 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 11 //1.需求: 导入Jedis坐标后创建Bean 12 //思路:判断redis.clients.jedis.Jedis.class文件是否存在 13 boolean flag = true; 14 try { 15 Class<?> cls = Class.forName("redis.clients.jedis.Jedis"); 16 } catch (ClassNotFoundException e) { 17 flag = false; 18 } 19 return flag; 20 } 21 22 }
1 package com.haifei.springboot8condition; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 @SpringBootApplication 8 public class Springboot8ConditionApplication { 9 10 public static void main(String[] args) { 11 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 12 Object user = context.getBean("user"); 13 System.out.println(user); 14 /* 15 pom.xml中有jedis的依赖坐标:com.haifei.springboot8condition.domain.User@c1a4620 16 pom.xml中无jedis的依赖坐标:NoSuchBeanDefinitionException: No bean named 'user' available 17 */ 18 } 19 20 }
4
1 package com.haifei.springboot8condition.config; 2 3 import com.haifei.springboot8condition.condition.ClassCondition; 4 import com.haifei.springboot8condition.condition.ConditionOnClass; 5 import com.haifei.springboot8condition.domain.User; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Conditional; 8 import org.springframework.context.annotation.Configuration; 9 10 @Configuration 11 public class UserConfig { 12 13 @Bean 14 // @Conditional(ClassCondition.class) 15 //使用自定义注解 16 // @ConditionOnClass("redis.clients.jedis.Jedis") 17 @ConditionOnClass("com.alibaba.fastjson.JSON") 18 public User user(){ 19 return new User(); 20 } 21 22 }
1 package com.haifei.springboot8condition.condition; 2 3 import org.springframework.context.annotation.Condition; 4 import org.springframework.context.annotation.ConditionContext; 5 import org.springframework.core.type.AnnotatedTypeMetadata; 6 7 import java.util.Map; 8 9 public class ClassCondition implements Condition { 10 11 /*@Override 12 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 13 //需求1: 导入Jedis坐标后创建Bean 14 //思路:判断redis.clients.jedis.Jedis.class文件是否存在 15 16 boolean flag = true; 17 try { 18 Class<?> cls = Class.forName("redis.clients.jedis.Jedis"); 19 } catch (ClassNotFoundException e) { 20 flag = false; 21 } 22 return flag; 23 }*/ 24 25 /** 26 * @param context 上下文对象。用于获取环境,IOC容器,ClassLoader对象 27 * @param metadata 注解元对象。可以用于获取注解定义的属性值 28 * @return 29 */ 30 @Override 31 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 32 //需求2: 导入通过自定义的注解的属性值value指定坐标后创建Bean 33 34 //2-1 获取属性值value(自定义为string[]了) 35 Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName()); 36 // System.out.println(map); //{value=[redis.clients.jedis.Jedis]} 37 String[] value = (String[])map.get("value"); 38 39 //2-2 遍历属性值value 40 boolean flag = true; 41 try { 42 for (String className : value) { 43 Class<?> cls = Class.forName(className); 44 } 45 } catch (ClassNotFoundException e) { 46 flag = false; 47 } 48 return flag; 49 } 50 51 }
5
1 // 2 // Source code recreated from a .class file by IntelliJ IDEA 3 // (powered by Fernflower decompiler) 4 // 5 6 package org.springframework.boot.autoconfigure.data.redis; 7 8 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 9 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 10 import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; 11 import org.springframework.boot.context.properties.EnableConfigurationProperties; 12 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Configuration; 14 import org.springframework.context.annotation.Import; 15 import org.springframework.data.redis.connection.RedisConnectionFactory; 16 import org.springframework.data.redis.core.RedisOperations; 17 import org.springframework.data.redis.core.RedisTemplate; 18 import org.springframework.data.redis.core.StringRedisTemplate; 19 20 @Configuration( 21 proxyBeanMethods = false 22 ) 23 @ConditionalOnClass({RedisOperations.class}) 24 @EnableConfigurationProperties({RedisProperties.class}) 25 @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) 26 public class RedisAutoConfiguration { 27 public RedisAutoConfiguration() { 28 } 29 30 @Bean 31 @ConditionalOnMissingBean( 32 name = {"redisTemplate"} 33 ) 34 @ConditionalOnSingleCandidate(RedisConnectionFactory.class) 35 public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { 36 RedisTemplate<Object, Object> template = new RedisTemplate(); 37 template.setConnectionFactory(redisConnectionFactory); 38 return template; 39 } 40 41 @Bean 42 @ConditionalOnMissingBean 43 @ConditionalOnSingleCandidate(RedisConnectionFactory.class) 44 public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { 45 StringRedisTemplate template = new StringRedisTemplate(); 46 template.setConnectionFactory(redisConnectionFactory); 47 return template; 48 } 49 }
6
1 package com.haifei.springboot8condition.config; 2 3 import com.haifei.springboot8condition.condition.ClassCondition; 4 import com.haifei.springboot8condition.condition.ConditionOnClass; 5 import com.haifei.springboot8condition.domain.User; 6 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Conditional; 9 import org.springframework.context.annotation.Configuration; 10 11 @Configuration 12 public class UserConfig { 13 14 @Bean 15 // @Conditional(ClassCondition.class) 16 //使用自定义注解 17 // @ConditionOnClass("redis.clients.jedis.Jedis") 18 // @ConditionOnClass("com.alibaba.fastjson.JSON") 19 public User user(){ 20 return new User(); 21 } 22 23 @Bean 24 //利用springboot提供的ConditionalOnXXX 25 @ConditionalOnProperty(name = "itcast", havingValue = "itheima") //该注解:当配置文件中存在一个键是“itcast”,且其值是"itheima"时才会加载这个bean 26 public User user2(){ 27 return new User(); 28 } 29 30 }
1 package com.haifei.springboot8condition; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 @SpringBootApplication 8 public class Springboot8ConditionApplication { 9 10 /*public static void main(String[] args) { 11 //启动springboot的应用,并返回spring的IOC容器 12 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 13 14 //获取bean redisTemlpate 15 Object redisTemplate = context.getBean("redisTemplate"); 16 System.out.println(redisTemplate); 17 // pom.xml中引入redis的起步依赖坐标前 18 // org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available 19 // 引入之后 20 // org.springframework.data.redis.core.RedisTemplate@315df4bb 21 }*/ 22 23 24 /* public static void main(String[] args) { 25 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 26 Object user = context.getBean("user"); 27 System.out.println(user); 28 // pom.xml中有jedis的依赖坐标:com.haifei.springboot8condition.domain.User@c1a4620 29 // pom.xml中无jedis的依赖坐标:NoSuchBeanDefinitionException: No bean named 'user' available 30 }*/ 31 32 33 public static void main(String[] args) { 34 ConfigurableApplicationContext context = SpringApplication.run(Springboot8ConditionApplication.class, args); 35 Object user2 = context.getBean("user2"); 36 System.out.println(user2); 37 //application.properties中未配置itcast=itheima时:NoSuchBeanDefinitionException: No bean named 'user2' available 38 //application.properties中配置itcast=itheima时:com.haifei.springboot8condition.domain.User@d4ab71a 39 } 40 41 }
3