美文网首页软件测试学习之路
SSM框架中如何通过ApplicationContext获取Sp

SSM框架中如何通过ApplicationContext获取Sp

作者: 乘风破浪的姐姐 | 来源:发表于2018-05-08 13:43 被阅读3次

    在SSM的Web项目中,因为使用Spring的IOC后,不再需要new对象。如果要获取对象,则使用getBean的方式获取对象。那么如何通过getBean获取对象呢?

    想要获取到ApplicationContext对象,然后通过该对象得到Spring容器启动时注入的Bean对象。可以创建一个工具类SpringContextsUtil ,来实现Spring中的ApplicationContextAware接口。
    这样,当Spring容器启动后,即配置文件web.xml中注入bean。Spring会自动调用setApplicationContext方法。此时我们就可以获取到Spring context。然后使用这个上下文环境对象得到Spring容器中的Bean。

    package com.sc.utils;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
     * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
     *
     */
    public class SpringContextUtils implements ApplicationContextAware {
        private static ApplicationContext applicationContext;
    
        /**
         * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
         */
        public void setApplicationContext(ApplicationContext applicationContext) {
            SpringContextUtils.applicationContext = applicationContext; // NOSONAR
        }
    
        /**
         * 取得存储在静态变量中的ApplicationContext.
         */
        public static ApplicationContext getApplicationContext() {
            checkApplicationContext();
            return applicationContext;
        }
    
        /**
         * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
         */
        @SuppressWarnings("unchecked")
        public static <T> T getBean(String name) {
            checkApplicationContext();
            return (T) applicationContext.getBean(name);
        }
    
        /**
         * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
         */
        @SuppressWarnings("unchecked")
        public static <T> T getBean(Class<T> clazz) {
            checkApplicationContext();
            return (T) applicationContext.getBean(clazz);
        }
    
        /**
         * 清除applicationContext静态变量.
         */
        public static void cleanApplicationContext() {
            applicationContext = null;
        }
    
        private static void checkApplicationContext() {
            if (applicationContext == null) {
                throw new IllegalStateException("applicaitonContext未注入");
            }
        }
    }
    

    此时,如果要使用该类加载springXML的配置,需要在该Spring的配置文件中加入,同时需要将该配置文件加入到web.xml中:

       <!-- 此文件为spring配置文件的扫描 -->
        <bean class="com.sc.utils.SpringContextUtils" lazy-init="false"/>
    

    在web项目中的web.xml中要配置加载Spring容器的Listener。初始化Spring容器,让Spring容器随Web应用的启动而自动启动。
    所以需要在web.xml加上以下的信息:

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    OK,在项目中即可通过这个SpringContextUtils调用getBean()方法得到Spring容器中的对象。
    例如,想要获取数据库的映射bean:TestCaseMapper.class,然后查询该表中的数据加入到list中。

     @DataProvider(name = "db")
        public Iterator<Object[]> parameterIntTestProvider() {
            List<Object[]> dataProvider = new ArrayList<Object[]>();
            TestCaseMapper testCaseMapper = SpringContextUtils.getBean(TestCaseMapper.class);
            TestCaseCriteria c = new TestCaseCriteria();
            c.createCriteria().andRunEqualTo("Y");
            List<TestCase> list = testCaseMapper.selectByExampleWithBLOBs(c);
            for (TestCase TestCase : list) {
                dataProvider.add(new Object[] { TestCase });
            }
            return dataProvider.iterator();
        }
    

    相关文章

      网友评论

        本文标题:SSM框架中如何通过ApplicationContext获取Sp

        本文链接:https://www.haomeiwen.com/subject/oxwirftx.html