栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SPRING注解驱动开发(二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SPRING注解驱动开发(二)

@Conditional的使用

 

 

 

 

 

 @Conditional({
            MacOsCondition.class
    })
    @Bean("person2") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
    public Person person2(){
        return new Person("wendi","18");
    }
  @Bean("person1") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
    @Conditional({WindowsOsCondition.class})
    public Person person(){
        return new Person("xushuai","18");
    }
package com.example.paymentdemo.config;

import com.example.paymentdemo.entity.MacOsCondition;
import com.example.paymentdemo.entity.Person;
import com.example.paymentdemo.entity.WindowsOsCondition;
import com.example.paymentdemo.main.MyimportBeanDefinitionRegistrar;
import com.example.paymentdemo.main.MyimportSelector;
import org.springframework.context.annotation.*;


@Configuration
//当标注在类上的时候,满足当前条件,这个类中标注的bean才会生效
@Conditional({WindowsOsCondition.class})
//快速导入组件 id默认是组件的全类名
//可以多个
@import({Person.class, MyimportSelector.class, MyimportBeanDefinitionRegistrar.class})
//@import(Person.class)
public class Main{
    //Specifies the name of the scope to use for the annotated component/bean.
    //Defaults to an empty string ("") which implies SCOPE_SINGLETON.
    // 默认是单例的 单例的请求ioc容器会调用方法创建对象放到ioc容器中,以后每次获取就是直接从容器中拿
    //ioc在容器启动的时候不会创建新对象,多实例每次调用都会获取个新的对象
    
    //ConfigurableBeanFactory.SCOPE_PROTOTYPE, ConfigurableBeanFactory.SCOPE_SINGLETON, 可以配置为多例
    // org.springframework.web.context.WebApplicationContext.SCOPE_REQUEST, web环境下可以有request 和session
    // org.springframework.web.context.WebApplicationContext.SCOPE_SESSION, value
    //还有个全局的global
    @Scope(value = "prototype")
    @Lazy //懒加载
    @Bean("person1") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
    @Conditional({WindowsOsCondition.class})
    public Person person(){
        return new Person("xushuai","18");
    }

    
    @Conditional({
            MacOsCondition.class
    })
    @Bean("person2") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
    public Person person2(){
        return new Person("wendi","18");
    }

    


}
package com.example.paymentdemo.entity;

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.context.annotation.Conditional;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypemetadata;


public class WindowsOsCondition implements Condition {

    //conditionContext: 判断条件能使用的上下文环境
    //AnnotatedTypeMetsdata:注释信息
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypemetadata metadata) {
        //获取当前ioc使用的工厂bean
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();

        //获取类加载器
        ClassLoader classLoader = context.getClassLoader();

        //获取环境
        Environment environment = context.getEnvironment();

        //获取Bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();

        //判断容器中bean的注册情况
        boolean person2 = registry.containsBeanDefinition("person2");
        //获取操作系统
        String property = environment.getProperty("os.name");
        if (property.contains("windows")){
            return true;
        }
        return false;
    }
}
package com.example.paymentdemo.main;

import com.example.paymentdemo.entity.Person;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;


public class ConditionTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.example.paymentdemo");
        String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
        //获取所有bean
        Map beansOfType = applicationContext.getBeansOfType(Person.class);
        //获取ioc的运行环境
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("os.name");
        System.err.println(property);

    }
}

@import的使用

 

 

 

package com.example.paymentdemo.main;

import org.springframework.context.annotation.importSelector;
import org.springframework.core.type.Annotationmetadata;

import java.util.function.Predicate;


public class MyimportSelector implements importSelector {
    //返回值就是要导入到容器中的组件全类名
    //Annotationmetadata:当前标注@Impoert注解的类的所有注解信息(其他注解)
    @Override
    public String[] selectimports(Annotationmetadata importingClassmetadata) {

        //把全类名返回
        //不能返回null值
        return new String[]{"com.example.paymentdemo.entity.Person","com.example.paymentdemo.entity.WindowsOsConfig"};
    }

    @Override
    public Predicate getExclusionFilter() {
        return importSelector.super.getExclusionFilter();
    }
}

 

package com.example.paymentdemo.main;

import com.example.paymentdemo.entity.Person;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.importBeanDefinitionRegistrar;
import org.springframework.core.type.Annotationmetadata;


public class MyimportBeanDefinitionRegistrar implements importBeanDefinitionRegistrar {
    

    @Override
    public void registerBeanDefinitions(Annotationmetadata importingClassmetadata, BeanDefinitionRegistry registry) {
        boolean person = registry.containsBeanDefinition("person");
        if (person){
            //注册时需要一个beanDefinition的类型
            //指定bean的定义信息,bean的类型等信息
            RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
            registry.registerBeanDefinition("person1",beanDefinition);
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号