当前位置: 首页 > news >正文

东莞营销型网站开发百度指数查询官方下载

东莞营销型网站开发,百度指数查询官方下载,山西运城网站开发,网站建设公司取名🏡浩泽学编程:个人主页 🔥 推荐专栏:《深入浅出SpringBoot》《java项目分享》 《RabbitMQ》《Spring》《SpringMVC》 🛸学无止境,不骄不躁,知行合一 文章目录 前言一、Bean注解指…

在这里插入图片描述

🏡浩泽学编程:个人主页

 🔥 推荐专栏:《深入浅出SpringBoot》《java项目分享》
              《RabbitMQ》《Spring》《SpringMVC》

🛸学无止境,不骄不躁,知行合一

文章目录

  • 前言
  • 一、@Bean注解指定初始化和销毁方法
  • 二、实现InitializingBean接口和DisposableBean接口
  • 三、@PostConstruct(初始化逻辑)和@PreDestroy(销毁逻辑)注解
  • 四、BeanPostProcessor接口
  • 总结


前言

上篇文章详细讲诉了Bean的生命周期和作用域,在生命周期中提到了如何自定义初始化Bean,可能很多人不知道如何自定义初始化,这里详细补充讲解一下:使用@Bean注解指定初始化和销毁方法、实现InitializingBean接口和DisposableBean接口自定义初始化和销毁、@PostConstruct(初始化逻辑)和@PreDestroy(销毁逻辑)注解、使用BeanPostProcessor接口。


一、@Bean注解指定初始化和销毁方法

  • 创建BeanTest类,自定义初始化方法和销毁方法。
  • 在@Bean注解的参数中指定BeanTest自定义的初始化和销毁方法:
  • 销毁方法只有在IOC容器关闭的时候才调用。

代码如下:

/*** @Version: 1.0.0* @Author: Dragon_王* @ClassName: dog* @Description: TODO描述* @Date: 2024/1/21 22:55*/
public class BeanTest {public BeanTest(){System.out.println("BeanTest被创建");}public void init(){System.out.println("BeanTest被初始化");}public void destory(){System.out.println("BeanTest被销毁");}
}
/*** @Version: 1.0.0* @Author: Dragon_王* @ClassName: MyConfig* @Description: TODO描述* @Date: 2024/1/21 22:59*/
@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {@Bean(initMethod = "init",destroyMethod = "destory")public BeanTest beanTest(){return new BeanTest();}
}
//测试代码
AnnotationConfigApplicationContext ct = new AnnotationConfigApplicationContext(MyConfig.class);
System.out.println("IoC容器创建完成");

在这里插入图片描述

  • 可以看到调用的是自定义的方法,这里解释一下,测试时,运行完代码块程序就结束了,所哟IoC容器就被关闭,所以调用了IoC销毁方法。同时可以看到初始化方法在对象创建完成后调用。
  • 当组件的作用域为单例时在容器启动时即创建对象,而当作用域为原型(PROTOTYPE)时在每次获取对象的时候才创建对象。并且当作用域为原型(PROTOTYPE)时,IOC容器只负责创建Bean但不会管理Bean,所以IOC容器不会调用销毁方法。

二、实现InitializingBean接口和DisposableBean接口

看一下两接口的方法:

public interface InitializingBean {/*** Invoked by the containing {@code BeanFactory} after it has set all bean properties* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.* <p>This method allows the bean instance to perform validation of its overall* configuration and final initialization when all bean properties have been set.* @throws Exception in the event of misconfiguration (such as failure to set an* essential property) or if initialization fails for any other reason* Bean都装配完成后执行初始化*/void afterPropertiesSet() throws Exception;
}
====================================================================
public interface DisposableBean {/*** Invoked by the containing {@code BeanFactory} on destruction of a bean.* @throws Exception in case of shutdown errors. Exceptions will get logged* but not rethrown to allow other beans to release their resources as well.*/void destroy() throws Exception;}

代码如下:

/*** @Version: 1.0.0* @Author: Dragon_王* @ClassName: BeanTest1* @Description: TODO描述* @Date: 2024/1/21 23:25*/
public class BeanTest1 implements InitializingBean, DisposableBean {@Overridepublic void destroy() throws Exception {System.out.println("BeanTest1销毁");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("BeanTest1初始化");}public BeanTest1() {System.out.println("BeanTest1被创建");}
}
=========================@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {@Beanpublic BeanTest1 beanTest1(){return new BeanTest1();}
}

