package chapter22;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
//如果被装配的Bean实现了相应的接口,就可以在Bean中获得相应的信息。,或者进行某些操作。
public class HelloServiceImpl implements HelloService,
//以下同时为接口的调用的顺序
BeanNameAware,//获得Bean名,也就是<Bean>标签的id属性值。
BeanClassLoaderAware,//获得装载过程中的ClassLoader对象。
BeanFactoryAware,//获得BeanFactory对象
ApplicationContextAware,//获得ApplicationContext对象
InitializingBean, //在Bean的所有属性设置完后,并且在调用完上面接口的方法后,调用此接口的afterPropertiesSet方法
DisposableBean //当销毁Bean时,调用此接口的destroy方法
{
private String greeting;
public String getGreeting() {
// TODO Auto-generated method stub
return "hello "+greeting;
}
//以下同时为方法的调用的顺序
public void setGreeting(String greeting)
{
this.greeting = greeting;
System.out.println("设置greeting属性");
}
public void setBeanName(String name) {
System.out.println("BeanNameAware接口方法 "+name);
}
public void setBeanClassLoader(ClassLoader arg0) {
System.out.println("BeanClassLoaderAware接口方法 "+arg0);
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("BeanFactoryAware接口方法 "+arg0);
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("ApplicationContextAware接口方法 "+applicationContext);
}
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean接口方法");
}
public void initMethod(){
System.out.println("<bean>标签的init-Method属性指定的方法,此方法在afterPropertiesSet()之后调用");
}
public void destroy() throws Exception {
System.out.println("destroy");
}
public void destroyMethod(){
System.out.println("<bean>标签的destroy-Method属性指定的方法,此方法在destroy()之后调用");
}
}
applicationContext.xml中的部分:
<bean id="greeting" class="chapter22.HelloServiceImpl" scope="prototype" init-method="initMethod" destroy-method="destroyMethod">
<property name="greeting" >
<value>yjz</value>
</property>
</bean>