一.   前言

    分散配置思路:创建properties文件,添加数据,在beans文件中先配置properties文件,再在bean中使用占位符引用数据

  1. 对于bean的生命周期中的很多处理接口,处理方法都是spring自带bean完成,即spring的特殊bean.

    2. 当通过 context:property-placeholder 引入 属性文件的时候,有多个需要使用 , 号间隔.

    3.beans文件使用占位符引用properties文件中内容:eg.${key}

二. 分散配置 

  通过自建properties文件,配置键值,在spring配置文件中读取,实现分散配置

  db.properties文件  

  username=admin
  password=123456
  driver=com.ahd.www

  配置文件beans:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 引入我们的db.properties文件 -->
<!-- 方式一:配置bean
<bean class="PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>com/ahd/dispatcher/db.properties</value>
</list>
</property>
</bean>
-->
<!-- 方式二: -->
<context:property-placeholder location="classpath:com/ahd/dispatcher/db.properties"></context:property-placeholder> <bean id="dbutil" class="com.ahd.dispatcher.DBUtil">
<property name="username"><value>${username}</value></property>
<property name="password"><value>${password}</value></property>
<property name="drivername"><value>${driver}</value></property>
</bean> </beans>

  

  DBUtil类:  

package com.ahd.dispatcher;

public class DBUtil {

    private String username;
private String password;
private String drivername; public DBUtil() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDrivername() {
return drivername;
}
public void setDrivername(String drivername) {
this.drivername = drivername;
} }

DBUtil.java

  测试类:Test.java

package com.ahd.dispatcher;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("com/ahd/dispatcher/beans.xml"); DBUtil dbutil = (DBUtil) ac.getBean("dbutil"); System.out.println(dbutil.getUsername()+" "+dbutil.getPassword());
} }

Test

spring_07使用spring的特殊bean、完成分散配置的更多相关文章

  1. 使用spring的特殊bean完成分散配置

    1.在使用分散配置时,spring的配置文件applicationContext.xml中写法如下: <!-- 引入db.properties文件, --> <context:pro ...

  2. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  3. Bean 注解(Annotation)配置(1)- 通过注解加载Bean

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

  4. 使用spring的特殊bean完成配置

    1.分散配置 beans.xml配置如下: 使用占位符变量代替bean装配文件中的硬编码配置.占位符采用${variable}形式. 说明:当通过context:property-placeholde ...

  5. [spring]03_装配Bean

    3.1 JavaBean 3.1.1 JavaBean 是什么 JavaBean 是一种JAVA语言写成的可重用组件. 为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器. Jav ...

  6. Spring系列之bean的使用

    一.Bean的定义 <bean id="userDao" class="com.dev.spring.simple.MemoryUserDao"/> ...

  7. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  8. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  9. spring容器对bean生命周期的管理三中方式

    spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)方式一:使用springXML配置中的init-meth ...

随机推荐

  1. linux根目录介绍

    1. /bin binary二进制 存放系统许多可执行程序文件 执行的相关指令,例如ls pwd whoami,后台的支持文件目录 2. /sbin super binary超级的二进制 存放系统许多 ...

  2. Maven package 报错解决记录以及编译scala的pom.xml

    可以打包的pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  3. C#中Quartz的简单易懂定时任务实现

    作为一个优秀的开源调度框架,Quartz 具有以下特点: 强大的调度功能,例如支持丰富多样的调度方法,可以满足各种常规及特殊需求: 灵活的应用方式,例如支持任务和调度的多种组合方式,支持调度数据的多种 ...

  4. Core 读取配置文件

    新建控制台 static void Main(string[] args) { Console.WriteLine("Hello World!"); //获取应用程序的当前工作目录 ...

  5. ubuntu 16.04 安装caffe2的方法及问题解决

    工作需要安装caffe2,从用户体验上来讲,caffe2的安装绝对是体验比较差的那种,花费了我那么多时间去倒腾,这样的用户体验的产品,估计后面是比较危险的. 废话少说,直接上步骤: 官网上有安装目录, ...

  6. JavaScript百宝箱

    Js的外部引用 外部文件中不用添加<script>标签,引用书写位置需在使用之前 <script type="text/javascript" src=" ...

  7. thymeleaf-在font标签中的使用

    <font color="red" th:text="开始了">font外</font>页面显示红色字体 开始了 (同时存在,则前者覆盖 ...

  8. python读取pdf文件

    pdfplumber简介 Pdfplumber是一个可以处理pdf格式信息的库.可以查找关于每个文本字符.矩阵.和行的详细信息,也可以对表格进行提取并进行可视化调试. 文档参考https://gith ...

  9. python(leetcode)-48旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  10. Django ListView实现分页

    效果: url.py main-urls from django.urls import path,include urlpatterns = [ path('admin/', admin.site. ...