软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot自动装配之Condition深入讲解

SpringBoot自动装配之Condition深入讲解

作者:不死鸟.亚历山大.狼崽子  发布时间:2023-12-03 02:20:29 

标签:SpringBoot,自动装配,Condition

Condition是在Spring 4.0 增加的条件判断功能,通过这个可以功能可以实现选择性的创建 Bean操作。

思考:

SpringBoot是如何知道要创建哪个Bean的?比如SpringBoot是如何知道要创建RedisTemplate的?

看一个例子:

当我们没导入redis-start时,会报错

引出问题

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:874)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1358)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
at com.example.condition.SpringbootDemo01Application.main(SpringbootDemo01Application.java:13)

当导入redis起步依赖后

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
org.springframework.data.redis.core.RedisTemplate@5a6d5a8f

问题:

SpringBoot是怎么知道我们导入redis坐标的呢?

案例一

需求:

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

通过condition设置加载或者不加载。

新建实体类:

package com.example.condition.entity;
public class User {
}

新建配置文件:

package com.example.condition.config;
import com.example.condition.condition.ClassCondition;
import com.example.condition.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
   @Bean
   @Conditional(ClassCondition.class)
   public User user(){
       return new User();
   }
}

新建condition:

如果为true则加载,如果为false则不加载

package com.example.condition.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 true;
   }
}

测试:

package com.example.condition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootDemo01Application {
   public static void main(String[] args) {
       //启动SpringBoot应用,返回Spring的IOC容器
       ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args);
       Object user = context.getBean("user");
       System.out.println(user);
   }
}

案例二

需求:

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

导入Jedis坐标后,加载该Bean,没导入,则不加载。

新建User实体类:

package com.example.condition.entity;
public class User {
}

新建配置文件:

package com.example.condition.config;
import com.example.condition.condition.ClassCondition;
import com.example.condition.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
   @Bean
   @Conditional(ClassCondition.class)
   public User user(){
       return new User();
   }
}

condition通过反射判断jedis是否已经加载完毕

package com.example.condition.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) {
       boolean flag =true;
       try {
           Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
       }catch (ClassNotFoundException e){
           flag =false;
       }
       return flag;
   }
}

测试类:

package com.example.condition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootDemo01Application {
   public static void main(String[] args) {
       //启动SpringBoot应用,返回Spring的IOC容器
       ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args);
       Object user = context.getBean("user");
       System.out.println(user);
   }
}

引入jedis进行测试判断:

<dependency>
           <groupId>redis.clients</groupId>
           <artifactId>jedis</artifactId>
           <version>3.6.0</version>
       </dependency>

案例三

需求:

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

  • 导入Jedis坐标后,加载该Bean,没导入,则不加载。

  • 将类的判断定义为动态的。判断哪个字节码文件存在可以动态指定。

实体类:

package com.example.condition.entity;
public class User {
}

自定义注解:

package com.example.condition.condition;import org.springframework.context.annotation.Conditional;import java.lang.annotation.*;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Conditional(ClassCondition.class)public @interface ConditionOnClass {    String[] value();}

Condition类:

package com.example.condition.condition;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ClassCondition.class)
public @interface ConditionOnClass {
   String[] value();
}

配置类:

package com.example.condition.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
public class ClassCondition implements Condition {
   /**
    * @param context  上下文对象,用于获取环境,ClassLoader对象
    * @param metadata 注解的元对象,可以用于注解定义的属性值
    * @return
    */
   @Override
   public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
       Environment environment = context.getEnvironment();
       //1.需求:导入指定坐标后创建Bean
       //思路:判断指定坐标文件是否存在
       //获取注解属性值 value
       Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
       String[] value = (String[]) map.get("value");
       boolean flag = true;
       try {
           for (String className : value) {
               Class<?> cls = Class.forName(className);
           }
       } catch (ClassNotFoundException e) {
           flag = false;
       }
       return flag;
   }
}

测试类:

package com.example.condition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootDemo01Application {
   public static void main(String[] args) {
       //启动SpringBoot应用,返回Spring的IOC容器
       ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args);
       Object user = context.getBean("user");
       System.out.println(user);
   }
}
  • ConditionalOnClass:判断环境中是否有对应字节码文件才初始化Bean

  • ConditionalOnMissingBean:判断环境中没有对应Bean才初始化Bean

来源:https://blog.csdn.net/u013938578/article/details/128680784

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com