spring_07使用spring的特殊bean、完成分散配置
一. 前言
分散配置思路:创建properties文件,添加数据,在beans文件中先配置properties文件,再在bean中使用占位符引用数据
- 对于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、完成分散配置的更多相关文章
- 使用spring的特殊bean完成分散配置
1.在使用分散配置时,spring的配置文件applicationContext.xml中写法如下: <!-- 引入db.properties文件, --> <context:pro ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- Bean 注解(Annotation)配置(1)- 通过注解加载Bean
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- 使用spring的特殊bean完成配置
1.分散配置 beans.xml配置如下: 使用占位符变量代替bean装配文件中的硬编码配置.占位符采用${variable}形式. 说明:当通过context:property-placeholde ...
- [spring]03_装配Bean
3.1 JavaBean 3.1.1 JavaBean 是什么 JavaBean 是一种JAVA语言写成的可重用组件. 为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器. Jav ...
- Spring系列之bean的使用
一.Bean的定义 <bean id="userDao" class="com.dev.spring.simple.MemoryUserDao"/> ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- spring容器对bean生命周期的管理三中方式
spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)方式一:使用springXML配置中的init-meth ...
随机推荐
- 2.SpringMVC注解开发
1.创建SpringMVC项目 配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...
- PHP中 PCRE正则表达式模式修饰符“u” 的使用。
u (PCRE_UTF8) 此修正符打开一个与 perl 不兼容的附加功能. 模式字符串被认为是utf-8的. 这个修饰符 从 unix 版php 4.1.0 或更高,win32版 php 4.2.3 ...
- spring-data-redis和jedis版本对应收集总结
基于spring主版本为4.3.13.RELEASE的项目,测试以下对应版本可用. spring-data-redis版本 jedis版本 备注 1.5.2.RELEASE 2.7.3 1.6.0 ...
- JVM运行时数据区(一)
1.名词解释: 栈帧:栈帧是用于支持虚拟机进行方法调用和方法执行的数据结构,它是虚拟机运行时数据区中的虚拟机栈的栈元素. 2.程序计数器: 程序计数器是一块比较小的内存空间,可以将它看作是当前线程所执 ...
- Virtual Networking
How the virtual networks used by guests work Networking using libvirt is generally fairly simple, an ...
- CSS中的px与物理像素、逻辑像素、1px边框问题
一直不太清楚CSS中的1px与逻辑像素.物理像素是个什么关系(作为一名前端感觉很惭愧 -_-!),今天终于花时间彻底弄清楚了,其实弄清楚之后就觉得事情很简单,但也只有在弄清楚之后,才会觉得简单(语出& ...
- 【sql注入教程】mysql注入直接getshell
Mysql注入直接getshell的条件相对来说比较苛刻点把 1:要知道网站绝对路径,可以通过报错,phpinfo界面,404界面等一些方式知道 2:gpc没有开启,开启了单引号被转义了,语句就不能正 ...
- 学习笔记第六课 VB程序
VB程序的特殊地方在于: 前几课学的破解方法,诸如设置API断点,修改关键CALL的返回值,MESSAGEBOX断点等,这些对于VB程序都是无效的. 这节课是设置VB的API断点,绕过报错弹窗来破解. ...
- 最优路径算法合集(附python源码)(原创)
主要的最优(最短)路径算法: 一.深度优先算法:二.广度优先算法:三.Dijstra最短路径:四.floyd最短路径(待): 一.深度优先算法 图的深度优先搜索(Depth First Search) ...
- okHttp超时报错解决方案
Android 使用okhttp,如果客户端等待的时间超过了okHttp的默认时间,就会报错java.net.SocketTimeoutException: timeout 所以,需要在调用okHtt ...