SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值