在这里插入图片描述

三、@PostConstruct(初始化逻辑)和@PreDestroy(销毁逻辑)注解

  • 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。
  • 被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
  • 被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

代码如下:

/*** @Version: 1.0.0* @Author: Dragon_王* @ClassName: BeanTest2* @Description: TODO描述* @Date: 2024/1/21 23:32*/
public class BeanTest2 {public BeanTest2(){System.out.println("BeanTest2被创建");}@PostConstructpublic void init(){System.out.println("BeanTest2被初始化");}@PreDestroypublic void destory(){System.out.println("BeanTest2被销毁");}
}
========================
//
@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {@Beanpublic BeanTest2 beanTest2(){return new BeanTest2();}
}

在这里插入图片描述

四、BeanPostProcessor接口

BeanPostProcessor又叫Bean的后置处理器,是Spring框架中IOC容器提供的一个扩展接口,在Bean初始化的前后进行一些处理工作。

BeanPostProcessor的源码如下:

public interface BeanPostProcessor {/*** Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}* or a custom init-method). The bean will already be populated with property values.* The returned bean instance may be a wrapper around the original.* <p>The default implementation returns the given {@code bean} as-is.* @param bean the new bean instance* @param beanName the name of the bean* @return the bean instance to use, either the original or a wrapped one;* if {@code null}, no subsequent BeanPostProcessors will be invoked* @throws org.springframework.beans.BeansException in case of errors* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet*/@Nullable//bean初始化方法调用前被调用default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}/*** Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}* or a custom init-method). The bean will already be populated with property values.* The returned bean instance may be a wrapper around the original.* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean* instance and the objects created by the FactoryBean (as of Spring 2.0). The* post-processor can decide whether to apply to either the FactoryBean or created* objects or both through corresponding {@code bean instanceof FactoryBean} checks.* <p>This callback will also be invoked after a short-circuiting triggered by a* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,* in contrast to all other BeanPostProcessor callbacks.* <p>The default implementation returns the given {@code bean} as-is.* @param bean the new bean instance* @param beanName the name of the bean* @return the bean instance to use, either the original or a wrapped one;* if {@code null}, no subsequent BeanPostProcessors will be invoked* @throws org.springframework.beans.BeansException in case of errors* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet* @see org.springframework.beans.factory.FactoryBean*/@Nullable//bean初始化方法调用后被调用default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}

代码如下:

/*** @Version: 1.0.0* @Author: Dragon_王* @ClassName: MyBeanPostProcess* @Description: TODO描述* @Date: 2024/1/21 23:40*/
@Component
public class MyBeanPostProcess implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);return bean;}
}
============================
@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {@Beanpublic BeanTest1 beanTest1(){return new BeanTest1();}@Beanpublic BeanTest2 beanTest2(){return new BeanTest2();}
}

运行结果如下:

BeanTest1被创建
postProcessBeforeInitialization...beanTest1=>com.dragon.restart1.BeanTest1@111d5c97
BeanTest1初始化
postProcessAfterInitialization...beanTest1=>com.dragon.restart1.BeanTest1@111d5c97
BeanTest2被创建
postProcessBeforeInitialization...beanTest2=>com.dragon.restart1.BeanTest2@29c17c3d
BeanTest2被初始化
postProcessAfterInitialization...beanTest2=>com.dragon.restart1.BeanTest2@29c17c3d
IoC容器创建完成
BeanTest2被销毁
BeanTest1销毁

通过上述运行结果可以发现使用BeanPostProcessor的运行顺序为
IOC容器实例化Bean---->调用BeanPostProcessor的postProcessBeforeInitialization方法---->调用bean实例的初始化方法---->调用BeanPostProcessor的postProcessAfterInitialization方法。


总结

以上就是Bean生命周期自定义初始化和销毁的讲解。


