使用spring的特殊bean

来源:互联网 发布:ubuntu 17.04更新源 编辑:程序博客网 时间:2024/04/30 14:45
一 让spring特殊对待这些bean
1 通过配置后加工bean,涉及到Bean和Bean工厂生命周期。
2 改变依赖注入,将字符串转换成其它类型。
3 从属性文本装载信息,包括信息国际化。
4 监听并处理其它bean及spring发布的系统消息。
5 知道自己在spring中的唯一表识。

二 代码
DBUtil
package com.hsp.dispatch;public class DBUtil {        private String drivername;        private String url;        private String name;        private String pwd;        public String getDrivername() {                return drivername;        }        public void setDrivername(String drivername) {                this.drivername = drivername;        }        public String getUrl() {                return url;        }        public void setUrl(String url) {                this.url = url;        }        public String getName() {                return name;        }        public void setName(String name) {                this.name = name;        }        public String getPwd() {                return pwd;        }        public void setPwd(String pwd) {                this.pwd = pwd;        }}

beans.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";><!-- 引入我们的db.properties文件 --><context:property-placeholder location="classpath:com/hsp/dispatch/db.properties,classpath:com/hsp/dispatch/db2.properties"/><!-- 配置一DBUtil对象 $占位符号 --><bean id="dbutil" class="com.hsp.dispatch.DBUtil"><property name="name" value="${name}" /><property name="drivername" value="${drivername}" /><property name="url" value="${url}" /><property name="pwd" value="${pwd}" /></bean><!-- 配置一DBUtil对象 --><bean id="dbutil2" class="com.hsp.dispatch.DBUtil"><property name="name" value="${db2.name}" /><property name="drivername" value="${db2.drivername}" /><property name="url" value="${db2.url}" /><property name="pwd" value="${db2.pwd}" /></bean></beans>

db.properties
name=scottdrivername=oracle:jdbc:driver:OracleDirverurl=jdbc:oracle:thin:@127.0.0.1:1521:hsppwd=tiger

db2.properties
db2.name=scott3db2.drivername=oracle:jdbc:driver:OracleDirver3db2.url=jdbc:oracle:thin:@127.0.0.1:1521:hsp3db2.pwd=tiger3

App1
package com.hsp.dispatch;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/dispatch/beans.xml");            DBUtil dbUtil=(DBUtil) ac.getBean("dbutil2");        System.out.println(dbUtil.getDrivername()+" "+dbUtil.getName());        }}

三 测试结果
oracle:jdbc:driver:OracleDirver3 scott3

四 小结
说明:当通过context:property-placeholder引入属性文件的时候,有多个需要使用,号间隔。