zoukankan      html  css  js  c++  java
  • Spring 注解驱动(一)基本使用规则

    Spring 注解驱动(一)基本使用规则

    Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html)

    一、基本使用

    @Configuration
    @ComponentScan(basePackages = "com.github.binarylei",
            excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}
    )
    @Lazy(false)
    public class AnnnotationConfig {
    
        // Scope 可取四个值:SCOPE_SINGLETON、SCOPE_PROTOTYPE、SCOPE_SESSION、SCOPE_REQUEST
        @Bean
        @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
        public User user() {
            return new User();
        }
    }
    

    启动测试:

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnnotationConfig.class);
        // AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // context.register(AnnnotationConfig.class);
        // context.refresh();
    
        User user = context.getBean(User.class);
    }
    

    二、@ComponentScan

    @Configuration
    @ComponentScan(basePackages = "com.github.binarylei",
            excludeFilters = {
                    @Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
                    @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {User.class}),
                    @Filter(type = FilterType.CUSTOM,classes = MyFilter.class)
            }, useDefaultFilters = false
    )
    public class AnnnotationConfig {
    }
    

    自定义的包扫描如下:

    public class MyFilter implements TypeFilter {
    
        /**
         * @param metadataReader        当前类的信息
         * @param metadataReaderFactory 获取其他类的信息
         */
        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
                throws IOException {
            // 1. 当前类的注解信息
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            // 2. 当前类的信息
            ClassMetadata classMetadata = metadataReader.getClassMetadata();
            // 3. 当前类的资源信息(类路径)
            Resource resource = metadataReader.getResource();
            return true;
        }
    }
    

    三、@Conditional

    @Bean
    @Conditional(MyCondition.class)
    public User user() {
        return new User();
    }
    
    // 条件装配
    public class MyCondition implements Condition {
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            // 1. IOC 容器
            ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
            // 2. 类加载器
            ClassLoader classLoader = context.getClassLoader();
            // 3. 环境变量
            Environment environment = context.getEnvironment();
            // 4. 可以向容器中注册 BeanDefinition
            BeanDefinitionRegistry registry = context.getRegistry();
            return environment.getProperty("os.name").contains("Windows");
        }
    }
    

    四、@Import

    给容器中注册组件有以下方式:

    1. @Bean
    2. 包扫描(@ComponentScan) + 注解(@Componet/@Repository/@Service/@Controller)
    3. @Import
      • @Import({User.class}) 导入单个组件
      • @Import({User.class, MyImportSelector.class}) MyImportSelector 批量导入组件
      • @Import({User.class, MyImportBeanDefinitionRegistrar.class}) MyImportBeanDefinitionRegistrar 批量导入组件
    4. FactoryBean
    @Import({User.class, MyImportSelector.class})
    @Import({User.class, MyImportBeanDefinitionRegistrar.class})
    public class AnnnotationConfig {
    }
    
    // 返回类名的全定限名称
    public class MyImportSelector implements ImportSelector {
    
        /**
         * @param importingClassMetadata 获取标注 @Import 注解的类所有注解信息(不仅仅是 @Import)
         */
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            // 不要返回 null,否则会报空指针异常
            return new String[]{User.class.getName()};
        }
    }
    
    // 使用 BeanDefinitionRegistry 注册
    public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    
        /**
         * @param importingClassMetadata 获取标注 @Import 注解的类所有注解信息(不仅仅是 @Import)
         * @param registry               向容器中注册 BeanDefinition
         */
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
                                            BeanDefinitionRegistry registry) {
    
            boolean definition1 = registry.containsBeanDefinition("red");
            boolean definition2 = registry.containsBeanDefinition("blue");
            if (definition1 && definition2) {
                RootBeanDefinition beanDefinition = new RootBeanDefinition(User.class);
                registry.registerBeanDefinition("user", beanDefinition);
            }
        }
    }
    

    五、Bean 的生命周期

    1. @Bean(initMethod = "init", destroyMethod = "destroy")
    2. 实现 InitializingBean, DisposableBean 接口

    每天用心记录一点点。内容也许不重要,但习惯很重要!

  • 相关阅读:
    vs2010配置驱动开发
    寒假训练 npuctf_2020_bad_guy(11/250)利用overlap与fastbin attack来篡改fd指针,从而通过stdout达到泄露libc
    寒假训练 [OGeek2019]bookmanager(10/250)
    寒假训练 npuctf_2020_level2(9/250)将heap分配到bss上,从而满足程序条件
    寒假训练 npuctf_2020_level2(8/250)修改ebp链来间接修改返回地址
    Windows XP源码跟踪
    寒假训练 houseoforange_hitcon_2016(7/250)
    寒假训练 jarvisoj_level6_x64(6/250)
    寒假训练 de1ctf_2019_weapon(5/250)
    glibc源码逆向——fwrite函数
  • 原文地址:https://www.cnblogs.com/binarylei/p/10203385.html
Copyright © 2011-2022 走看看