spring 使用xml、注解的形式装配Bean
我们可以通过注解@Autowired 很简单方便获取bean,如下:
@Autowired
private Test test;
虽然这种方法很简单方便,但是有些特殊场景用不了。
借助ConfigurableApplicationContext获取bean
还可以用 springboot启动类 SpringApplication.run 返回的 ConfigurableApplicationContext对象的getBean 获取想要的bean, ConfigurableApplicationContext 继承自 ApplicationContext。
通过启动类ConfigurableApplicationContext 对象geBean, 如果不在在启动类中用起来就有点小麻烦,虽然可以做到。
借助ApplicationContextAware获取bean
spring加载配置文件时,会自动调用ApplicationContextAware中的setApplicationContext
我们可以实现一个工具类继承ApplicationContextAware并重写setApplicationContext 获取applicationContext 保存到工具类中,通过注解@Component 或其他 将工具类bean交给spring管理创建。这以后我们就能够方便在我们想要的地方通过applicationContext 获取想要的bean了。
具体实现代码
package com.example.applicationcontextutil_demo.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
/**
* 获取静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
}
下面是一个测试demo源码
目录结构
pom.xml 依赖就一个web包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.properties 端口被占用了随便选了个 8091
server.port=8091
TestServiceImpl
package com.example.applicationcontextutil_demo.service;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl {
public String hello(){
return "hello world";
}
}
TestController
package com.example.applicationcontextutil_demo.controller;
import com.example.applicationcontextutil_demo.service.TestServiceImpl;
import com.example.applicationcontextutil_demo.util.ApplicationContextUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
//注意getBean(String s) bean在spring中对象名小写开头
return ((TestServiceImpl)ApplicationContextUtil.getBean("testServiceImpl")).hello();
}
@RequestMapping("/test2")
public String test2(){
return ApplicationContextUtil.getBean(TestServiceImpl.class).hello();
}
}
测试
浏览器输入 127.0.0.1:8091/test
浏览器输入 127.0.0.1:8091/test2
源码
链接:https://pan.baidu.com/s/1P20wO2Gj6X2rM17DMIu6fw
提取码:di9o