码迷,mamicode.com
首页 > 编程语言 > 详细

springboot_Enable*

时间:2018-06-29 23:34:43      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:str   color   except   runtime   rar   throw   []   关闭   root   

注销掉SpringBootApplication

使用

@EnableAutoConfiguration
@ComponentScan
也能代替,其实最终生效的是@EnableConfigurationProperties
@EnableConfigurationProperties
@ComponentScan

开启异步
@Component
public class User {


    @Async
    public void show() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
@SpringBootApplication
@EnableAsync
public class Springboot6Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Springboot6Application.class, args);
        User bean = context.getBean(User.class);
        bean.show();
        System.out.println("---------------end-------------");
    }
}

查看详细的EnableAsync 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {

    
    AdviceMode mode() default AdviceMode.PROXY;

    int order() default Ordered.LOWEST_PRECEDENCE;

}

 

可以看到
@Import(AsyncConfigurationSelector.class)
这个类实现了
ImportSelector

这个接口,可以实现这个接口,重写这个方法,这里返回的一个数组,看我们会自动纳入的spring的管理中.

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{
                Dog.class.getName(),
                User.class.getName(),
                Role.class.getName(),
        };
    }
}

 

//@Import({Role.class,User.class,MyConfig.class})
//可以导入类和配置类
//类会被托管,如果是配置类,那么所有的bean都会被托管
//其中有两个特殊的接口
//1.ImportSelector这个接口的返回值的类名会被注册的到spring
//2.ImportBeanDefinitionRegistrar:这个可以自己手动注册

 

 

举个栗子

自己实现一个Enable*

定义一个注解,作用:标注的包的bean加载的时候都打印日志

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EchoLogImportBeanDefinitionRegistrar.class)
public @interface EnableEchoLog {

    String[] packages();
}

 

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class EchoLogImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {

        Map<String, Object> map = metadata.getAnnotationAttributes(EnableEchoLog.class.getName());
        String[] packagesArr = (String[])map.get("packages");
        List<String> packages = Arrays.asList(packagesArr);
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EchoLogBeanPostProcessor.class);
        builder.addPropertyValue("packages",packages);
        registry.registerBeanDefinition(EchoLogBeanPostProcessor.class.getName(),builder.getBeanDefinition() );
    }
}
package com.fz.song.one;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.List;


public class EchoLogBeanPostProcessor implements BeanPostProcessor {

    private List<String> packages;

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        String name = bean.getClass().getName();
        for (String pk:packages){
            if(name.startsWith(pk)){
                System.out.println("正在为您加载[" + pk+"]下的["+name+"]对象");
            }
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public List<String> getPackages() {
        return packages;
    }

    public void setPackages(List<String> packages) {
        this.packages = packages;
    }
}

 

这样就能模拟出效果,添加EnableEchoLog 注解的时候,就能开启一个功能,取消注解就能关闭功能


springboot_Enable*

标签:str   color   except   runtime   rar   throw   []   关闭   root   

原文地址:https://www.cnblogs.com/songfahzun/p/9245698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!