一.   前言

    分散配置思路:创建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. 最小化安装的centos7.5上编译安装git2.19

    VMware Workstation已经采用最小化安装CentOS7,显示版本为CentOS7.5,准备采用yum安装git. 采用yum list git发现可安装的GIT软件包版本1.8.3.1, ...

  2. Linux下Oracle表空间及用户创建

    记录详细过程以备使用 Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as sys@i ...

  3. Virtio: An I/O virtualization framework for Linux

    The Linux kernel supports a variety of virtualization schemes, and that's likely to grow as virtuali ...

  4. [转] The QCOW2 Image Format

    The QCOW2 Image Format https://people.gnome.org/~markmc/qcow-image-format.html The QCOW image format ...

  5. [转] KVM I/O slowness on RHEL 6

    KVM I/O slowness on RHEL 6 http://www.ilsistemista.net/index.php/virtualization/11-kvm-io-slowness-o ...

  6. python 字典详细使用

    1. 字典 字典是无序.可变序列. 定义字典时,每个元素的键和值用冒号分隔,元素之间用逗号分隔,所有的元素放在一对大括号“{}”中. 字典中的键可以为任意不可变数据,比如整数.实数.复数.字符串.元组 ...

  7. Memcached详解

    Memcached介绍 Memcached是什么? Free & open source, high-performance, distributed memory object cachin ...

  8. SpringBoot 项目打包后运行报 org.apache.ibatis.binding.BindingException

    今天把本地的一个SpringBoot项目打包扔到Linux服务器上,启动执行,接口一访问就报错,但是在本地Eclipse中启动执行不报错,错误如下: org.apache.ibatis.binding ...

  9. Spark基础-scala学习(八、隐式转换与隐式参数)

    大纲 隐式转换 使用隐式转换加强现有类型 导入隐式转换函数 隐式转换的发生时机 隐式参数 隐式转换 要实现隐式转换,只要程序可见的范围内定义隐式转换函数即可.Scala会自动使用隐式转换函数.隐式转换 ...

  10. Spring 静态代理+JDK动态代理和CGLIB动态代理

    代理分为两种:静态代理 动态代理 静态代理:本质上会在硬盘上创建一个真正的物理类 动态代理:本质上是在内存中构建出一个类. 如果多个类需要进行方法增强,静态代理则需要创建多个物理类,占用磁盘空间.而动 ...