JAVAEE顛覆者,SpringBoot實戰一書學習小記(Profile,applicationEvent)
阿新 • • 發佈:2019-01-24
Profile
概念
profile為在不同環境下使用不通的配置提供了支援(開發環境下的配置和生產環境下的配置肯定不同的,例如資料庫的配置)。
首先建立一個bean
package com.cn.sola.bean; public class DemoBean { private String content; public DemoBean(String content){ super(); this.content = content; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
一個config類
package com.cn.sola.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import com.cn.sola.bean.DemoBean; @Configuration @ComponentScan("com.cn.sola.service") public class ProfileConfig { @Bean @Profile("dev")//profile為dev時例項化devDemoBean public DemoBean devDemoBean(){ return new DemoBean("from development profile"); } @Bean @Profile("prod")//profile為prod時例項化proDemoBean public DemoBean proDemoBean(){ return new DemoBean("from production profile"); } }
測試類
package com.cn.sola; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.test.context.junit4.SpringRunner; import com.cn.sola.bean.DemoBean; import com.cn.sola.config.ProfileConfig; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootShiZhanApplicationTests { @Test public void contextLoads() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //設定 context.getEnvironment().setActiveProfiles("prod"); context.register(ProfileConfig.class);//然後註冊bean配置類,不然會報bean未定義的錯誤 context.refresh();//重新整理容器 DemoBean demoBean = context.getBean(DemoBean.class); System.out.println(demoBean.getContent()); context.close(); } }
結果就是如果設定prod就會輸出from production profile
結果就是如果設定prod就會輸出from development profile
事件 Application Event
概念
Spring的事件為Bean與Bean之間的訊息通訊提供了支援。當一個Bean處理完一個任務之後,希望另外一個Bean知道並能做相應的處理,這時我們就需要讓另一個Bean監聽當前Bean所傳送的事件。
Spring 的事件需要遵循如下流程:
1.自定義事件,整合ApplicationEvent。
2.定義事件監聽器,實現ApplicationListener。
3.使用容器釋出事件。
首先建立一個bean
package com.cn.sola.bean;
import org.springframework.context.ApplicationEvent;
public class DemoEvent extends ApplicationEvent {
/**
*
*/
private static final long serialVersionUID = 1L;
private String msg;
public DemoEvent(Object source, String msg) {
super(source);
// TODO Auto-generated constructor stub
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
建立一個監聽類
package com.cn.sola.bean;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener implements ApplicationListener<DemoEvent>{//實現ApplicationListener介面,並指定監聽的事件型別
@Override
public void onApplicationEvent(DemoEvent event) {//使用onApplicationEvent方法對小心進行接受處理
// TODO Auto-generated method stub
String msg = event.getMsg();
System.out.println("我(Bean-demoListener)接收到了bean-demoPublisher釋出的訊息:"+msg);
}
}
建立一個事件釋出類
package com.cn.sola.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class DemoPublisher {
@Autowired
ApplicationContext applicationcontext;//注入ApplicationContext用來發不是事件
public void publish(String msg){
applicationcontext.publishEvent(new DemoEvent(this,msg));//使用Application的publishEvent方法來發布
}
}
配置類
package com.cn.sola.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.cn.sola.bean")
public class EventConfig {
}
執行
package com.cn.sola;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import com.cn.sola.bean.DemoPublisher;
import com.cn.sola.config.EventConfig;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootShiZhanApplicationTests {
@Test
public void contextLoads() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
DemoPublisher bean = context.getBean(DemoPublisher.class);
bean.publish("hello applicationEvent");
context.close();
}
}
結果會輸出監聽的資訊+傳入的字串資訊