This article is reproduced from: still do not understand Spring common configuration? Ali is a look at how Daniel configuration
A, Bean's Scope
1 Introduction
- Scope describes how the Spring container is a new instance of the Bean.
- Singleton: only one instance of a Spring container.
- Prototype: Each call will create a new instance of Bean.
2. Requirements
Singleton and Prototype, respectively from the two Bean Spring container, it is judged whether the same
3. Example
①. The preparation of Singleton Bean
package com.eleven.scope1;
import org.springframework.stereotype.Service;
@Service // 表示当前类是Spring管理的一个Bean
// @Scope("singleton") 默认为Singleton,所以注释掉
public class DemoSingletonService {
}
②. Write Prototype of Bean
package com.eleven.scope1;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service // 表示当前类是Spring管理的一个Bean
@Scope("prototype") // 声明scope为prototype
public class DemoPrototypeService {
}
③. Configuration class
package com.eleven.scope1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration // 声明当前类是一个配置类
@ComponentScan("com.eleven.scope1") // 自动扫描包下面的所有配置
public class ScopeConfig {
}
④. Run
package com.eleven.scope1;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// 声明AnnotationConfigApplicationContext是Spring管理的一个Bean,将ScopeConfig注入进去
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
// 获取DemoSingletonService声明的Bean
DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
System.out.println("Singleton:s1和s2是否相等:" + s1.equals(s2));
System.out.println("Prototype:p1和p2是否相等:" + p1.equals(p2));
context.close();
}
}
5. Output
Singleton:s1和s2是否相等:true
Prototype:p1和p2是否相等:false
二、Profile
1 Introduction
Profile showing different configurations may be used in different environments.
2. Requirements
Profile by using annotations to configure the production / development environment.
3. Example
①. Examples Bean
package com.eleven.profile;
public class DemoBean {
private String content;
public DemoBean(String context) {
super();
this.content = context;
}
/** get/set方法 */
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
②. Profile Configuration
package com.eleven.profile;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration // 声明当前类是一个配置类
public class ProfileConfig {
@Bean // 表示当前类的返回值就是一个Bean
@Profile("dev")
public DemoBean devDemoBean() {
return new DemoBean("开发环境");
}
@Bean
@Profile("prod")
public DemoBean prodDemoBean() {
return new DemoBean("生产环境");
}
}
③. Run
package com.eleven.profile;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// 使用AnnotationConfigApplicationContext作为Spring的容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 通过设定Environment的ActiveProfiles来设定当前context的配置环境
context.getEnvironment().setActiveProfiles("prod");
// 先将活动的Profile设置为Prod
// context.getEnvironment().setActiveProfiles("dev"); // 表示开发环境
context.register(ProfileConfig.class);
// 注册Bean的配置类
context.refresh();
// 刷新容器
// 获得DemoBean声明的Bean
DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getContent());
context.close();
}
}
④. Output
生产环境
Third, the event (Application Event)
1 Introduction
Spring event provides support for messaging between Bean and Bean.
When a Bean finished with a task, I hope other Bean to be aware of and make the appropriate treatment, then, we need to let the other current event listener Bean Bean sent.
2. Requirements
- Custom events, integration ApplicationEvent.
- Define event listeners to realize ApplicationListener.
- Use container publishing events.
3. Example
①. Custom events
package com.eleven.event;
import org.springframework.context.ApplicationEvent;
public class DemoEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
private String msg;
/** get/set **/
public DemoEvent(Object source, String msg) {
super(source);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
②. Define event listeners
package com.eleven.event;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component // 将普通的pojo对象实例化到Spring容器中
public class DemoListener implements ApplicationListener<DemoEvent> {
// 实现了ApplicationListener接口,并指定监听事件的类型
@Override
public void onApplicationEvent(DemoEvent event) {
// 使用onApplicationEvent方法对消息进行接收处理
String msg = event.getMsg();
System.out.println("我(bean-demoListener)接收到了bean-demoPublisher发布的消息:" + msg);
}
}
③. Use containers publishing events
package com.eleven.event;
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;
// 注入ApplicationContext用来发布事件
public void publish(String msg) {
applicationContext.publishEvent(new DemoEvent(this, msg));
// 使用ApplicationContext的PublishEvent方法来发布
}
}
④. Configuration class
package com.eleven.event;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration // 声明当前是一个配置类
@ComponentScan("com.eleven.event") // 自动扫描包下面所以的配置
public class EventConfig {
}
⑤. Run
package com.eleven.event;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
// 将AnnotationConfigApplicationContext作为Spring的容器,并将参数配置进去
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
// 获得DemoPublisher的声明Bean
DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
// 发布消息
demoPublisher.publish("Hello Application Event");
context.close();
}
}
⑥. Output
I (bean-demoListener) received the message bean-demoPublisher published: Hello Application Event