springbean生命周期事件-创新互联
整体流程图
成都创新互联公司不只是一家网站建设的网络公司;我们对营销、技术、服务都有自己独特见解,公司采取“创意+综合+营销”一体化的方式为您提供更专业的服务!我们经历的每一步也许不一定是最完美的,但每一步都有值得深思的意义。我们珍视每一份信任,关注我们的网站制作、成都网站设计质量和服务品质,在得到用户满意的同时,也能得到同行业的专业认可,能够为行业创新发展助力。未来将继续专注于技术创新,服务升级,满足企业一站式全网整合营销推广需求,让再小的品牌网站制作也能产生价值!程序示例
maven依赖
org.springframework
spring-context
4.3.12.RELEASE
junit
junit
4.12
test
配置类
@Configuration
@ComponentScan("per.ym.processor, per.ym.service")
public class LifeCycleConfig {
@Bean(initMethod = "init", destroyMethod = "destroy")
public LifeCycleBean lifeCycleBean() {
return new LifeCycleBean();
}
}
实例bean
public class LifeCycleBean implements InitializingBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {
public LifeCycleBean() {
System.out.println("consturctor......");
}
public void init() {
System.out.println("init......");
}
@Autowired
public void setService(PersonService service) {
System.out.println("setService......");
}
public void destroy() {
System.out.println("destroy......");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet......");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("setBeanFactory......");
}
@Override
public void setBeanName(String name) {
System.out.println("setBeanName......");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("setBeanClassLoader......");
}
}
InstantiationAwareBeanPostProcessor
package per.ym.processor;
@Component
public class MyInstantiationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
@Override
public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessBeforeInstantiation......");
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessAfterInstantiation......");
}
return true;
}
@Override
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessPropertyValues......");
}
return pvs;
}
}
BeanPostProcessor
package per.ym.processor;
@Component
public class MyPostProcess implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessBeforeInitialization......");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessAfterInitialization......");
}
return bean;
}
}
service
package per.ym.service;
@Service
public class Service {
}
测试类
public class Test_Normal {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
@Test
public void test() {
applicationContext.getBean("lifeCycleBean");
applicationContext.close();
}
}
测试结果:
postProcessBeforeInstantiation......
consturctor......
postProcessAfterInstantiation......
postProcessPropertyValues......
setService......
setBeanName......
setBeanClassLoader......
setBeanFactory......
postProcessBeforeInitialization......
afterPropertiesSet......
init......
postProcessAfterInitialization......
destroy......
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文名称:springbean生命周期事件-创新互联
文章地址:http://scjbc.cn/article/ihoeg.html