`
zjm16
  • 浏览: 70125 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
文章分类
社区版块
存档分类
最新评论

spring中所配置Bean的後處理方式(轉)

阅读更多
BeanFactoryPostProcessor 接口是对Bean 工厂的后处理操作。


在Spring 的PropertyPlaceholderConfigurer 类是实现BeanFactoryProcessor 接口中非常有用的类。它用于Spring 从外部属性文件中载入属性,并使用这些属性值替换Spring 配置文件中的占位符变量(${varible})。


Spring 的ApplicationContext 容器可以非常方便的使用PropertyPlaceholderConfigurer,只需通过简单的配置即可使用。

示例:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties" />
</bean>

如果需要使用多个配置文件可以使用PropertyPlaceholderConfigurer 的locations属性。


具體一點的例子如下:

Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。藉由这个类别,您可以将一些组态设定,移出至.properties档案中,如此的安排可以让XML定义档负责系统相关设定,而.properties档可以作为客户根据需求,自定义一些相关的参数。
来看一个Bean定义档的实际例子:

beans-config.xml
<?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="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           <property name="location">               <value>hello.properties</value>           </property>       </bean>

      <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
          <property name="helloWord">
              <value>${onlyfun.caterpillar.helloWord}</value>
          </property>
      </bean>
</beans>
假设在helloBean中有许多依赖注入的属性,这些都是比较不常变动的属性,而其中helloWord会经常变动,可以透过hello.properties来简单的设定,而这个资讯已设定在configBean的location属性中:


hello.properties
onlyfun.caterpillar.helloWord=Welcome!
在helloBean的helloWord属性中,${onlyfun.caterpillar.helloWord}将会被hello.properties的helloWord所取代。

如果有多个.properties档案,则可以透过locations属性来设定,例如:

beans-config.xml
<?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="configBean"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           <property name="locations">
              <list>                  <value>hello.properties</value>
                  <value>welcome.properties</value>
                  <value>other.properties</value>    
              </list>
          </property>           </bean>

      <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
          <property name="helloWord">
              <value>${onlyfun.caterpillar.helloWord}</value>
          </property>
          ...
      </bean>
</beans> ======================================PropertyPlaceholderConfigurer类就是bean factory post-processor的一种,它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。
例如---spring-context.xml----
<bean id="propertyConfigurer"   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
             <list>
                 <value>/WEB-INF/jdbc.properties</value>
             </list>
         </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"><value>${driver}</value></property>
    <property name="url"><value>jdbc:${dbname}</value></property>
</bean>而实际的jdbc.propertis文件是
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
而jdbc.propertis是怎样引用的呢: 将上边一段配置注册在web.xml中就可以了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
当然,不要忘了spring的监听器注册
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这样,一个简单的数据源就设置完毕了。
实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义
的工具。
分享到:
评论

相关推荐

    jdk代理,cgib代理和spring后处理bean的关系

    NULL 博文链接:https://wuhuajun.iteye.com/blog/1926731

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    2023最新Spring全家桶面试题-图灵徐庶

    13. 配置Bean的方式:包括XML配置、JavaConfig、注解配置等。 14. Spring支持的几种bean的作用域:包括singleton、prototype、request、session、global session等。 15. 单例bean的优势:可以减少对象的创建次数...

    spring杂谈 作者zhang KaiTao

    1.1 Spring事务处理时自我调用的解决方案及一些实现方式的风险 1.2 我对AOP的理解 1.3 Spring开闭原则的表现-BeanPostProcessor的扩展点-1 1.4 我对IoC/DI的理解 1.5 SpringMVC + spring3.1.1 + hibernate4.1.0 集成...

    Spring实战之Bean的后处理器操作示例

    主要介绍了Spring实战之Bean的后处理器操作,结合实例形式详细分析了Bean的后处理器相关配置、操作方法及使用注意事项,需要的朋友可以参考下

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    Spring攻略(第二版 中文高清版).part1

    2.11 创建Bean后处理器 85 2.11.1 问题 85 2.11.2 解决方案 85 2.11.3 工作原理 86 2.12 外部化Bean配置 89 2.12.1 问题 89 2.12.2 解决方案 89 2.12.3 工作原理 90 2.13 解析文本消息 91 2.13.1...

    Spring in Action(第二版 中文高清版).part2

    3.5.2 Bean工厂的后处理 3.5.3 配置属性的外在化 3.5.4 提取文本消息 3.5.5 程序事件的解耦 3.5.6 让Bean了解容器 3.6 脚本化的Bean 3.6.1 给椰子上Lime 3.6.2 脚本化Bean 3.6.3 注入脚本化Bean的属性 ...

    Spring in Action(第二版 中文高清版).part1

    3.5.2 Bean工厂的后处理 3.5.3 配置属性的外在化 3.5.4 提取文本消息 3.5.5 程序事件的解耦 3.5.6 让Bean了解容器 3.6 脚本化的Bean 3.6.1 给椰子上Lime 3.6.2 脚本化Bean 3.6.3 注入脚本化Bean的属性 ...

    Spring MVC 入门实例

    配置 SimpleUrlHandlerMapping, 在上面的配置文件中, /hello.do 的请求将被 helloController 处理. "/hello.do"和"helloController" 是变量, 你可以更改. 但是你注意到了吗, hello.do 以 .do 作为后缀名. 如果这里...

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    -- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性--&gt; &lt;bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt; &lt;!-- 指定对满足哪些bean ...

    Spring攻略(第二版 中文高清版).part2

    2.11 创建Bean后处理器 85 2.11.1 问题 85 2.11.2 解决方案 85 2.11.3 工作原理 86 2.12 外部化Bean配置 89 2.12.1 问题 89 2.12.2 解决方案 89 2.12.3 工作原理 90 2.13 解析文本消息 91 2.13.1...

    spring in action英文版

     2.4.2 对Bean工厂进行后处理  2.4.3 分散配置  2.4.4 定制属性编辑器  2.4.5 解析文本信息  2.4.6 监听事件  2.4.7 发布事件  2.4.8 感知其他Bean  2.5 小结  第3章 创建切面  3.1 ...

    spring boot oauth2 jwt 中文文档.docx

    在 Spring Boot 中,我们可以使用 @Configuration 注解来定义配置类,并使用 @Bean 注解来定义 Bean。 使用 JWT(JSON Web Token)来生成访问令牌是 OAuth2 授权框架的一种常见实现。JWT 是一种基于 json 的令牌,...

    Spring3.x企业应用开发实战(完整版) part1

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    Spring in Action(第2版)中文版

    16.4.3在jsf页面中使用springbean 16.4.4在jsf中暴露应用程序环境 16.5spring中带有dwr的支持ajax的应用程序 16.5.1直接web远程控制 16.5.2访问spring管理的beandwr 16.6小结 附录a装配spring a.1下载spring ...

    Spring Security 中文教程.pdf

    Spring Bean配置 19.4.6. LDAP属性和自定义UserDetails 20. JSP标签库 20.1. 声明Taglib 20.2. authorize 标签 20.3. authentication 标签 20.4. accesscontrollist 标签 21. Java认证和授权服务...

    Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现.doc

    Spring AOP 实现原理详解之 AOP...postProcessBeforeInstantiation 是在创建 Bean 之前的后处理方法。在这个方法中,我们可以判断是否是 AOP 基础类,并且可以跳过 shouldSkip。 五、postProcessAfterInitialization ...

    Spring_Framework_ API_5.0.5 (CHM格式)

    所以 Spring4 必须支持 Java6,7 和8,为了保持向后兼容性, Spring 框架没有适应 Java8 带来的许多新特性,比如 lambda 表达式。 Spring5 的基准版本为8,因此它使用了 Java8 和9的许多新特性。例如: Spring ...

    spring security 参考手册中文版

    5.9后处理配置的对象 45 5.10自定义DSL 46 6.安全命名空间配置 47 6.1简介 47 6.1.1命名空间的设计 49 6.2安全命名空间配置入门 50 6.2.1 web.xml配置 50 6.2.2最小的配置 50 6.2.3表单和基本登录选项 52 设置默认的...

Global site tag (gtag.js) - Google Analytics