@Servicepublic class BeanFactoryHelper implements BeanFactoryAware {
private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public static Object getBean(String beanName){
if(beanFactory == null){
throw new NullPointerException("BeanFactory is null!");
}
return beanFactory.getBean(beanName); } }
@Service
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName){
if(applicationContext == null){
throw new NullPointerException("ApplicationContext is null!");
}
return applicationContext.getBean(beanName);
}
}
@Test
public void test() throws SQLException {
//通过从classpath中加载spring-mybatis.xml实现bean的获取
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
IUserService userService = (IUserService) context.getBean("userService");
User user = new User();
user.setName("test");
user.setAge(20);
userService.addUser(user);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有