在做一个需要从static代码块中操作数据库的方式。如果重新建立一个数据库连接有点low,所以从spring中进行获取。网上搜索到几种方式。需要new几个对象。因为已经有一个了。再new一个。不合理。所以本人使用了如下的方式进行获取bean。
所以使用了实现ApplicationContextAware来实现
@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中进行配置。
这样调用getBean方法就可以获取bean对象了。
具体的在static代码块中进行查询数据。如下。
有一个中间类。下面分为两段代码。
/**
* 使用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");
方法二:通过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")
方法三:继承自抽象类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的实现方式。