一
Bean後處理器允許在初始化回調方法前後進行額外的Bean處理
Bean後處理器的基本要求是實現BeanPostProcessor接口
(
(
(
(
(
(
(
(
使用Bean工廠作為IoC容器時
二
StorageConfig標記接口
[java]
package deproject
/**
* 標記接口
* @author jackie
*
*/
public interface StorageConfig {
public String getPath();
}
package deproject
/**
* 標記接口
* @author jackie
*
*/
public interface StorageConfig {
public String getPath();
} Cashier類
[java]
package deproject
import java
import java
import java
import java
import java
import org
/**
* Cashier類實現BeanNameAware感知接口和
* @author jackie
*
*/
public class Cashier implements BeanNameAware
private String name;
private BufferedWriter writer;
private String path;
public void setPath(String path) {
this
}
public void openFile() throws IOException {
File file = new File(path
FileWriter fw = new FileWriter(file
writer = new BufferedWriter(fw);
}
public void checkout(ShoppingCart cart) throws IOException {
double total =
for (Product product : cart
total += product
}
writer
writer
}
public void closeFile() throws IOException {
writer
}
@Override
public String getPath() {
return path;
}
@Override
public void setBeanName(String beanName) {
this
}
}
package deproject
import java
import java
import java
import java
import java
import org
/**
* Cashier類實現BeanNameAware感知接口和
* @author jackie
*
*/
public class Cashier implements BeanNameAware
private String name;
private BufferedWriter writer;
private String path;
public void setPath(String path) {
this
}
public void openFile() throws IOException {
File file = new File(path
FileWriter fw = new FileWriter(file
writer = new BufferedWriter(fw);
}
public void checkout(ShoppingCart cart) throws IOException {
double total =
for (Product product : cart
total += product
}
writer
writer
}
public void closeFile() throws IOException {
writer
}
@Override
public String getPath() {
return path;
}
@Override
public void setBeanName(String beanName) {
this
}
}
PathCheckingBeanPostProcessor自定義Bean後處理器
[java]
package deproject
import java
import org
import org
/**
* 通用的組件
* @author jackie
*
*/
public class PathCheckingBeanPostProcessor implements BeanPostProcessor {
/**
* 在文件打開之前進行路徑檢查
* 須返回所處理的Bean的一個實例
*/
@Override
public Object postProcessBeforeInitialization(Object bean
throws BeansException {
// 如果Bean實現了StorageConfig接口
if (bean instanceof StorageConfig) {
String path = ((StorageConfig)bean)
File file = new File(path);
if (!file
file
}
}
return bean;
}
/**
* 即使什麼都不做
*/
@Override
public Object postProcessAfterInitialization(Object bean
throws BeansException {
return bean;
}
}
package deproject
import java
import org
import org
/**
* 通用的組件
* @author jackie
*
*/
public class PathCheckingBeanPostProcessor implements BeanPostProcessor {
/**
* 在文件打開之前進行路徑檢查
* 須返回所處理的Bean的一個實例
*/
@Override
public Object postProcessBeforeInitialization(Object bean
throws BeansException {
// 如果Bean實現了StorageConfig接口
if (bean instanceof StorageConfig) {
String path = ((StorageConfig)bean)
File file = new File(path);
if (!file
file
}
}
return bean;
}
/**
* 即使什麼都不做
*/
@Override
public Object postProcessAfterInitialization(Object bean
throws BeansException {
return bean;
}
}Bean配置
[html]
<?xml version=
<beans xmlns=
xmlns:xsi=
xsi:schemaLocation=
<!
<bean class=
<bean id=
<property name=
<property name=
</bean>
<bean id=
<property name=
<property name=
</bean>
<bean id=
<property name=
<property name=
</bean>
<bean id=
<bean id=
<property name=
</bean>
</beans>
<?xml version=
<beans xmlns=
xmlns:xsi=
xsi:schemaLocation=
<!
<bean class=
<bean id=
<property name=
<property name=
</bean>
<bean id=
<property name=
<property name=
</bean>
<bean id=
<property name=
<property name=
</bean>
<bean id=
<bean id=
<property name=
</bean>
</beans>測試類
[java]
package deproject
import java
import org
import orgntext
import orgntext
/**
* @author jackie
*
*/
public class BeanPostProcessorTest {
@Test
public void testBeanPostProcessor() throws IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
Product aaa = (Product) applicationContext
Product cdrw = (Product) applicationContext
Product dvdrw = (Product) applicationContext
ShoppingCart shoppingCart = (ShoppingCart) applicationContext
shoppingCart
shoppingCart
shoppingCart
Cashier cashier = (Cashier) applicationContext
cashier
}
}
package deproject
import java
import org
import orgntext
import orgntext
/**
* @author jackie
*
*/
public class BeanPostProcessorTest {
@Test
public void testBeanPostProcessor() throws IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
Product aaa = (Product) applicationContext
Product cdrw = (Product) applicationContext
Product dvdrw = (Product) applicationContext
ShoppingCart shoppingCart = (ShoppingCart) applicationContext
shoppingCart
shoppingCart
shoppingCart
Cashier cashier = (Cashier) applicationContext
cashier
}
}
注
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27853.html