当时做铂金网校的网站维护,遇到了一个读取Spring配置文件,取的ApplicationContext时,出现了NullPointerException。
使用ApplicationContextAware进行读取Spring配置文件取的对象。
具体代码如下:
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; //Spring应用上下文环境
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
* @param name bean注册名
* @param requiredType 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
完成这个类之后,我们可以在Action中进行调用,当然也可以在jsp中进行调用。
//不用再加载springContext.xml文件,因为在web.xml中配置了,在程序中启动是就有了.
StudentManagersm=(StudentManagerImpl)SpringContextUtil.getBean("studentManager");
通过这句话来进行使用,读取你自己配置SpringBean就可以了。
注意:
一定要在Spring的配置文件applicationContext.xml中加上:
<bean id="SpringContextUtil " class="项目中applicationContext的类路径比如:com.ptlearning.student.SpringContextUtil " singleton="true" />
当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关推荐
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
主要介绍了读取spring配置文件的方法,需要的朋友可以参考下
我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。
NULL 博文链接:https://konghaibo.iteye.com/blog/430146
此工具自动生成非注解时Spring整合MyBatis时需要的一些配置文件,对于使用较新版本的spring,可以忽视生成的dao的配置文件,建议采用spring的扫描方式。生成的GeneratorConfigXML修改jdbc驱动位置后可直接用使用...
spring读取properties配置文件的key值。
在程序中读取Spring 配置文件,通过Spring框架获得Bean,完成相应的操作 在上面是我们的开发流程,我们第一步一般都是导入所需jar包,但由于刚开始时根本不知道该在什么时候用到哪些jar包,所以这里事先不导入jar包...
Spring源码分析:配置文件读取流程 - ImportNew
NULL 博文链接:https://songwensheng.iteye.com/blog/629578
主要介绍了Spring读取配置文件属性实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,
NULL 博文链接:https://jiachenmianyu.iteye.com/blog/1745042
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
Spring Boot读取配置文件常用方式
本资源对spring-boot中的核心配置文件进行详细介绍,具体内容参考博客:http://blog.csdn.net/pengjunlee/article/details/79640361,包括:核心配置文件介绍,读取配置,动态加载配置等。
实践方志鹏博客搭建Springcloud+RabbitMQ+Config-client+config-server Eureka-server的微服务架构,通过/bus/refresh接口触发所有config-client自动从config-server重新读取配置文件。SpringCloud和SpringBoot版本...
Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
osgi多个bundle之间读取同一配置文件,需要单独添加一个用来读取配置文件的bundle
NULL 博文链接:https://java--hhf.iteye.com/blog/2171034
主要介绍了详解Spring-boot中读取config配置文件的两种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下