一、用例说明
对于通过Spring的Bean注入对象,关于“如何实现对象初始化前后、初始加载及销毁方法前后等不同阶段解析处理”、“简单初始化配置实现复杂的bean加载过程根据隐蔽自动化”等等相关场景用例分析说明,具体通过@PostConstruct 和 @PreDestroy 、InitializingBean和 DisposableBean接口、FactoryBean接口等方式实现,
二、代码用例
1.如何实现对象初始化前后、初始加载及销毁方法前后等不同阶段解析处理,优先级:Constructor > @PostConstruct > InitializingBean > init-method > @PreConstruct > DisposableBean > destroy-methodimport javax.annotation.PostConstruct;@b@import javax.annotation.PreDestroy;@b@import org.springframework.beans.factory.InitializingBean;@b@@b@public class SpringBeanInitingLevel implements InitializingBean,DisposableBean{@b@@b@ public SpringBeanInitingLevel() { @b@ System.out.println(" 【Level】 1 ============================== > : Constructor "); @b@ } @b@ @b@ @PostConstruct @b@ public void postConstruct() { @b@ System.out.println(" 【Level】 2 ============================== > : PostConstruct "); @b@ } @b@ @b@ @b@ @Override @b@ public void afterPropertiesSet() throws Exception { @b@ System.out.println(" 【Level】 3 ============================== > : InitializingBean @afterPropertiesSet "); @b@ } @b@ @b@ public void initMethod() { @b@ System.out.println(" 【Level】 4 ============================== > : init-method "); @b@ } @b@ @b@ @PreDestroy@b@ public void preConstruct() { @b@ System.out.println(" 【Level】 5 ============================== > : PreConstruct "); @b@ } @b@ @b@ @b@ @b@ @Override@b@ public void destroy() throws Exception {@b@ System.out.println(" 【Level】 6 ============================== > : DisposableBean @destory "); @b@@b@ }@b@@b@ public void destoryMethod(){@b@ System.out.println(" 【Level】 7 ============================== > : destroy-method "); @b@ }@b@@b@}
spring配置如下<?xml version="1.0" encoding="UTF-8"?>@b@@b@@b@@b@@b@@b@ @b@
测试用例类SpringBeanInitingLevelTestimport org.junit.Test;@b@import org.springframework.test.context.ContextConfiguration;@b@import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;@b@@b@@ContextConfiguration(locations={"classpath*:spring-*.xml"})@b@public class SpringBeanInitingLevelTest extends AbstractJUnit4SpringContextTests {@b@@b@ @b@ @Test@b@ public void test(){@b@ System.out.println(" junit user case 1 ... ");@b@}@b@ @b@@b@}
运行控制台输出【Level】 1 ============================== > : Constructor @b@ 【Level】 2 ============================== > : PostConstruct @b@ 【Level】 3 ============================== > : InitializingBean @afterPropertiesSet @b@ 【Level】 4 ============================== > : init-method @b@ junit user case 1 ... @b@ 【Level】 5 ============================== > : PreConstruct @b@ 【Level】 6 ============================== > : DisposableBean @destory @b@ 【Level】 7 ============================== > : destroy-method
2.下面通过用例来具体说明 - 通过配置古龙的代表作《流星蝴蝶剑》销售量按照行业标准来鉴别该书是否好书,具体代码如下
Book类public class Book {@b@@b@private String title;@b@@b@private String author;@b@@b@private boolean isGood;@b@@b@public Book(String title, String author) {@b@super();@b@this.title = title;@b@this.author = author;@b@}@b@@b@public String getTitle() {@b@return title;@b@}@b@@b@public void setTitle(String title) {@b@this.title = title;@b@}@b@@b@public String getAuthor() {@b@return author;@b@}@b@@b@public void setAuthor(String author) {@b@this.author = author;@b@}@b@@b@public boolean isGood() {@b@return isGood;@b@}@b@@b@public void setGood(boolean isGood) {@b@this.isGood = isGood;@b@}@b@}
GoodBook类 - FactoryBean工厂实现类import org.springframework.beans.factory.FactoryBean;@b@@b@public class GoodBook implements FactoryBean {@b@@b@private int sales;//销售量@b@@b@public int getSales() {@b@return sales;@b@}@b@@b@public void setSales(int sales) {@b@this.sales = sales;@b@}@b@@b@@Override@b@public Book getObject() throws Exception {@b@Book book=new Book("流行蝴蝶剑","古龙");@b@if(sales>100000)@b@book.setGood(true);@b@else@b@book.setGood(false);@b@return book;@b@}@b@@b@@b@@Override@b@public Class> getObjectType() {@b@return Book.class;@b@}@b@@b@@b@@Override@b@public boolean isSingleton() {@b@return false;@b@}@b@@b@}
Gulong类 - 古龙本人public class Gulong {@b@@b@private Book book;@b@@b@public Book getBook() {@b@return book;@b@}@b@@b@public void setBook(Book book) {@b@this.book = book;@b@}@b@@b@}
spring配置<?xml version="1.0" encoding="UTF-8"?>@b@@b@@b@@b@@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@
SpringFactoryBeanTest测试类import org.springframework.context.ApplicationContext;@b@import org.springframework.context.support.ClassPathXmlApplicationContext;@b@@b@public class SpringFactoryBeanTest {@b@ @b@public static void main(String[] args) {@b@@b@ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); @b@ @b@ Gulong gulong=(Gulong)context.getBean("gulong");@b@ @b@ System.out.println(" 古龙的代表作《"+gulong.getBook()+"》是一本"+(gulong.getBook().isGood()?"有趣":"无聊")+"书 ");@b@@b@}@b@@b@}
控制台结果古龙的代表作《com.xwood.test.spring.Book@46e06703》是一本有趣书