使用spring的特殊bean

一 让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.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://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=scott
drivername=oracle:jdbc:driver:OracleDirver
url=jdbc:oracle:thin:@127.0.0.1:1521:hsp
pwd=tiger

db2.properties
db2.name=scott3
db2.drivername=oracle:jdbc:driver:OracleDirver3
db2.url=jdbc:oracle:thin:@127.0.0.1:1521:hsp3
db2.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引入属性文件的时候,有多个需要使用,号间隔。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值