spring配置文件加载后applicationContext 为null

今天遇到一个问题:

通过ApplicationContext获取spring容器中的bean时,报ApplicationContext为null,代码如下:

public class ServiceBeanUtils implements ApplicationContextAware {

    protected static ApplicationContext staticContext;
	
	protected ServiceBeanUtils() {
		
    }

    public void setApplicationContext(ApplicationContext context) throws BeansException {
        staticContext = context;
    }

    public static Object getBean(String strBeanName) {
    	try {
    		return staticContext.getBean(strBeanName);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    }

    public static IBaseService getServiceBean(String strBeanName){
    	return (IBaseService)getBean(strBeanName);
    }

}

 查找原因:xml文件的加载顺序不正确,ServiceBeanUtils还没有加载, ApplicationContext还没有初始化,而服务启动时就有个类通过调用ApplicationContext去取bean进行初始化了

解决方案:先加载ServiceBeanUtils类,去初始化ApplicationContext,然后再加载要调用ApplicationContext的类去初始化此类

如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<bean class="com.hw.hwsafe.platform.util.ServiceBeanUtils" />

	
	<bean id="loginLogService" class="com.hw.hwsafe.platform.service.impl.LoginLogServiceImpl"/>
	

</beans>

 

 

转载于:https://my.oschina.net/starforever/blog/489626

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在使用IDEA加载Spring框架时,通常需要进行以下几个步骤: 1. 确保你的项目中已经添加了Spring的相关依赖。可以在项目的pom.xml文件中添加Spring的依赖项,或者手动将Spring的jar包添加到项目的类路径中。 2. 创建一个Spring配置文件,通常是一个XML文件,用于定义Spring的bean和其他配置信息。在配置文件中,你可以定义bean的名称、类、属性等信息。你可以根据自己的需求来配置Spring的各种功能,比如数据源、事务管理等。 3. 在你的代码中使用Spring的注解或者配置文件来声明和使用bean。你可以使用注解如@Autowired来自动注入依赖的bean,或者使用配置文件中的<bean>标签来定义和获取bean。 4. 在IDEA中配置Spring加载方式。你可以在IDEA的配置中指定Spring配置文件的位置,以便在项目启动时加载Spring的配置信息。你可以在项目的运行配置中设置启动类和Spring配置文件的路径。 需要注意的是,你提供的引用内容中包含了两种不同的加载方式。引用\[1\]中使用了HibernateTemplate来访问数据库,而引用\[3\]中使用了SessionFactory来获取数据库会话。这两种方式都是Spring框架中常用的访问数据库的方式,你可以根据自己的需求选择其中一种方式来加载Spring。另外,引用\[2\]中的异常信息表明在加载配置文件时出现了文件不存在的错误,请确保配置文件的路径和名称正确,并且文件存在于指定的位置。 总结起来,加载Spring框架通常需要添加依赖、创建配置文件、使用注解或配置文件声明和使用bean,并在IDEA中配置加载方式。希望这些信息对你有帮助。 #### 引用[.reference_title] - *1* *3* [idea项目添加spring](https://blog.csdn.net/C_J33/article/details/79297577)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【maven-spring】IDEA加载spring配置文件使用ApplicationContext找不到xml文件的问题](https://blog.csdn.net/weixin_44914334/article/details/119782088)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值