现在的位置: 首页 > 综合 > 正文

spring知识点3

2013年03月11日 ⁄ 综合 ⁄ 共 1817字 ⁄ 字号 评论关闭

Bean的继承:

抽象bean

通过在bean元素添加属性abstract=”true”声明该bean为抽象的,即Spring容器在加载配置文件时不加载此bean。

注意:抽象bean不能被实例化,即不能通过ctx.getBean(“name”)得到bean的实例;也不能让其他的bean的ref指向该抽象bean。

 

定义子bean

通过添加bean元素的属性parent=”beanId”来指定子bean及它的父bean。

 

???Spring bean的继承与java中继承的区别:

 

Bean后处理器

Spring容器提供了一种bean,该bean无id,负责对容器中其他bean进行管理。该bean类必须实现BeanPostProcessor接口。该接口包含以下两个方法:

× postProcessorBeforeInitialization();

×  postProcessorAfterInitialization();

这两个方法在每个管理bean执行初始化前后执行。

 

 

容器后处理器

类似bean后处理器,实现BeanFactoryPostProcessor接口,重写postProcessBeanFactory()。

Spring提供了两种处理器:

× PropertyPlaceHolderConfigurer:调用属性文件中的值用在bean中;

× PropertyOverrideConfigurer.:调用属性文件中的值覆盖bean中相应有的值。

 

 

与容器交互

 

 

ApplicationContext介绍

国际化支持

ApplicationContext接口继承MessageSource接口,因此具备国际化功能。下面是MessageSource接口中定义的三个用于国际化的方法:

× String getMessage(String code,Object[] args,Locale loc);

× String getMessage(String code,Object[] args,String default,Locale loc);

× String getMessage(MessageSourceResolvable resolvable,Locale loc);

bean类通过id=”messageSource” class=”org.springframework.context.support.ResourceBundleMessageSource”来确定国际化。

 

 

事件处理

Spring的事件框架:

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

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

 

Web应用中自动加载ApplicationContext

通过ContextLoader声明式的创建ApplicationContext。ContextLoader有以下两个实现类:

× ContextLoaderListener。(需要支持Servlet2.4规范的容器)。

在web.xml文件中添加如下代码(多个配置文件用逗号“,”隔开):

<context-param>

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

    <param-value>

/WEB-INF/appContext1.xml

,/WEB-INF/appContext2.xml

</param-value>

 

</context-param>

<listener>

       <listener-class>

       org.springframework.web.context.ContextLoaderListener

       </listener-class>

</listener>

×  ContextLoaderServlet。

在web.xml文件中添加如下代码:

<servlet>

       <servlet-name>demoServlet</servlet-name>

       <servlet-class>

           org.springframework.web.context.ContextLoaderServlet

       </servlet-class>

</servlet>

抱歉!评论已关闭.