java配置文件初始化_关于java应用如何通过spring的配置文件移动初始化对象配置...

关于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@}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值