spring不依赖注入得到实体bean

2023-07-31,,

如题,我们一般用spring的ioc,通过配置注入接口得到这个实现类,现在通过研究公司平台框架发现还有一种方法得到spring文件配置的bean方法,举个例子(注:这个ApplicationContext是自己定义的):

private static MyTestBean myTestBean = (MyTestBean) ApplicationContext.getInstance().getBizComponent("myTestBean");

配置文件:

<bean id="myTestBean" class="com.wxw.web.demo.MyTestBean"></bean>

通过自己定义的ApplicationContext的getBizComponent(String bean)方法得到实现类。

spring通过接口来注入实现了这个接口的实现类,但我们很多时候并不需要接口来测试开发,比如A业务类调用B业务类的方法,我们如果还是通过在A业务类里声明B业务类的接口,然后再注入这个接口,就略显麻烦。所以下面这个方法的出现就很好的解决了这个问题,还有这个方法可以直接启动spring容器,因为是容器类的单例模式,所以开发的时候可以直接在mian方法里运行,这样测试某个业务方法起来就方便很多。

项目工程文件图:

关键代码:

SpringContextLoad.java:

public class SpringContextLoad extends ContextLoaderListener{

@Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);//实际加载spring
        SpringContainer.instance = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
       
        Class type = SpringContainer.instance.getType("myTestBean");
        ApplicationContext.getInstance();
        Object obj = ApplicationContext.getInstance().getBizComponent("myTestBean");
    }
}

web启动spring加载ContextLoaderListener,改成自己定义的SPringContextLoad,然后通过WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());得到spring里的applicationContext

ApplicationContext.java

package com.wxw.platform.spring.impl;

public class ApplicationContext {
   
    /**
     * 容器启动锁,如果已有启动进程存在,则抛出异常。避免启动多个容器/循环依赖!
     */
    public static boolean startup_lock = false;
   
    private static ApplicationContext instance;
   
    private Contanier container;
   
    public static ApplicationContext getInstance(){
        if(instance == null){
            instance = new ApplicationContext();
        }
        return instance;
    }
   
    private ApplicationContext(){
        try {
            if (startup_lock) {
                throw new Exception("容器被重复初始化,可能是非法的调用所致");
            }
            startup_lock = true;
            String provider = "com.wxw.platform.spring.impl.SpringContainer";
            container = (Contanier) Class.forName(provider, true,
                    Thread.currentThread().getContextClassLoader()).newInstance();//其他方式获取容器实现
            container.start();
            startup_lock = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    public Object getBizComponent(Object object) {
        try {
            return container.getBizComponent(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

这个主要是想说明一点,一定要写成单例模式,这样的话,就不怕重复启动spring了。

这里想说明有一个很好用的方法:

String provider = "com.wxw.platform.spring.impl.SpringContainer";
            container = (Contanier) Class.forName(provider, true,
                    Thread.currentThread().getContextClassLoader()).newInstance();//其他方式获取容器实现

通过类加载得到这个接口的实现类

总结:还是那句话,如果想了解某个方法实现的原理,最好先自己理解了,然后,自己把这个方法用自己的方法实现起来,那样你会得到很多意想不到的东西。不要眼高手低,一步一个脚印。

下面是打包了的代码:

http://files.cnblogs.com/wxwall/springTestCase.zip

spring不依赖注入得到实体bean的相关教程结束。

《spring不依赖注入得到实体bean.doc》

下载本文的Word格式文档,以方便收藏与打印。