spring读取配置文件,且获取bean实例
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
/**
* 实例化容器测试类
* */
public class Test {
public static void main(String[] args){
//方式一:在CLASSPATH路径下获取XMLBeanFactory实例
ClassPathResource res = new ClassPathResource("container.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
HelloBean hellobean = (HelloBean)factory.getBean("helloBean");
hellobean.sayHelloWorld();
//方式二:指定绝对路径建ApplicatinContext实例
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\My_Struts_Cvs8\\springioc\\src\\container.xml");
BeanFactory factory2 = (BeanFactory) context;
HelloBean hellobean2 = (HelloBean)factory2.getBean("helloBean");
hellobean2.sayHelloWorld();
//方式三:通过ClassPathXmlApplicationContext创建BeanFactory实例
ClassPathXmlApplicationContext context3 = new ClassPathXmlApplicationContext("container.xml");
BeanFactory factory3 = (BeanFactory) context3;
HelloBean hellobean3 = (HelloBean)factory3.getBean("helloBean");
hellobean3.sayHelloWorld();
}
最新文章
- Hibernate 错题分析
- 如何使用ArcGIS Online的地图数据作为底图
- mysql查询语句中用户变量的使用
- TopShelf&;Quartz.Net实现多任务的值守
- Effective Java 36 Consistently use the Override annotation
- git一些常用设置
- CRM 2016 自动保存 Save event arguments
- C++ Code_animateCtrl
- Properties 转换成Map
- POJ 2481 Cows
- SQL开发中容易忽视的一些小地方(六)
- iOS UICollectionView 长按移动cell
- MPSOC之6——开发流程linux编译
- instrument(2)
- 针对特定网站scrapy爬虫的性能优化
- Fiddler抓包2-只抓APP的请求
- dependencies与dependencyManagement的区别
- python---九九乘法表代码
- win7下安装curl
- 2D旋转和3D旋转