spring 出了奇怪的问题:实现BeanPostProcessor后,方法没有调用
java代码:
package com.init;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Bean implements BeanPostProcessor {
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
public void init() {
System.out.println("this is init method");
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessAfterInitialization");
return arg0;
}
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessAfterInitialization");
return arg0;
}
public void sayHello() {
System.out.println("hello ");
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"init.xml");
Bean bean = (Bean) context.getBean("bean");
bean.sayHello();
}
}
xml文件 :<?xml version="1.0" encoding="GB18030"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
">
<bean id="bean" class="com.init.Bean" init-method="init">
<property name="name">
<value>testName</value>
</property>
</bean>
</beans>
感谢回复者,祝你连同你的家人快乐。