SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>
2017-02-14 17:38
996 查看
Bean的Scope -demo.springboot.scope
Singleton:一个Spring容器只有一个,默认选项Prototype:每次调用创建一个新的Bean
Requst:给每个requst创建一个新的Bean
Session:给每个session创建一个新的Bean
@Service @Scope("prototype") public class DemoPrototypeService { }
SpringEL -demo.springboot.el
@PropertySource:在Java中引入properties/xml文件@Value:注入值到对应的属性中,来源可以是文件/url/静态方法等,通过表达式访问
@PropertySource("classpath:el/test.properties") public class ElConfig { @Value("Love U") String normal; @Value("#{systemProperties['os.name']}") String osName; @Value("#{T(java.lang.Math).random()*100.00}") double randomNumber; @Value("#{demoReadFileService.addition}") String addition; @Value("classpath:el/test.txt") Resource testFile; @Value("http://www.baidu.com") Resource testUrl; @Value("${project.name}") String projectName; }
Bean的工作流程 -demo.springboot.prepost
构造函数-》init方法-》destroy方法* 可以通过@Bean(initMethod=” “,destroyMethod=” “)指定Bean的生存周期方法
* 可以通过@PostConstruct@PreDestroy,在Bean内部预先注解好生存周期方法
//Bean方式 public class BeanWayService { public BeanWayService() {} public void init() {} public void destroy() {} } //JSR方式 public class JSR250WayService { public JSR250WayService() {} @PostConstruct public void init() {} @PreDestroy public void destroy() {} } //实际使用 public class ProPostConfig { @Bean(initMethod = "init", destroyMethod = "destroy") BeanWayService beanWayService() { return new BeanWayService(); } @Bean JSR250WayService jsr250WayService() { return new JSR250WayService(); } }
Profile 不同环境的配置切换
在JVM或者application.yaml里设置 spring.profiles.active=xxx ,spring里会根据Profile名称匹配* 使用@Profile(…)注解需要切换的@Bean,
* 设置application-dev.yaml/application-test.yaml等不同配置
public class ProfileConfig { @Bean @Profile("dev") public String devBean() { return "获取dev"; } @Bean @Profile("test") public String testBean() { return "获取test"; } }
事件
Bean与Bean的消息通信:定义事件-》定义监听-》发布事件* 自定义事件,继承ApplicationEvent(还有一些内置的Event)
public class DemoEvent extends ApplicationEvent { private String msg; public DemoEvent(Object source, String msg) { super(source); this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
定义监听器,实现ApplicationListener,指定监听的事件类
@Component public class DemoListener implements ApplicationListener<DemoEvent> { @Override public void onApplicationEvent(DemoEvent event) { String msg = event.getMsg(); System.out.println("收到了DemoEvent的消息:" + msg); } }
发送事件,通过获取AbstractApplicationContext,执行publishEvent函数发布事件
@Component public class DemoPublisher { @Autowired AbstractApplicationContext context; public void pulish() { context.publishEvent(new DemoEvent(this, "欢迎欢迎")); } }
测试
@Configuration @ComponentScan("demo.springboot.event") public class EventConfig { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); DemoPublisher publisher = context.getBean(DemoPublisher.class); publisher.pulish(); context.close(); } }
相关文章推荐
- SpringBoot学习-第一章 新的Spring基础-<Spring Boot 实战>
- SpringBoot学习-第三章 Spring高级应用-<Spring Boot 实战>
- SpringBoot学习-第四章 SpringMVC基础-<Spring Boot 实战>
- spring-boot-starter-logging logback常用配置之<appender>标签详解
- spring-boot-started-logging logback常用配置之<filter>标签详解
- SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置
- spring-boot-starter-logging logback常用配置之<appender>标签详解
- spring-boot-started-logging logback常用配置之<filter>标签详解
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- SpringBoot中Tomcat配置(学习SpringBoot实战)
- Spring入门学习——配置文件<beans>属性配置记录
- spring boot框架学习学前掌握之重要注解(1)-sprng的java配置方式
- spring boot框架学习学前掌握之重要注解(1)-java配置方式
- SpringBoot学习-(四)SpringBoot配置Druid连接池
- spring-boot-starter-logging logback配置之<configuration><logger>标签详解
- 【系统学习SpringBoot】SpringBoot读取Property配置
- SpringBoot属性配置实战(多种方式)
- spring-boot-starter-logging logback配置之<configuration><logger>标签详解
- springboot学习笔记-2 一些常用的配置以及整合mybatis
- (转)Spring boot——logback.xml 配置详解(四)<filter>