首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >打印加载的所有Spring beans

打印加载的所有Spring beans
EN

Stack Overflow用户
提问于 2012-03-07 21:46:34
回答 5查看 105.5K关注 0票数 105

有没有办法打印启动时加载的所有spring bean?我使用的是Spring2.0。

EN

回答 5

Stack Overflow用户

发布于 2014-06-04 21:49:40

代码语言:javascript
复制
public class PrintBeans {
    @Autowired
    ApplicationContext applicationContext;

    public void printBeans() {
        System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames()));
    }
}
票数 70
EN

Stack Overflow用户

发布于 2015-03-08 04:28:27

打印所有bean名称及其类:

代码语言:javascript
复制
package com.javahash.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloWorldController {

    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping("/hello")
    public String hello(@RequestParam(value="key", required=false, defaultValue="World") String name, Model model) {

        String[] beanNames = applicationContext.getBeanDefinitionNames();

        for (String beanName : beanNames) {

            System.out.println(beanName + " : " + applicationContext.getBean(beanName).getClass().toString());
        }

        model.addAttribute("name", name);

        return "helloworld";
    }
}
票数 22
EN

Stack Overflow用户

发布于 2016-08-09 20:29:33

使用Spring Boot和执行器启动器

代码语言:javascript
复制
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

您可以检查端点/beans

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9602664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档