配置文件调用相对简单
1.新建 spring上下文
public class SpringContextUtilC implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
SpringContextUtilC.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
2.在application.xml的上方添加bean
<beans:bean id="SpringContextUtilC" class="com.sdstc.maxvalue.itsm.business.utils.SpringContextUtilC"/>
3.获取
SpringContextUtilC.getBean("aaa");
对于调用注解的bean
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.setParent(SpringContextUtilC.getApplicationContext()); //设置spring上下文 否则会找不到sessionfactory
ctx.scan("com.sdstc.maxvalue.cmdb.assetmanage.asset.*.*");
ctx.refresh();
IAssetService service =(IAssetService) ctx.getBean("assetServiceImpl");