`
youyu4
  • 浏览: 424928 次
社区版块
存档分类
最新评论

BeanFactory和ApplicationContext的区别

 
阅读更多

BeanFactory和ApplicationContext的区别

 

描述

 

BeanFactory:

是Spring里面最低层的接口,提供了最简单的容器的功能,只提供了实例化对象和拿对象的功能;

 

ApplicationContext:

应用上下文,继承BeanFactory接口,它是Spring的一各更高级的容器,提供了更多的有用的功能;

1) 国际化(MessageSource)

2) 访问资源,如URL和文件(ResourceLoader)

3) 载入多个(有继承关系)上下文 ,使得每一个上下文都专注于一个特定的层次,比如应用的web层  

4) 消息发送、响应机制(ApplicationEventPublisher)

5) AOP(拦截器)

 

 

 

两者装载bean的区别

 

BeanFactory:

BeanFactory在启动的时候不会去实例化Bean,中有从容器中拿Bean的时候才会去实例化;

 

ApplicationContext:

ApplicationContext在启动的时候就把所有的Bean全部实例化了。它还可以为Bean配置lazy-init=true来让Bean延迟实例化; 

 

 

 

我们该用BeanFactory还是ApplicationContent

 

延迟实例化的优点:(BeanFactory

应用启动的时候占用资源很少;对资源要求较高的应用,比较有优势; 

 

不延迟实例化的优点: (ApplicationContext

1. 所有的Bean在启动的时候都加载,系统运行的速度快; 

2. 在启动的时候所有的Bean都加载了,我们就能在系统启动的时候,尽早的发现系统中的配置问题 

3. 建议web应用,在启动的时候就把所有的Bean都加载了。(把费时的操作放到系统启动中完成) 

 

 

 

spring国际化例子(MessageSource)

 

1. 在xml中配置messageSource

 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
    <!-- 资源国际化测试 --> 
    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="basenames"> 
            <list> 
                <value>org/rjstudio/spring/properties/messages</value> 
            </list> 
        </property> 
    </bean> 
</beans> 

 

 

2. “org/rjstudio/spring/properties/messages”,是指org.rjstudio.spring.proerties包下的以messages为主要名称的properties文件

 

文件如下:

messages_en_US.properties

messages_zh_CN.properties

messages_zh_HK.properties

 

3. 取值的时候是通过ApplicationContext.getMessage(),拿到对应语言的内容

    public class MessageTest { 
        public static void main(String[] args) { 
            ApplicationContext ctx = new ClassPathXmlApplicationContext("messages.xml"); 
            Object[] arg = new Object[] { "Erica", Calendar.getInstance().getTime() }; 
            String msg = ctx.getMessage("userinfo", arg,Locale.CHINA); 
            System.out.println("Message is ===> " + msg); 
        } 
    } 

 

 

 

spring访问资源(ResourceLoader)

这是spring对资源文件(如:properties)进行存取操作的功能

 

ApplicationContext acxt =new ClassPathXmlApplicationContext("/applicationContext.xml");

 

1.通过虚拟路径来存取。当资源位于CLASSPATH路径下时,可以采用这种方式来存取。

Resource resource = acxt.getResource("classpath:messages_en_CN.properties");

 

2.通过绝对路径存取资源文件。

Resource resource = acxt.getResource("file:F:/testwork/MySpring/src/messages_en_CN.properties");

 

3.相对路径读取资源文件。

Resource resource = acxt.getResource("/messages_en_CN.properties");

 

 

Resource常用的方法:

getFilename() : 获得文件名称 

contentLength() : 获得文件大小 

createRelative(path) : 在资源的相对地址上创建新文件 

exists() : 是否存在 

getFile() : 获得Java提供的File 对象 

getInputStream() :  获得文件的流 

 

 

 

spring载入多个上下文

 

不同项目使用不同分模块策略,spring配置文件分为

applicationContext.xml(主文件,包括JDBC配置,hibernate.cfg.xml,与所有的Service与DAO基类)

applicationContext-cache.xml(cache策略,包括hibernate的配置)

applicationContext-jmx.xml(JMX,调试hibernate的cache性能)

applicationContext-security.xml(acegi安全)

applicationContext-transaction.xml(事务)

moduleName-Service.xml

moduleName-dao.xml

 

两种方法配置

 

1.可以在applicationContext.xml文件中引用

    <beans></beans>标记之间引入其他applicationContext.xml 

    <beans>

         <import resource="applicationContext-cache.xml"/>

    </beans>

 

2.或者在web.xml文件中引用

   <context-param>

     <param-name>contextConfigLocation</param-name>

     <param-value>

         WEB-INF/classes/applicationContext-security.xml

        ,WEB-INF/classes/applicationContext-dao.xml

        ,WEB-INF/classes/applicationContext-Service.xml

     </param-value>

   </context-param>

   <listener>

      <listener-class>

            org.springframework.web.context.ContextLoaderListener

      </listener-class>

   </listener>

 

 

 

spring事件机制(订阅发布模式 == 观察者模式)

 

ApplicationContext事件机制是观察者设计模式的 实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。 如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。

 

两个重要成员

ApplicationEvent:容器事件,必须由ApplicationContext发布;

ApplicationListener:监听器,可由容器中的任何监听器Bean担任。

 

1. 定义容器事件

 

package com.cxg.test.springPlatfrom;

import org.springframework.context.ApplicationEvent;
/**
 * Title: email之事件类
 * EmailEvent类继承了ApplicationEvent类,除此之外,它就是一个普通的Java类
 * Description: dataPlatfrom
 * @author: xg.chen
 * @date:2016年8月24日
 */
public class EmailEvent extends ApplicationEvent{
    private static final long serialVersionUID = 1L;
    //属性
    private String address;
    private String text;
    //构造方法
    public EmailEvent(Object source) {
        super(source);
    }
    public EmailEvent(Object source, String address, String text) {
        super(source);
        this.address = address;
        this.text = text;
    }
    //getter和setter设置
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}
 

 

2. 定义监听器

 

package com.cxg.test.springPlatfrom;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
 * Title: email之监听类
 * 容器事件的监听器类必须实现ApplicationListener接口,实现该接口就必须实现
 * Description: dataPlatfrom
 * @author: xg.chen
 * @date:2016年8月24日
 */
public class EmailNotifier implements ApplicationListener<ApplicationEvent>{

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof EmailEvent){
            EmailEvent emailEvent = (EmailEvent) event;
            System.out.println("email's address:"+emailEvent.getAddress());
            System.out.println("email's text:"+emailEvent.getText());
        } else {
            System.out.println("the Spring's event:"+event);
        }
    }

}
 

 

3. 将监听器注入到spring容器

 

<!-- 配置事件监听 -->
<bean class="com.cxg.test.springPlatfrom.EmailNotifier" />
 

 

4. 测试

 

package com.cxg.test.springPlatfrom;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Title: Spring的ApplicationContexet单元成测试
 * Description: dataPlatfrom
 * @author: xg.chen
 * @date:2016年8月24日
 */
public class SpringTest {
    public static void main(String arg[]){
        //读取Spring容器的配置文件
        @SuppressWarnings("resource")
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        //创建一个事件对象
        EmailEvent emailEvent = new EmailEvent("hello Spring!", "cxg@126.com", "This is SpringApplicatoinContext test!");
        //主动触发事件监视机制
        applicationContext.publishEvent(emailEvent);
    }
}
 

 

 

 

spring的AOP(常用的是拦截器)

 

一般拦截器都是实现HandlerInterceptor,其中有三个方法preHandle、postHandle、afterCompletion

 

1. preHandle:执行controller之前执行

2. postHandle:执行完controller,return modelAndView之前执行,主要操作modelAndView的值

3. afterCompletion:controller返回后执行

 

 

实现步骤:

 

1. 注册拦截器,并且确定拦截器拦截哪些URL

<!-- Check Session -->
    <bean id="validateSystemUserSessionInterceptor" class="com.cherrypicks.appsdollar.cms.interceptor.ValidateSystemUserSessionInterceptor" />
    
    <!-- Interceptors -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/login" />
            <mvc:exclude-mapping path="/logout" />
            <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 --> 
            <ref bean="validateSystemUserSessionInterceptor"  />
        </mvc:interceptor>
    </mvc:interceptors>
    <!-- SpringMVC.end} -->

 

2. 定义拦截器实现类

package com.cherrypicks.appsdollar.cms.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cherrypicks.appsdollar.common.constant.Constants;
import com.cherrypicks.appsdollar.common.exception.InvalidUserSessionException;
import com.cherrypicks.appsdollar.service.cms.CmsUserSessionService;

public class ValidateSystemUserSessionInterceptor extends HandlerInterceptorAdapter {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private CmsUserSessionService userSessionService;

    @Override
    public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler)
            throws Exception {
        logger.debug("ValidateUserSessionInterceptor.preHandle run....");

        final String userIdStr = request.getParameter(Constants.USERID);
        final String sessionId = request.getParameter(Constants.SESSIONID);
        if (!StringUtils.isNotBlank(userIdStr) || !StringUtils.isNotBlank(sessionId)) {
            throw new InvalidUserSessionException(
                    "Invalid user session. userId[" + userIdStr + "], sessionId[" + sessionId + "]");
        }

        final Long userId = Long.parseLong(userIdStr);

        // validate userId and sessionId
        if (!userSessionService.validateUserSession(userId, sessionId)) {
            throw new InvalidUserSessionException(
                    "Invalid user session. userId[" + userId + "], sessionId[" + sessionId + "]");
        }

        return true;
    }

    public static void main(final String[] args) {
        final String i = "a";
        System.out.println(StringUtils.isNotBlank(i));
    }
}

 

 

 

 

参考

http://kouhao123.iteye.com/blog/1633574

http://m.blog.csdn.net/article/details?id=51112702

http://blog.csdn.net/EthanWhite/article/details/52445346

http://site.leshou.com/s/12730359.html

http://elim.iteye.com/blog/1750680

分享到:
评论

相关推荐

    day38 05-Spring的BeanFactory与ApplicationContext区别

    NULL 博文链接:https://364232252.iteye.com/blog/2369489

    Spring中ApplicationContext和beanfactory区别.rar

    Spring中ApplicationContext和beanfactory区别.rar

    BeanFactory&&ApplicationContext;

    这份代码主要适用于我写的一篇博客的资源,主要是想通过简短的代码来帮助我们更清晰的理解IoC实现思路,代码一式两份,分别是纯代码和注解方式。

    1开源框架面试专题及答案.pdf

    BeanFactory 和 ApplicationContext 有什么区别 &gt; BeanFactory 可以理解为含有 bean 集合的工厂类。BeanFactory 包含了种 bean 的定 义, 以便在接收到客户端请求时将对应的 bean 实例化。 &gt; BeanFactory 还能在实例...

    spring 容器.docx

    Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口。他们都可代表Spring容器,Spring容器是生成Bean实例的工厂,并且管理容器中的Bean。 Bean是Spring管理的...

    大厂真题之百度-Java中级

    BeanFactory 和 ApplicationContext 有什么区别 &gt; BeanFactory 可以理解为含有 bean 集合的工厂类。BeanFactory 包含了种 bean 的定义, 以便在接收到客户端请求时将对应的 bean 实例化。 &gt; BeanFactory 还能在实例...

    Spring入门.docx

    (2)BeanFactory与ApplicationContext: ApplicationContext内部封装了BeanFactory,功能更加强大。 加载xml文件使用ClassPathXmlApplicationContext("applicationContext.xml") (3)BeanFactory(bean工厂)与...

    SpringFramework常见知识点.md

    - BeanFactory和ApplicationContext的区别是什么? - 什么是IOC容器和DI依赖注入? - Spring依赖注入的方式有几种? - 一个bean的定义包含了什么?(BeanDefinition) - bean的作用域有哪些? - Spring 的扩展点主要...

    Spring面试专题1

    2、使用Spring框架能带来哪些好处 3、什么是控制反转(IOC) 5、BeanFactory和ApplicationContext有什么区别 2、FileS

    25个经典的Spring面试问答

    BeanFactory和ApplicationContext有什么区别 Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的...

    Spring的Bean配置

    Spring IOC和DI概述,Bean的配置形式,IOC容器BeanFactory和ApplicationContext概述,依赖注入的方式,属性注入,构造器注入等案例

    高级开发spring面试题和答案.pdf

    Spring中BeanFactory和ApplicationContext的区别 谈谈Spring IOC的理解,原理与实现? bean的生命周期,详细看上面 SpringBoot自动装配的过程的原理: spring的缓存; spring是如何解决的循环依赖; BeanFactory和...

    Java面试 spring知识点 线程池 面试题

    Spring原理 2 Spring ioc 原理 3 ...beanfactory和applicationcontext 5 类装载器ClassLoader 6 Spring aop 原理 6 Aop代理 7 Spring 事物 10 数据库锁 12 ThreadLocal 13 Spring TaskExecutor线程池 16

    spring课堂笔记.docx

    Spring 容器:介绍了 Spring 容器的不同类型,包括 BeanFactory 和 ApplicationContext,以及它们在管理对象生命周期和依赖注入方面的作用。 依赖注入:详细解释了依赖注入的原理和用法,以及如何配置和管理 Bean ...

    Spring面试专题.pdf

    5、BeanFactory 和 ApplicationContext 有什么区别? 6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、...

    Spring面试题.zip

    5、BeanFactory 和 ApplicationContext 有什么区别? 6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、...

    Spring Bean 的生命周期.docx

    Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。...本文分别对 BeanFactory 和 ApplicationContext 中的生命周期进行分析。

    spring面试题25道图文并茂的spring面试题

    1. 什么是Spring框架?Spring框架有哪些主要模块? 2. 使用Spring框架有什么好处? 3. 什么是控制反转(IOC)?...5. BeanFactory和ApplicationContext有什么区别? 等。。。。。。。。。。。。。。。。

    海创软件组-Spring 核心之IoC(一)

    目录 Spring IoC容器的两个接口 依赖注入的类型 Bean的配置 …IOC:控制反转依赖注入。它使程序组件或类之间...ApplicationContext和BeanFactory的区别在于对Bean的创建时机不同。BeanFactory在初始化的时候,不会被

Global site tag (gtag.js) - Google Analytics