当前位置: 代码迷 >> 综合 >> spring注解@Conditional
  详细解决方案

spring注解@Conditional

热度:102   发布时间:2023-10-26 08:33:15.0
@Conditional注解在spring底层被大量使用。

@Conditional可以被加在类上或者是方法上

接下来看具体的用法:

从spring环境中获取当前系统的类型,如果是Window系统则注册bill类,如果是Linux系统则注册linux类

 配置类:

其中WindowCondition.class和LiunxCondition.class,是我自己实现的Condition类

package com.annotation.config;import com.annotation.conditional.LinuxCondition;
import com.annotation.conditional.WindowCondition;
import com.annotation.entities.Person;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;@Configuration
//将conditional注解放在类上,若不满足条件,则类中所有的bean都不会被注册到IOC容器中
//@Conditional({WindowCondition.class})
public class MainConfig2 {//    @Scope(value = WebApplicationContext.SCOPE_REQUEST)//request
//    @Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION)//session
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)//prototype@Lazy@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)//singleton@Bean(name = "person")public Person person(){Person person = new Person();person.setName("lqf");person.setEmail("lqf@163.com");return person;}/*** @Conditational按照条件给IOC容器中注册bean*/@Conditional({WindowCondition.class})@Bean(name = "bill")public Person person01(){return new Person(1,"bill","bill@email");}@Conditional({LinuxCondition.class})@Bean(name = "linus")public Person person02(){return new Person(2,"linus","linus@email");}}

下面是两个Condition类,需要实现spring的Condition接口,重写metches方法,该方法返回出则注该bean,返回false不注册该bean。

package com.annotation.conditional;import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;public class WindowCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {String osName = context.getEnvironment().getProperty("os.name");if (osName.contains("Windows")){return true;}return false;}
}
package com.annotation.conditional;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;public class LinuxCondition implements Condition {/*** ConditionContext:判断条件能使用的上下文环境* AnnotatedTypeMetadata:注解信息*/@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {//获取IOC容器使用的beanfactoryConfigurableListableBeanFactory beanFactory = context.getBeanFactory();//获取容器中是否有person的声明(容器中所有的bean先声明,然后再实例,所以使用该方法不用担心bean还没有被IOC声明而获取不到)System.out.println("是否包含person的定义" + beanFactory.containsBean("person"));//获取类加载器ClassLoader classLoader = context.getClassLoader();//获取当前环境信息Environment environment = context.getEnvironment();//获取bean定义的注册类BeanDefinitionRegistry registry = context.getRegistry();String osName = environment.getProperty("os.name");System.out.println(osName);if (osName.contains("Linux")){return true;}return false;}
}

 

 下面是测试方法:

    @Testpublic void testConditational(){setApplicationContext2();String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();for (String beanDefinitionName : beanDefinitionNames) {System.out.println(beanDefinitionName);}}

若果想要测试linux系统可以在运行测试类的时候为测试类添加参数,-Dos.name=linux

  相关解决方案