# springboot-config **Repository Path**: cbbnice/springboot-config ## Basic Information - **Project Name**: springboot-config - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-04-12 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 实验二 利用Spring boot的自动装配特性实现动态注册组件 院(系)名称:网络空间安全学院 专业班级: 17软卓2班 学号: 201741412219 姓名: 崔彬彬 实验题目: 实验二 利用Spring boot的自动装配特性实现动态注册组件 实验日期:2020/4/12 实验(上机)学时: 2 成绩: ## 一、实验目的 1、 掌握Spring Boot的自动配置原理; 2、 掌握Spring框架动态注册Bean的原理; 3、 掌握自动生成元数据文件。 4、 掌握spring框架的事件模型。 ## 二、实验环境 1、 JDK 1.8或更高版本 2、 Maven 3.6+ 3、 IntelliJ IDEA ## 三、实验任务 **1、 通过IntelliJ IDEA的Spring Initializr向导创建Spring Boot项目;** ![index1](image/1-1.JPG) ![index1](image/1-2.JPG) **2、 创建一个自定义的CommandLineRunner接口的实现类;** ``` @SuppressWarnings("unused") public class InitCommandLineRunner implements CommandLineRunner{ Environment environment; public InitCommandLineRunner(Environment environment){ this.environment=environment; } @Override public void run(String... args){ System.out.println("The random number is ".concat(Objects.requireNonNull(environment.getProperty("random.")))); } } ``` **3、 创建一个自定义的自动配置类;** ``` @Bean CommandLineRunner setCommandLineRunner(Environment environment){ return new InitCommandLineRunner(environment); } ``` **4、 创建spring.factories文件;** ![index1](image/4-1.JPG) ![index1](image/4.JPG) **5、 给自动配置类添加有效条件** ``` @Bean @ConditionalOnProperty(prefix = "bin.auto",name = "enable",havingValue = "false") CommandLineRunner setCommandLineRunner(Environment environment){ return new InitCommandLineRunner(environment); } ``` ![index1](image/5-1.JPG) ![index1](image/5-2.JPG) **6、 自定义的一个Bean,绑定属性值,并生成spring配置类的元数据文件** ![index1](image/6-1.JPG) ![index1](image/6-2.JPG) **7、 自定义的事件发布器** ``` @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME) ApplicationEventMulticaster InitApplicationEventMulticaster(ThreadPoolTaskExecutor taskExecutor){ SimpleApplicationEventMulticaster eventMulticaster=new SimpleApplicationEventMulticaster(); eventMulticaster.setTaskExecutor(taskExecutor); return eventMulticaster; } ``` **8、 自定义事件类;** ``` static class NoticeEvent extends ApplicationEvent{ private static final Logger logger= LoggerFactory.getLogger(NoticeEvent.class); //private static final Logger l private final String message; public NoticeEvent(String message){ super(message); this.message=message; logger.info("success!message:"+message); //logger.info("success!message:{}",message); } public String getMessage(){ return message; } } ``` **9、 自定义事件监听器** ``` @Component static class NoticeListener implements ApplicationListener{ private static final Logger logger= LoggerFactory.getLogger(NoticeListener.class); @Override public void onApplicationEvent(NoticeEvent noticeEvent){ logger.info("NoticeEvent success!Thread sleeping time is two second..."); try { Thread.sleep(2000); }catch (InterruptedException e){ e.printStackTrace(); } logger.info("NoticeEvent message is:"+noticeEvent.getMessage()); } } ``` **10、 编写一个测试用例,检查发布事件时,是否使用了多线程异步处理。** ``` @SpringBootTest class SpringbootConfigApplicationTests { @Autowired ApplicationEventMulticaster eventMulticaster; @Test void contextLoads() throws InterruptedException{ eventMulticaster.multicastEvent(new AutoConfig.NoticeEvent("1111")); eventMulticaster.multicastEvent(new AutoConfig.NoticeEvent("2222")); Thread.sleep(2100); } } ``` ![index1](image/10.JPG)