`
三里小龙
  • 浏览: 86016 次
  • 性别: Icon_minigender_1
  • 来自: 孝感
社区版块
存档分类
最新评论

获取Spring中ApplicationContext.xml的Bean

阅读更多

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();

}

}


0
1
分享到:
评论

相关推荐

    applicationContext.xml 详细配置

    ApplicationContext.xml 是 Spring 框架中用于配置应用程序的核心配置文件。通过该文件,可以定义 Bean、数据源、Session 工厂、 Hibernate 配置等相关信息,从而实现应用程序的自动装配和依赖注入。 一、XML 声明...

    applicationContext.xml详解

    ApplicationContext.xml文件是Spring应用程序中的核心配置文件,它用于定义和配置各种Bean,并管理Bean之间的依赖关系。通过配置ApplicationContext.xml文件,我们可以轻松地实现Spring应用程序的开发和维护。

    Spring中ApplicationContext加载机制

    &lt;param-value&gt;classpath:applicationContext-bean.xml,classpath:spring-jamon.xml ``` 配置完成之后,即可通过 WebApplicationContextUtils.getWebApplicationContext 方法在 Web 应用中获取 ApplicationContext...

    struts2+spring+hibernate整合示例

    c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts ...

    Spring入门.docx

    所有在xml里配置的Bean标签最后都会转换为BeanDefinition对象存放在beanFactory里的beanDefinitionMap中。实例化完成的Bean对象存放在singletonObjects当中。 FileSystemXmlApplicationContext是加载磁盘里具体位置...

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的入口。下面将详细介绍五种获取 WebApplicationContext 和 ...

    17 、各种spring项目建立1

    在 applicationContext.xml 文件中,我们可以配置 Spring bean 的相关信息。下面是 applicationContext.xml 文件的示例代码: ```xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.spring...

    spring技术入门相关源码

    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 System.out.println(java.util.Arrays....

    Spring的监听器ContextLoaderListener的作用

    这样,Spring 中的所有 bean 都由这个类来创建。 其次,ContextLoaderListener 负责部署 applicationContext 的 xml 文件。在 web.xml 文件中,如果不写任何参数配置信息,默认的路径是"/WEB-INF/...

    Spring选择题(含答案).doc

    4. 在 Spring 中,applicationContext.xml 文件可以配置 Bean 的作用域,例如,prototype 作用域表示每次请求 Bean 时都会创建新的实例。 根据代码,可以判断控制台输出的结果是: A. false true service1 和 ...

    Spring MVC开发配置文件 applicationContext

    Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext

    Spring项目主配置文件

    bean.xml或者applicationContext.xml都可以,在下载的Spring包中H:\spring-framework-4.1.0.RELEASE-dist\spring-framework-4.1.0.RELEASE\docs\spring-framework-reference\html\beans.html页面中去拷贝也行

    第一个spring IOC例子(maven)

    注: applicationContext.xml 配置bean信息所依赖的class路径要一致且大小写敏感,否则运行时会出现java.lang.NoClassDefFoundError (wrong name)。 https://blog.csdn.net/my_bkn/article/details/6875481

    Spring框架1

    1.Spring概述 3.编写配置文件注册bean 4.代码测试 1.Spring的配置文件名常用applicationContext.xml 2.Bean在创

    整合struts2和spring源代码(可以直接在tomcat中运行)

    注意:如果让spring来创建对象可以将action中的class属性中的值为spring 中bean 中的id值,如果想让struts来创建可以直接指定类 附加: 整合原理 : Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin...

    spring aop 实现源代码--xml and annotation(带lib包)

    Log before public abstract void com.savage.simplespring.bean.MessageSender.send(java.lang.String) by LogBeforeAdvice. Send Message[message] by http. 正如你所看到的,在执行MessageSender的send方法前先...

    面试知识点总结--Spring 应用框架技术.pdf

    可以在 applicationContext.xml 文件中加载一个 ResourceBundleMessageSource Bean,例如: &lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;value&gt;...

    实验一 Springioc基本操作.docx

    在 pom.xml 文件中,需要添加 Spring IOC 相关的依赖项,例如 spring-context、spring-core、spring-beans 和 spring-expression 等。 2. 配置 applicationContext.xml 文件:applicationContext.xml 文件是 Spring...

    Spring与Web环境集成.pdf

    在Spring框架中,ApplicationContext是应用程序的核心容器,它负责管理应用程序中的Bean对象。通常,ApplicationContext对象是通过new ClassPathXmlApplicationContext(spring配置文件)方式获取的,但是这种方式存在...

    asm5.0.jar

    Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/E:/cloudwave-core/src/main/webapp/WEB- INF/classes/...

Global site tag (gtag.js) - Google Analytics