使用Spring的特殊bean
package com.hsp.beanlife;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object arg0,
String arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization 函
数被调用");
System.out.println(arg0+" 被创建的时间是"+new
java.util.Date());
return arg0;
}
public Object postProcessBeforeInitialization(Object arg0,
String arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization
函数被调用");
return arg0;
}
}
<!-- 配置我们的自己后置处理器(有点类似我们的filter) -->
<bean id="myBeanPostProcessor"
class="com.hsp.beanlife.MyBeanPostProcessor" />
</beans>
通过properties文件注入特殊bean
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;
}
}
name=scott
drivername=oracle:jdbc:driver:OracleDirver
url=jdbc:oracle:thin:@127.0.0.1:1521:hsp
pwd=tiger
# key=value
db2.name=scott3
db2.drivername=oracle:jdbc:driver:OracleDirver3
db2.url=jdbc:oracle:thin:@127.0.0.1:1521:hsp3
db2.pwd=tiger3
# key=value
<?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文件 -->
<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>
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());
}
}
运行结果:
2020-3-2 14:15:34
org.springframework.context.support.AbstractApplicationContext
prepareRefresh
信息: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@1e
f9f1d: display name
[org.springframework.context.support.ClassPathXmlApplicationContext@1
ef9f1d]; startup date [Mon Mar 02 14:15:34 CST 2020]; root of context
hierarchy
2020-3-2 14:15:34
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
信息: Loading XML bean definitions from class path resource
[com/hsp/dispatch/beans.xml]
2020-3-2 14:15:35
org.springframework.context.support.AbstractApplicationContext
obtainFreshBeanFactory
信息: Bean factory for application context
[org.springframework.context.support.ClassPathXmlApplicationContext@1
ef9f1d]:
org.springframework.beans.factory.support.DefaultListableBeanFactory@
f0c85e
2020-3-2 14:15:35
org.springframework.core.io.support.PropertiesLoaderSupport
loadProperties
信息: Loading properties file from class path resource
[com/hsp/dispatch/db.properties]
2020-3-2 14:15:35
org.springframework.core.io.support.PropertiesLoaderSupport
loadProperties
信息: Loading properties file from class path resource
[com/hsp/dispatch/db2.properties]
2020-3-2 14:15:35
org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
信息: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@
f0c85e: defining beans
[org.springframework.beans.factory.config.PropertyPlaceholderConfigur
er#0,dbutil,dbutil2]; root of factory hierarchy
```
oracle:jdbc:driver:OracleDirver3 scott3