Getting Spring Application Context

本文探讨了在Spring应用中如何全局地获取ApplicationContext实例。通过实现ApplicationContextAware接口或使用@Autowired注解,可以方便地让需要访问容器的类获得ApplicationContext。对于容器外部的对象,则建议采用GoF单例模式来管理Spring容器。

Is there a way to statically/globally request a copy of the ApplicationContext in a Spring application?

Assuming the main class starts up and initializes the application context, does it need to pass that down through the call stack to any classes that need it, or is there a way for a class to ask for the previously created context? (Which I assume has to be a singleton?)

If the object that needs access to the container is a bean in the container, just implement the BeanFactoryAware or ApplicationContextAware interfaces.

If an object outside the container needs access to the container, I’ve used a standard GoF singleton pattern for the spring container. That way, you only have one singleton in your application, the rest are all singleton beans in the container.

You can implement ApplicationContextAware or just use @Autowired:

public class SpringBean {
  @Autowired
  private ApplicationContext appContext;
}

SpringBean will have ApplicationContext injected, within which this bean is instantiated. For example if you have web application with a pretty standard contexts hierarchy:

main application context <- (child) MVC context

and SpringBean is declared within main context, it will have main context injected; otherwise, if it’s declared within MVC context, it will have MVC context injected.

RSocket是一种用于响应式通信的网络协议,它基于Reactive Streams规范。Spring Boot提供了对RSocket的良好支持,使得开发者能够轻松地在Spring Boot应用中集成RSocket服务器和客户端。 要开始使用Spring Boot创建RSocket服务器,你需要添加相应的依赖项到你的`pom.xml`文件中。对于Maven项目,你可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-rsocket</artifactId> </dependency> ``` 一旦添加了依赖项,你就可以在你的Spring Boot应用中配置RSocket服务器。以下是一个简单的示例,展示了如何创建一个基本的RSocket服务器: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.messaging.rsocket.RSocket; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; import reactor.core.publisher.Mono; @SpringBootApplication public class RSocketServerApplication { public static void main(String[] args) { SpringApplication.run(RSocketServerApplication.class, args); } @Bean public RSocket rsocketServer() { return RSocketMessageHandler.create() .route("greet", String.class, String.class) .handle((payload, metadata) -> Mono.just("Hello " + payload)) .get(); } } ``` 在这个例子中,我们定义了一个名为`greet`的路由,当接收到字符串消息时,它将返回一个问候语。我们还创建了一个名为`rsocketServer`的bean,它使用了`RSocketMessageHandler`来处理传入的消息。 为了运行这个示例,你需要确保你的Spring Boot应用的主类被正确配置为启动类。然后,你可以使用以下命令来启动你的Spring Boot应用: ```bash mvn spring-boot:run ``` 这将启动一个监听在默认端口(通常是7000)上的RSocket服务器。你可以使用任何支持RSocket的客户端来连接并测试这个服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值