`
yuxiangtong
  • 浏览: 6363 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

Spring测试

阅读更多
好几天都在做spring源码分析,初步分析
package cn.com.nuoter.playbar.groupon;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;

import cn.com.nuoter.playbar.groupon.service.GrouponAllService;

public class TestSpring {
	public static void main(String[] args) throws Exception {
		StringBuffer buffer=new StringBuffer();
	       ApplicationContext ctx = new ClassPathXmlApplicationContext(
	              "/conf/spring/applicationContext*.xml");
	       GrouponAllService service = (GrouponAllService) ctx.getBean("groupService"); //spring beanId  必须用接口类进行强转和接收
	       buffer.append("是否存在此bean:").append(ctx.containsBean("groupService")).append("\n");
	       buffer.append("此bean是否单例:").append(ctx.isSingleton("groupService")).append("\n");
	       buffer.append("根据bean的别名判断是否是目标类类型:").append(ctx.isTypeMatch("groupService", GrouponAllService.class));
	       GrouponAllService impl= (GrouponAllService)ctx.getBean("groupService",GrouponAllService.class);
	       aa();
           System.out.println(buffer.toString());
	    }
	
	public static void aa(){
	    ClassPathResource res = new ClassPathResource("/conf/spring/applicationContext-groupon.xml");
	    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	    System.out.println("此配置文件个共配置了"+reader.loadBeanDefinitions(res)+"类文件");
	    //GrouponAllService service = (GrouponAllService)factory.getBean("groupService"); //spring beanId  必须用接口类进行强转和接收
	    System.out.println("是否存在此bean:"+factory.containsBean("groupService"));
	    

	    System.out.println("-----------------------------------------------------------");
	}

}

输出如下:


各位有什么建议,请指教。。。刚开始研究
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics