spring中InitializingBean,DisposableBean,BeanPostProcessor接口例子

来源:互联网 发布:主流社交软件 编辑:程序博客网 时间:2024/05/20 12:47

package com.apjept.bean.chapter;

@Service
public class Demo implements InitializingBean,DisposableBean,BeanPostProcessor{

 @Override
 public void afterPropertiesSet() throws Exception {
  System.out.println("*********");
 }

 @Override
 public void destroy() throws Exception {
  System.out.println("%%%%%%%%%%%");
 }

 @Override
 public Object postProcessAfterInitialization(Object arg0, String arg1)
   throws BeansException {
  System.out.println("****22222*****");
  return arg0;
 }

 @Override
 public Object postProcessBeforeInitialization(Object arg0, String arg1)
   throws BeansException {
  System.out.println("*****111****");
  return arg0;
 }

}

定义AspectJTestBean

package com.apjept.bean.chapter;

@Service
public class AspectJTestBean{

 public String MyMethod(String loginInfo) {
  System.out.println("Person login() " + "LoginInfo:" + loginInfo + this);
  return  " logining..."+loginInfo;
 }

}

 

application.xml配置

<context:component-scan base-package="com.apjept.bean.chapter"/>

调用

AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
  ctx.registerShutdownHook();
ctx.getBean("aspectJTestBean");

原创粉丝点击