小编给大家分享一下怎么获取所有spring管理的bean,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
bean是基于spring应用的基础,所有bean都驻留在ioc容器中,由容器负责管理bean生命周期
有两种方式可以获取容器中的bean:
- 使用ListableBeanFactory接口
- 使用Spring Boot Actuator
ListableBeanFactory接口提供了getBeanDefinitionNames() 方法,能够返回所有定义bean的名称。该接口被所有factory实现,负责预加载bean定义去枚举所有bean实例。官方文档提供所有其子接口及其实现。
下面示例使用Spring Boot 构建:
首先,我们创建一些spring bean,先定义简单的Controller FooController:
@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";
}
}
该Controller依赖另一个spring Bean FooService:
@Service
public class FooService {
public String getHeader() {
return "Display All Beans";
}
public String getBody() {
return "This is a sample application that displays all beans "
+ "in Spring IoC container using ListableBeanFactory interface "
+ "and Spring Boot Actuators.";
}
}
我们创建了两个不同的bean:
1.fooController
2.fooService
现在我们运行该应用。使用applicationContext 对象调用其 getBeanDefinitionNames() 方法,负责返回applicationContext上下文中所有bean。
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
displayAllBeans();
}
public static void displayAllBeans() {
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
for(String beanName : allBeanNames) {
System.out.println(beanName);
}
}
}
会输出applicationContext上下文中所有bean:
fooController
fooService
//other beans
需注意除了我们定义的bean外,它还将打印容器中所有其他bean。为了清晰起见,这里省略了很多。
Spring Boot Actuator提供了用于监视应用程序统计信息的端点(endpoint)。除了/beans,还包括很多其他端点,官方文档有详细说明。
现在我们访问url: http//
:/beans,如果没有指定其他独立管理端口,我们使用缺省端口,结果会返回json,包括容器所有定义的bean信息:
[
{
"context": "application:8080",
"parent": null,
"beans": [
{
"bean": "fooController",
"aliases": [],
"scope": "singleton",
"type": "com.baeldung.displayallbeans.controller.FooController",
"resource": "file [E:/Workspace/tutorials-master/spring-boot/target
/classes/com/baeldung/displayallbeans/controller/FooController.class]",
"dependencies": [
"fooService"
]
},
{
"bean": "fooService",
"aliases": [],
"scope": "singleton",
"type": "com.baeldung.displayallbeans.service.FooService",
"resource": "file [E:/Workspace/tutorials-master/spring-boot/target/
classes/com/baeldung/displayallbeans/service/FooService.class]",
"dependencies": []
},
// ...other beans
]
}
]
当然,结果同样包括很多其他的bean,为了简单起见,这里没有列出。
上面介绍了使用ListableBeanFactory 接口和 Spring Boot Actuators 返回spring 容器中所有定义的bean信息。
Spring容器默认情况下,当服务启动时,解析配置文件,实例化文件中的所有类。
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
MyService myService1 = (MyService) ctx.getBean("myService");
代码如下:
1.第一步,创建java project,引入spring.jar
2.创建spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
3.创建接口MyService,只需要一个测试方法save
4.创建实现类MyServiceImpl,控制台输出一句话
5.创建一个自己的解析类MyClassPathXmlApplicationContext
主要是构造方法中的两步
// 装载实例化bean
private Map<String, Object> beanMap = new HashMap<String, Object>();
// 装载配置文件的属性和值
private List<MyBeans> beanlist = new ArrayList<MyBeans>();
public MyClassPathXmlApplicationContext(String filename) {
//第一步,解析spring配置文件
readXml(filename);
//第二步,通过反射,实例化所有注入bean
initBeans();
}
/**
* 通过反射机制,初始化配置文件中的bean
*/
private void initBeans() {
for (MyBeans bean : beanlist) {
try {
if (bean.getClassName() != null && !"".equals(bean.getClassName())) {
beanMap.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 解析配置文件,把解析后的bean设置到实体中,并保持到list
*
* @param filename
*/
private void readXml(String filename) {
SAXReader reader = new SAXReader();
Document doc = null;
URL xmlpath = this.getClass().getClassLoader().getResource(filename);
try {
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");
doc = reader.read(xmlpath);
XPath xpath = doc.createXPath("//ns:beans//ns:bean");// 创建//ns:beans//ns:bean查询路径
xpath.setNamespaceURIs(nsMap);// 设置命名空间
List<Element> eles = xpath.selectNodes(doc);// 取得文档下所有节点
for (Element element : eles) {
String id = element.attributeValue("id");
String cn = element.attributeValue("class");
//自定义实体bean,保存配置文件中id和class
MyBeans beans = new MyBeans(id, cn);
beanlist.add(beans);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String beanId) {
return beanMap.get(beanId);
}
6.实体类
package com.mooing.service;
public class MyBeans {
private String id;
private String className;
public MyBeans(String id, String className) {
this.id = id;
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
7.测试
MyClassPathXmlApplicationContext ctx = new MyClassPathXmlApplicationContext("spring.xml");
MyService myService = (MyService) ctx.getBean("myService");
myService.save();
以上是“怎么获取所有spring管理的bean”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。