在作一个须要从static代码块中操做数据库的方式。若是从新创建一个数据库链接有点low,因此从spring中进行获取。网上搜索到几种方式。须要new几个对象。由于已经有一个了。再new一个。不合理。因此本人使用了以下的方式进行获取bean。java
因此使用了实现ApplicationContextAware来实现web
@Service public class SpringContextHolder implements ApplicationContextAware { // ServletContextListener private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextHolder.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String beanName) { return applicationContext.getBean(beanName); } public static <T>T getBean(String beanName , Class<T>clazz) { return applicationContext.getBean(beanName , clazz); } }
本人使用的是注解方式。只须要在类上面加上一个@Service 就能够。不用在xml中进行配置。spring
这样调用getBean方法就能够获取bean对象了。数据库
具体的在static代码块中进行查询数据。以下。app
有一个中间类。下面分为两段代码。ide
/** * 使用static方法获取 spring 中bean对象 * * Created by abing on 2015/11/6. */ public class CommonBeanUtils { // public static Object getBean(String name){ Object object = SpringContextHolder.getBean(name); return object; } }
static { PlatformLogger.message("============================================"); TUserService tUserService = (TUserService) CommonBeanUtils.getBean("userService"); List<TUser> list = tUserService.getUserById("1"); PlatformLogger.message("============================================"); for (TUser tUser : list) { PlatformLogger.message(tUser.getId() + " " + tUser.getName() + " " + tUser.getPassword()); } PlatformLogger.message("============================================"); }
这样就能够在static代码块中查询数据库了。工具
经过在网上搜索,发现还有几种方式能获取bean对象。下面列出几种方式来。测试
方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId"); ui
方法二:经过Spring提供的工具类获取ApplicationContext对象
代码:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId")spa
方法三:继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,能够方便的获取到ApplicationContext。
Spring初始化时,会经过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport
说明:相似上面方法,调用getWebApplicationContext()获取WebApplicationContext
方法四:继承自抽象类WebApplicationObjectSupport
说明:相似上面方法,调用getWebApplicationContext()获取WebApplicationContext
还有一种方式是beanFactoryAware
本身测试了一下确实可行。下面贴出代码。
@Service public class HolderBeanFactoryAware implements BeanFactoryAware { private static BeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { HolderBeanFactoryAware.beanFactory = beanFactory; } public static Object getBean(String beanName) { return beanFactory.getBean(beanName); } public static <T>T getBean(String beanName , Class<T>clazz) { return beanFactory.getBean(beanName , clazz); } }
本身操做数据库的地方
/** * 测试BeanFactoryAware来获取bean对象的例子 */ public static void testBeanFactoryAware(){ TUserService tUserService = (TUserService) HolderBeanFactoryAware.getBean("userService"); List<TUser> list = tUserService.getUserById("1"); PlatformLogger.message("============================================"); for (TUser tUser : list) { PlatformLogger.message(tUser.getId() + " " + tUser.getName() + " " + tUser.getPassword()); } PlatformLogger.message("============================================"); }
获取bean的方式 常见的方式为上面的五种方式。还有下面一种实现beanFactoryAware的实现方式。