Spring 加载bean
2015-01-06 17:30
120 查看
1:常用方式:通过配置文件,在启动时加载配置文件,即可把配置文件里的bean加载;
2:直接指定需要加载的配置文件,如下方式,即获得了名字为“crawldataSource”的bean,
private DataSource crawldataSource;
public MyCrawler() {
ClassPathXmlApplicationContext ctxss = new ClassPathXmlApplicationContext("applicationContext-crawldataSource.xml");
BeanFactory ctx = (BeanFactory) ctxss;
crawldataSource = (DataSource) ctx.getBean("crawldataSource");
}
3:
public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("E:/myeclipse/workspace/zystnew/WebRoot/WEB-INF/config/spring/applicationContext*.xml");
XXXService ds = (XXXService) ac.getBean("xxxService");
}
2:直接指定需要加载的配置文件,如下方式,即获得了名字为“crawldataSource”的bean,
private DataSource crawldataSource;
public MyCrawler() {
ClassPathXmlApplicationContext ctxss = new ClassPathXmlApplicationContext("applicationContext-crawldataSource.xml");
BeanFactory ctx = (BeanFactory) ctxss;
crawldataSource = (DataSource) ctx.getBean("crawldataSource");
}
3:
public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("E:/myeclipse/workspace/zystnew/WebRoot/WEB-INF/config/spring/applicationContext*.xml");
XXXService ds = (XXXService) ac.getBean("xxxService");
}
相关文章推荐
- 快速上手Spring--3. 加载Bean的配置文件
- 快速上手Spring--3. 加载Bean的配置文件
- spring加载多个bean配置文件案例
- 5.0:Spring-bean的加载
- Spring在加载后,怎么获取bean且已经是注入好了对象
- Spring配置文件加载时出现Bean property 'newscontentDAO' is not writable or has an invalid setter method错误的解决
- Spring BeanPostProcessor与动态加载数据源配置
- 纯java实现spring时加载bean
- spring心得1--spring入门介绍@bean的加载初讲@第一个spring项目helloWord
- Spring bean 延迟加载
- Spring配置文件加载时出现Bean property 'newscontentDAO' is not writable or has an invalid setter method错误的解决[00原创]
- spring加载xml文件 读取bean时遇到的一个问题
- 关于spring加载applicationContext初始化bean顺序的问题
- Spring 容器从加载文件到创建一个完整Bean的工作流程
- spring 加载xml文件或bean的方法
- 快速上手Spring--3. 加载Bean的配置文件
- web.xml中filter代码中加载spring中定义的bean
- spring中bean的作用域与懒加载及初始化方法和销毁方法以及spring容器的正常关闭
- CloudStack采用spring加载bean(cloud-framework-spring-module模块)
- spring注解bean加载顺序问题(一)