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();
}
}
结果会输出监听的信息+传入的字符串信息