Getting Spring Application Context

Getting Spring Application Context

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.

Spring get current ApplicationContext

Simply inject it..

@Autowired
private ApplicationContext appContext;

or implement this interface: ApplicationContextAware

What are the ways to get ApplicationContext object in Spring?

You can also use annotation based configuration

@Configuration
public class Config {

@Bean
public Bean1 bean1() {
return new Bean1();
}

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
}
}

How to retrieve the Application Context in Spring Boot 2

tl;dr: You don't need the context; there's a better way.

ApplicationContextAware is an artifact from much older versions of Spring, before many of the now-standard features were available. In modern Spring, if you need the ApplicationContext, just inject it like any other bean. However, you almost certainly shouldn't interact with it directly, especially for getBean, which should be replaced with injecting whatever you were getting.

In general, when you need a Spring bean, you should declare it as a constructor parameter. (If you have multiple constructors, you need to annotate one with @Autowired, but if there's only a single constructor, Spring is smart enough to know to use it.) If you're using Lombok, you can use @Value to automatically write the constructor, and Groovy and Kotlin have similar features.

In the specific case of Micrometer, which you're showing here, it is not conventional to declare individual metrics as beans because they are fine-grained tools intended to apply to specific code paths. (Some services might have 10 separate metrics to track various possible scenarios.) Instead, you inject the MeterRegistry and select the counters or other metrics that you need as part of your constructor. Here, your controller class should look like this. (I've eliminated the duplicate AtomicLong, but you could add it back in as you showed if there's a specific reason you need it.)

@RestController
public class GreetingController {

private static final Logger LOG = LoggerFactory.getLogger(GreetingController.class);

private static final String template = "Hello, %s!";

private final Counter counter;

public GreetingController(MeterRegistry meterRegistry) {
counter = meterRegistry.counter("my.counter");
}

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {

counter.increment();
long count = (long) counter.count();

return new Greeting(count, String.format(template, name));
}
}

java spring: how to access Application Context of another Spring Application

Spring boot application doesn't expose beans to be consumed by other applications, so you can't really do that.
Spring boot application by design is "self-contained" and beans are not "exposed services" but internal building blocks of the application that implement the business logic of your app.

This is a design of all spring applications.

In general, Spring is something that "lives" inside the JVM however you run two different JVM processes so you'll have to do some kind of inter-process communication between them.

Also, it's a good idea to "control" what functions are exposed to be used by other applications and how (protocol, parameters, results, etc).

Luckily spring boot application allows to easily establish such control by using controllers of spring-MVC. So basically you mark a bean with a couple of annotations (bean = a class managed by spring and allows injection of other spring beans) and it gets exposed (by some URL) automatically. This is the closest "spring-ish" way to achieve what you want.

Of course, if you need a GRPC style of inter-process communication you definitely can create GRPC services.

There are also other ways that can be used in different use cases (for example for management you might opt for JMX) or some kind of message bus for asynchronous communication, but this is a topic too broad to be discussed in one question. Bottom line, this is not "automatic" and you have to choose the style of IPC that works for your case, but definitely, you need to have something and you can't just "call" the beans as long as you have two different applications that run on two different JVMs (and it doesn't really matter whether they're running on the same host or not).

Access spring application context from everywhere

Use @ImportResource to load the beans from the configuration files.

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ImportResource.html

Accessing Spring ApplicationContext inside of BeanDefinitionRegistryPostProcessor

I had a similar task and declaring BDRPP as a Bean worked:

public class MyCustomBdrpp implements BeanDefinitionRegistryPostProcessor {

private ApplicationContext context;

private MyCostomBdrpp(ApplicationContext context) {
this.context = context;
}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//foo
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
//bar
}
}

And then:

@Configuration
class MyConfig {

@Bean
public MyCustomBdrpp myBdrpp(@Autowired ApplicationContext context) {
return new MyCustomBdrpp(context);
}

}

But I need to say that I am creating context manually:

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MyConfig.class);
context.refresh();


Related Topics



Leave a reply



Submit