使用spring annotation完成初始化 bean 和销毁前所做的操作

阅读更多
package com.myapp.core.annotation.init;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Service
public class PersonService {
  
	private String  message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	@PostConstruct
	public void  init(){
		System.out.println("I'm  init  method  using  @PostConstrut...."+message);
	}
	
	@PreDestroy
	public void  dostory(){
		System.out.println("I'm  destory method  using  @PreDestroy....."+message);
	}
	
}

 

spring xml 配置文件:

 








 

测试类:

package com.myapp.core.annotation.init;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
	
	public static void main(String[] args) {
		
		ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");
		
		PersonService   personService  =  (PersonService)context.getBean("personService");
		
		personService.dostory();
	}

}

 

你可能感兴趣的:(spring)