技术:spring-4.3.9.RELEASE + java8
运行环境:IDEA 15.2 + jdk8 + windows 7
demo功能:提供一个获取spring 上下文的代码demo
在IndexController中获取IndexController的实例, 照理讲 获取的实例都是同一个(因为这个controller是单例),但是2方法不同。其余的正常获取
@RestController
@RequestMapping("/")
public class IndexController {
@Autowired
private ContextHolder contextHolder;
@Autowired
private ContextHolder1 contextHolder1;
@Autowired
private ContextHolder2 contextHolder2;
@RequestMapping("1")
public String get1() {
IndexController indexController = SpringContextUtil.getBean(IndexController.class);
//因为controller实例默认是单例的, 所以通过上下文获取的 index Controller实例和当前this 实例的hashcode 应该是一样的。所以相减等于0
return String.valueOf(this.hashCode() - indexController.hashCode());
}
@RequestMapping("2")
public String get2(HttpServletRequest request) {
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
IndexController indexController = ctx.getBean(IndexController.class);
//这种方法获取的不是当前的controller 实例, 待确认为啥
return String.valueOf(this.hashCode() - indexController.hashCode()) + " 这个需要确认获取的实例不是当前运行的controller实例";
}
@RequestMapping("3")
public String get3() {
IndexController indexController = contextHolder.getApplicationContext().getBean(IndexController.class);
//因为controller实例默认是单例的, 所以通过上下文获取的 index Controller实例和当前this 实例的hashcode 应该是一样的。所以相减等于0
return String.valueOf(this.hashCode() - indexController.hashCode());
}
@RequestMapping("4")
public String get4() {
IndexController indexController = contextHolder1.getApplicationContext().getBean(IndexController.class);
//因为controller实例默认是单例的, 所以通过上下文获取的 index Controller实例和当前this 实例的hashcode 应该是一样的。所以相减等于0
return String.valueOf(this.hashCode() - indexController.hashCode());
}
@RequestMapping("5")
public String get5() {
IndexController indexController = contextHolder2.getCtx().getBean(IndexController.class);
//因为controller实例默认是单例的, 所以通过上下文获取的 index Controller实例和当前this 实例的hashcode 应该是一样的。所以相减等于0
return String.valueOf(this.hashCode() - indexController.hashCode());
}
}
//第一种
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
}
@SuppressWarnings("unchecked")
public static T getBean(Class<?> clazz) throws BeansException {
return (T) applicationContext.getBean(clazz);
}
}
//第二种
@Component
public class ContextHolder extends ApplicationObjectSupport {
}
//第三种
@Component
public class ContextHolder1 extends WebApplicationObjectSupport {
}
//第四种
@Component
public class ContextHolder2 implements ApplicationContextAware {
private static ApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ContextHolder2.ctx = applicationContext;
}
public ApplicationContext getCtx() {
return ctx;
}
}
当然实现方法还有
//这两种是重新生成上下文信息, 很慢。通常用在测试
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
欢迎添加微信,互相学习↑↑↑ -_-
白老虎
programming is not only to solve problems, ways to think
grafana 级连 菜单 templating (variables) 配置
rocketmq 集群搭建 (2master + 2slave + 2namesrv)
AI 机器人 抓取 微信 聊天中的 百度网盘 分享地址和密码