Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Which bean are started by Springboot applications?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what bean the Springboot application starts, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Overview

We will explore techniques related to getting all the bean managed by spring in the container. What's the use of this? It is mainly used for troubleshooting. It is usually a problem that one of the bean we created did not start. After all, you always encounter all kinds of bug at work. There is no harm in knowing something in advance.

2. IoC container

Bean is the foundation of spring-managed applications, and all bean resides in the IOC container, which manages their lifecycles.

We can get a list of all the bean in the container in two ways:

Use the ListableBeanFactory interface

Use Spring Boot Actuator

3. Use the ListableBeanFactory interface

The ListableBeanFactory interface provides the getBeanDefinitionNames () method, which returns the names of all bean defined in this factory. You can find a list of all known subinterfaces and their implementation classes in the official documentation. Let's look at how to get all the bean in this way.

Step 1: create a Controller

@ Controller public class FooController {@ Autowired private FooService fooService; @ RequestMapping (value= "/ displayallbeans") public String getHeaderAndBody (Map model) {model.put ("header", fooService.getHeader ()); model.put ("message", fooService.getBody ()); return "displayallbeans";}}

This Controller depends on another FooService.

Step 2: create a Service

@ Service public class FooService {public String getHeader () {return "Display All Beans";} public String getBody () {return "Show all cases of beans";}}

Notice that we have created two different bean here:

FooController

FooService

Here you use the applicationContext object and call its getBeanDefinitionNames () method, which returns all the bean in the applicationContext container:

Step 3: set up the SpringBootApplication startup class

@ SpringBootApplication public class DemoApplication {private static ApplicationContext applicationContext; public static void main (String [] args) {applicationContext = SpringApplication.run (DemoApplication.class, args); displayAllBeans ();} public static void displayAllBeans () {String [] allBeanNames = applicationContext.getBeanDefinitionNames (); for (String beanName: allBeanNames) {System.out.println (beanName);}

Step 4: test printing

This will print all the bean in the applicationContext container:

Note that in addition to the bean we defined, it records all other bean in the container. For the sake of clarity, we omit them here because there are many of them.

4. Use Spring Boot Actuator

Spring Boot Actuator provides endpoints for monitoring application statistics. Let's take a look at this:

Step 1: add dependencies

Org.springframework.boot spring-boot-starter-actuator

Step 2: modify application.properties

Management.endpoints.web.exposure.include=*

Add the above code to the properties file.

Step 3: use the publish endpoint to view

Because the Actuator here is not configured, the display is rather messy.

The above is exactly what bean is launched by Springboot applications. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report