获取Spring中ApplicationContext.xml的Bean 博客分类: Spring SpringBeanFactoryApplicationContext
package org.sunnysolong.web.mvc.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.sunnysolong.web.mvc.service.UserService;
/**
* Title: mvcs<br>
* Description: Get Spring Application Context<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jul 21, 2011 3:21:49 PM <br>
* @author wangmeng
*/
public class Commision {
@SuppressWarnings("deprecation")
public static void main(String[] args){
Resource resource = new FileSystemResource("WebRoot/WEB-INF/config/applicationContext.xml");
BeanFactory oneFac = new XmlBeanFactory(resource);
UserService oneUs = (UserService) oneFac.getBean("userService");
System.out.println(oneUs.notice());
//the classpathContext.xml is in the classpath directory.
ClassPathResource cpResource = new ClassPathResource("classpathContext.xml");
BeanFactory twoFac = new XmlBeanFactory(cpResource);
UserService twoUs = (UserService) twoFac.getBean("userService");
twoUs.say();
ApplicationContext ctxSt = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/config/applicationContext.xml");
UserService threeUs = (UserService) ctxSt.getBean("userService");
System.out.println(threeUs.notice());
ApplicationContext ctxNd = new ClassPathXmlApplicationContext("file:WebRoot/WEB-INF/config/applicationContext.xml");
UserService fourUs = (UserService) ctxNd.getBean("userService");
fourUs.say();
}
}
推荐阅读
-
Spring ApplicationContext 中的对象bean的生命周期 博客分类: Spring applicationcontextspringbeanbeanfactory
-
获取Spring中ApplicationContext.xml的Bean 博客分类: Spring SpringBeanFactoryApplicationContext
-
Spring基本用法4——创建Bean的三种方式 博客分类: Spring Spring创建Bean的三种方式静态工厂方法实例工厂方法
-
Spring基本用法5——容器中Bean的生命周期 博客分类: Spring Spring容器Bean生命周期Spring基本用法
-
Spring基本用法6——协调作用域不同步的Bean 博客分类: Spring Spring作用域不同步具体类实现lookup方法
-
Spring基本用法3——Spring容器中Bean的相关属性 博客分类: Spring SpringBean属性集合注入
-
Spring中控制反转和依赖注入的深入理解 博客分类: Spring SpringIOCDI概念理解区分
-
spring中单例中获取新的bean实例
-
JSP 获取spring容器中bean的两种方法总结
-
解析Java中如何获取Spring中配置的bean