关于java项目(非web项目)如何通过spring配置文件初始化生成对象,示例代码如下:
package com.**.base;@b@@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@import org.springframework.context.support.AbstractXmlApplicationContext;@b@import org.springframework.context.support.ClassPathXmlApplicationContext;@b@@b@/**@b@ * 类描述@b@ * @author nijun@b@ * @version 版本创建于 2015-6-3@b@ */@b@@b@public class SpringBeansMgr{@b@ @b@ protected static String[] configLocations;@b@ private static AbstractXmlApplicationContext ctx = null;@b@ protected Logger log = LoggerFactory.getLogger(this.getClass());@b@ @b@ static{@b@ init();@b@ }@b@ @b@ public static void init(){@b@ if(getConfigLocations() == null){@b@ setConfigLocations(new String[]{"classpath*:applicationContext-*.xml"@b@ });@b@ }@b@ try{@b@ ctx = new ClassPathXmlApplicationContext(getConfigLocations());@b@ }catch(Exception e){@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ public Object getBo(String beanName){@b@ return ctx.getBean(beanName);@b@ }@b@ @b@ public static Object getBean(String beanName){@b@ return ctx.getBean(beanName);@b@ }@b@ @b@@b@ public static String[] getConfigLocations() {@b@ return configLocations;@b@ }@b@@b@ public static void setConfigLocations(String[] _configLocations) {@b@ configLocations = _configLocations;@b@ }@b@@b@}