java获取spring的bean

/**

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

* @author xiongzy

*

*/

public final class BeanLocator {

private static final Logger logger = Logger.getLogger(BeanLocator.class);

/**

* 单实例.

*/

private static BeanLocator instance = null;

/**

* 缺省配置文件名称.

*/

private static final String DEFAULT_CONFIGURATION_FILENAME = "spring/applicationContext.xml";

/**

* 加载配置文件名.

*/

private static String configurationFileName = null;

/**

* spring环境.

*/

private ApplicationContext applicationContext = null;

/**

* 单例模式.

* @return 接口

*/

public static BeanLocator getInstance() {

if (instance == null) {

// 同步控制代码, 防止初始化多次.

synchronized(logger) {

if (instance == null) {

instance = new BeanLocator();

}

}

}

return instance;

}

/**

* 设置配置文件名.

* @param fileName 配置文件名

*/

public static void setConfigurationFileName(String fileName) {

configurationFileName = fileName;

}

/**

* 私有构造.

*/

private BeanLocator() {

if (configurationFileName == null || configurationFileName.length() == 0) {

configurationFileName = DEFAULT_CONFIGURATION_FILENAME;

}

// 得到spring框架bean环境

try{

applicationContext = new ClassPathXmlApplicationContext(configurationFileName);

}catch(Exception e){

logger.error("初始化spring配置文件时发生异常:" + e.getMessage(), e);

throw new RuntimeException("初始化spring配置文件时发生异常:" + e.getMessage(), e);

}

}

/**

* spring getBean 接口.

* @param beanName 接口名称

* @return 接口

*/

public Object getBean(String beanName) {

return applicationContext.getBean(beanName);

}

public static void main(String[] args) {

CityInfoService cityInfoService = (CityInfoService)BeanLocator.getInstance().getBean("cityInfoService");

System.out.println(cityInfoService);

}

}

java获取spring的bean

时间: 2024-12-19 04:57:15

java获取spring的bean的相关文章

普通java类获取spring容器bean的方法

很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有些好的东西,拿到用就好了. 这里是多种方式中的一种. 通过实现ApplicationContextAware获取bean.这里有个问题,就是,如果spring容器没有启动完成的时候,不能通过这个方法获取,因为这样,会报空指针,因为 private static ApplicationContext

static关键字,引发的spring普通类获取spring的bean的思考

在c++和java中static关键字用于修饰静态成员变量和成员函数 举例一个普通的javabean class AA { int a; static int b; geta/seta;//此处省略getset getb/setb; } 如果创建了一个对象AA, AA a =new AA(); 这个时候只会在内存中给这个对象分配四个字节,也就是a变量所占的字节数,因为static申明的全局变量在全局区中,是所有这个类的对象共有的,例如: a.setB(10); AA b =new A(); Sy

JAVA获取Spring上下文

1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 public static WebApplicationContext springContext; public void contextDestroyed(ServletContextEvent event) { //springContext = null; } /** * 获取spring上下

获取Spring容器Bean对象工具类

在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;

获取Spring管理bean对象

package com.sinosoft.base.util; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.Applica

获取Spring的Bean

直接这样就可以获取bean,不会有重复生成的问题.(这里的环境是使用Spring开发RestController) WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean(beanID); 如果是下面这样,Spring配置初始化一次,执行下面代码的时候会再初始化一次,这样就会有两个dataSource,可以在Druid的监控页面上查看具体情况. ApplicationCont

java之Spring装配Bean(手动装配、自动装配、注解装配)

在上一篇控制反转中我们看到了依靠一个Bean文件来实现对代码的控制,可谓十分便捷,再也不用去实例化对象了,2333~~~ 1.手动装配 1 <bean id="todo" class="com.eco.daoimp.Usertodo1"></bean> 2 3 <!--定义Userservice类内部接口的引用(userdao)指向具体的实现类对象(Usertodo1) --> 4 <bean id="userse

获取Spring Bean工具类SpringContextUtil

有时候需要在非Spring环境获取Spring的Bean import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import o

获取spring的ApplicationContext几种方式【转】

转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 代码: 1 ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 2 ac.getBean(&qu