`
fengshujuan
  • 浏览: 160344 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

在spring容器中注册过的类中,不能出现AppContext.getBean的原因

    博客分类:
  • java
阅读更多

问题描述

在代码中,取得一个在 spring 容器中注册的类有两种方法:

1) AppContext.getBean

2) applicationContext-Service.xml 中配置好类之间引用关系,在对应的类代码中加入 get set 方法。

如果在 spring 容器中注册过的类中,出现 AppContext.getBean 的代码,例如:

public class Fsj1{

         public Fsj1(){

                   System.out.println("111");

         }

}

public class Fsj2 {

         Fsj1 fsj1 = (Fsj1) AppContext.getBean("service1");

         public Fsj2(){

                   System.out.println("222");

         }

}

applicationContext-service.xml 中配置如下:

<bean id="service1" class="cn.ac.iscas.share.cus.service.Fsj1"/>

<bean id="service2" class="cn.ac.iscas.share.cus.service.Fsj2"/>

那么在程序启动的时候就会 报错:

    java.lang.NullPointerException

         at cn.ac.intec.entity.orm.AppContext.getBean(AppContext.java:146)

问题分析

      我们来看一下 AppContext 类代码:

public class AppContext{

        private static BeanFactory factory = null;

public static Object getBean(String name)  {

             return factory.getBean(name);

         }

}

空指针的错误正是由于 factory null 引起的,这是因为在程序启动时, BeanFactory 尚未对所有的对象创建完毕,也就是说 factory 实例还没有初始化完毕。在这个过程中又去调用 factory.getBean 当然会出现空指针错误。

当程序启动完毕后, factory 就初始化完毕了,这时候在程序的运行中(最常见的就是在 action 中)调用 AppContext.getBean 就不会出错了。

 

问题解决

对于两个都在 spring 中注册的类,互相之间的依赖关系应该在配置文件中写好,即使是循环依赖, spring 也可以很好的解决这个问题。例如:

public class Fsj1 {

         Fsj2 service2;

         public Fsj1(){

                   System.out.println("111");

         }

         public Fsj2 getService2() {

                   return service2;

         }

         public void setService2(Fsj2 service2) {

                   this. service2= service2;

         }

}

public class Fsj2 {

         Fsj1 service1;

         public Fsj2(){

                   System.out.println("222");

         }

         public Fsj1 getService1() {

                   return service1;

         }

         public void setService1(Fsj1 service1) {

                   this.service1 = service1;

         }

}

applicationContext-service.xml 中配置如下:

<bean id="service1" class="cn.ac.iscas.share.cus.service.Fsj1">

        <property name="service2" ref="service2" />

  </bean>

<bean id="service2" class="cn.ac.iscas.share.cus.service.Fsj2">

         <property name="service1" ref="service1" />

</bean>

分享到:
评论

相关推荐

    Spring的getBean和JdbcTemplate

    NULL 博文链接:https://summerbell.iteye.com/blog/364702

    加载spring配置文件,提供getBean接口.

    加载spring配置文件,提供getBean接口.

    spring.xls

    * IOC:spring容器控制对象的生命周期:前提条件:在spring容器中的bean必须是单例的 * 创建 * 方式 * 利用默认的构造函数,如果没有默认的构造函数,会报错 * 利用静态工厂方法 * 利用实例工厂方法 * 时机 *...

    maven相关资料

    Spring中ClassPathXmlApplicationContext类的简单使用 Posted on 2011-06-22 17:08 xcp 阅读(14689) 评论(0) 编辑 收藏 所属分类: Spring 一、简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例...

    利用spring的ApplicationContext在程序中唤醒quartz的job

    NULL 博文链接:https://dolphin-ygj.iteye.com/blog/368874

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    处理ssh组合框架中如何用getBean获取实体

    处理ssh组合框架中如何用getBean获取实体 在ssh中不能使用练习时候用的方法 初始化 applicationCon..文件 所以使用该方法也可以获取 getBean方法!

    在web容器(WebApplicationContext)中获取spring中的bean

    Spring把Bean放在这个容器中,普通的类在需要的时候,直接用getBean()方法取出

    spring技术入门相关源码

    //输出spring容器 System.out.println(ctx); //打印加载的bean名称 System.out.println(java.util.Arrays.toString(ctx.getBeanDefinitionNames())); /*MyBean bean = (MyBean)ctx.getBean("myBean"); //...

    ssh(structs,spring,hibernate)框架中的上传下载

     文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...

    SpringContextUtils

    java工具类, 用于不方便注入的地方获取spring bean. 根据名称,类型,注解.

    spring+springmvc+mybatis的整合

    但是有一些部分自己没有能完成,主要是如何从spring容器里取出ApplicationContext,这个我的实现比较low,看了看讲义,才OK的。 我的实现: [java] view plain copy WebApplicationContext acc = ...

    Spring getBean流程图1

    getBean开始Bean 是否 不为null并且args为null调用getObjectForBeanInstance方法停止调用AbstractBeanFa

    获取spring管理的bean

    以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入&lt;bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /&gt; 即可使用

    spring+hibernate+osworkflow

    spring+hibernate+osworkflow,服务器为Tomcat6.0,数据库为Mysql 把viewlivegraph2.jsp改为viewlivegraph.jsp. 其中把wf的获得修改为 ApplicationContext cxt = WebApplicationContextUtils.get...

    struts2驱动包

    信息: The listener "org.springframework.web.context.ContextLoaderListener" is already configured for this context. The duplicate definition has been ignored. log4j:WARN No appenders could be found for...

    spring static静态方法里面注入bean空指针解决

    该项目中注入jedisPool时使用了getBean()注入,然而怎么跑都是NullPoint,一翻扣头发后得已解决。 解决方法1 使用@Autowired的方式注入 必须在类上加@Component。我是用的这种 @Component public class ...

    自定义标签中@Autowired的属性为null

    自定义标签中@Autowired的属性...1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加&lt;bean id="" class="com............SpringContext"&gt; 3.使用SpingContext.getBean("bean名");获取

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    memo-jdbc-spring-app:一个Spring的JDBC案例

    使用getBean方法获取容器中的对象 注解方式依赖注入 调整包和目录结构,将其变成一个maven项目 编写pom文件。导入spring依赖。依赖配置见spring.io官网 在resource目录下创建application-context.xml文件,在其中...

Global site tag (gtag.js) - Google Analytics