SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>
来源:互联网 发布:淘宝店购物哪个软件好 编辑:程序博客网 时间:2024/05/22 06:07
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,指定监听的事件类
@Componentpublic class DemoListener implements ApplicationListener<DemoEvent> { @Override public void onApplicationEvent(DemoEvent event) { String msg = event.getMsg(); System.out.println("收到了DemoEvent的消息:" + msg); }}
- 发送事件,通过获取AbstractApplicationContext,执行publishEvent函数发布事件
@Componentpublic 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(); }}
0 0
- SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>
- SpringBoot学习-第三章 Spring高级应用-<Spring Boot 实战>
- SpringBoot学习-第四章 SpringMVC基础-<Spring Boot 实战>
- SpringBoot学习-第一章 新的Spring基础-<Spring Boot 实战>
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
- SpringBoot:spring boot mysql配置
- SpringBoot:spring boot log4j 配置
- SpringBoot学习笔记(2) Spring Boot的一些配置
- SpringBoot学习笔记(3) Spring Boot 运行原理,自动配置
- spring boot 学习(十)SpringBoot配置发送Email
- SpringBoot学习笔记(2) Spring Boot的一些配置
- SpringBoot学习笔记(3) Spring Boot 运行原理,自动配置
- SpringBoot学习笔记(2) Spring Boot的一些配置
- Spring Boot配置方式
- Springboot-启动 Spring Boot服务的方式
- Spring Boot自动配置实战
- Spring Boot 学习第二步 配置MySQL数据库+JPA
- Spring Boot 学习第二步 配置MySQL数据库+JPA
- 海康摄像头+web调用(官方开发包)+手把手全方位扫盲
- Ubuntu or Deepin 上安装 mysql 数据库
- Google App Engine技术架构资料大盘点
- hdu 4349 Xiao Ming's Hope (Lucas定理)
- 使用Python提取免费的ip代理
- SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>
- ThreadLocal在项目中的最佳实践
- 大型网站架构不得不考虑的10个问题
- mac版本Beyond Compare如何一直试用和拷贝的xcode升级问题
- Android 常用 adb 命令总结
- Redis系列(八):分布式锁服务
- 软件架构设计箴言理解
- 什么导致了Context泄露:Handler&内部类
- ZMQ.Socket API