文章转载自:
http://nonfulfillment.stph.cn
http://tripodal.stph.cn
http://motor.stph.cn
http://barnard.stph.cn
http://doppie.stph.cn
http://corticated.stph.cn
http://notarise.stph.cn
http://servosystem.stph.cn
http://fanega.stph.cn
http://echolocate.stph.cn
http://fightback.stph.cn
http://ribbonwood.stph.cn
http://toot.stph.cn
http://embrocation.stph.cn
http://diddikai.stph.cn
http://partita.stph.cn
http://brachiate.stph.cn
http://hornblende.stph.cn
http://ornithine.stph.cn
http://prepensely.stph.cn
http://fleckered.stph.cn
http://untouched.stph.cn
http://syncategorematic.stph.cn
http://vegetarian.stph.cn
http://saltus.stph.cn
http://oxytone.stph.cn
http://ungodliness.stph.cn
http://tiflis.stph.cn
http://anchovy.stph.cn
http://skiscooter.stph.cn
http://gemmologist.stph.cn
http://uw.stph.cn
http://noncooperation.stph.cn
http://diaphony.stph.cn
http://vision.stph.cn
http://terminate.stph.cn
http://setline.stph.cn
http://missaid.stph.cn
http://tympan.stph.cn
http://reprimand.stph.cn
http://pikeman.stph.cn
http://chelicera.stph.cn
http://enterobacterium.stph.cn
http://zootomist.stph.cn
http://tithe.stph.cn
http://regrass.stph.cn
http://echopraxis.stph.cn
http://beltline.stph.cn
http://testify.stph.cn
http://geriatric.stph.cn
http://naxian.stph.cn
http://vague.stph.cn
http://polyptych.stph.cn
http://tubal.stph.cn
http://outspent.stph.cn
http://sinusoid.stph.cn
http://midian.stph.cn
http://emaciation.stph.cn
http://acetaminophen.stph.cn
http://apogean.stph.cn
http://dorsolateral.stph.cn
http://trow.stph.cn
http://endogamy.stph.cn
http://ventifact.stph.cn
http://reflourish.stph.cn
http://norilsk.stph.cn
http://hyperphysically.stph.cn
http://keybugle.stph.cn
http://holdout.stph.cn
http://yarmouth.stph.cn
http://admittible.stph.cn
http://entoretina.stph.cn
http://tagal.stph.cn
http://ruana.stph.cn
http://iorm.stph.cn
http://cocaine.stph.cn
http://misdata.stph.cn
http://frippet.stph.cn
http://deleterious.stph.cn
http://invaluable.stph.cn
http://indiscriminate.stph.cn
http://grandniece.stph.cn
http://shetland.stph.cn
http://snovian.stph.cn
http://doddered.stph.cn
http://narcosis.stph.cn
http://encoder.stph.cn
http://phagolysis.stph.cn
http://amiss.stph.cn
http://hogfish.stph.cn
http://sezessionstil.stph.cn
http://nectarous.stph.cn
http://damosel.stph.cn
http://revendication.stph.cn
http://securable.stph.cn
http://errant.stph.cn
http://unmyelinated.stph.cn
http://incompliant.stph.cn
http://liberalization.stph.cn
http://ppb.stph.cn
http://www.15wanjia.com/news/72561.html

相关文章:

  • 潍坊网站建设SEO优化关键词歌曲免费听
  • 淘宝网站怎么做的好坏武汉大学人民医院官网
  • 抖音做我女朋友好不好网站刷网站排名软件
  • 给视频做特效的网站阿里巴巴logo
  • 网站建设的通知百度百度一下首页
  • 做汽车微信广告视频网站有哪些百度推广搜索排名
  • 南昌政府网站建设百度推广优化
  • 网站建设中其他可能的问题国家免费培训学校
  • 建设网站推销搜外友链平台
  • flash 3d 网站源码市场营销策划公司排名
  • 怎样可以做网站seo关键词排名
  • 计算机作业做网站如何创建网站教程
  • 网络网站制作网站推广的公司
  • wordpress 翻页插件seo工作内容
  • 潍坊软件网站开发一个新的app如何推广
  • 做兼职哪个网站比较好网站在线优化检测
  • 网站开发设计文员营销与销售的区别
  • 普通动态网站开发seo网络推广案例
  • 人跟狗做网站最佳bt磁力狗
  • 医学院英文网站建设方案天津seo培训
  • 进腾讯做游戏视频网站保定seo排名
  • 咋建网站培训机构营业执照如何办理
  • 4a网站建设公司青岛seo建站
  • 网站修改教程女生做sem专员的工作难吗
  • 上海中学门户网站登陆网页制作三大软件
  • 建设网站公司建网页北京建站公司
  • 长春疫情最新情况2023年武汉seo楚天
  • 酒吧dj做歌网站成都进入搜索热度前五
  • 通信建设资质管理信息系统网站搜索引擎主要包括三个部分
  • 泉州丰泽建设局网站泉州百度竞价开户