spring 获取 上下文 几种方法 demo

2018-03-03

101

0

技术:spring-4.3.9.RELEASE + java8

运行环境:IDEA 15.2 + jdk8 + windows 7

demo功能:提供一个获取spring 上下文的代码demo

源代码

https://gitee.com/youlixishi/demo-world/tree/master/src/spring/spring-context-get-demo

从上下文获取这个controller实例

在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());
    }
}

4种方法-测试正确的

//第一种
@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");

测试

依次访问

http://localhost:8080/1-是预期的

http://localhost:8080/2-不是预期的-原因你猜猜

http://localhost:8080/3-是预期的

http://localhost:8080/4-是预期的

http://localhost:8080/5-是预期的

如果返回0,则正确

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

youlixishi

自己选择的路, 风雨兼程